Guides/Unity Project

*Guide by **SuperKael***

There are many reasons why you may wish to create a Roguelands Unity Project. Perhaps you wish to create AssetBundles, or maybe you just want to look at how the Unity assets and scenes are constructed. Either way, this page will explain how to create one.

The Roguelands Unity Project involves two parts: the game assets (sound/images/icons) and code (scripts, ids, behaviors).

## Exporting Unity Files

This method can be used for most of the games made in Unity to extract their assets. You will need uTinyRipper.

### Steps

1. First, open uTinyRipper. You will be presented with a window with the text "Drag & Drop files/folders here".
2. Open the 'Roguelands' folder, the one containing 'Roguelands\_Data'.
3. Drag 'Roguelands\_Data' into the uTinyRipper window. If you are unable to do so, make sure you are NOT running uTinyRipper as administrator.
4. After a moment, it should say "Files has been loaded", with a button labeled 'Export'. Press 'Export', and choose a destination directory. It will place the extracted Unity files into a subfolder (that uTinyRipper will create) of wherever you select.
5. Now, the actual data extraction will be performed by uTinyRipper. Be patient, this can take a long time. Once complete, open the folder you told it to export to.

However, there is one type of file uTinyRipper can't extract: Code.

## Creating the Unity Project

This part uses a lot of space on the computer. It requires Unity 2017 (2017.3.0 Unity Installer recommended).

1. Select the folder named 'globalgamemanagers' (Which was created by uTinyRipper) and rename it to 'Roguelands'.
2. In Unity, tell it to open/add an existing project, and put it to the 'Roguelands' folder that you just renamed. It may or may not need a while to process the files into a project.

This Roguelands project, although functional in the editor, will do absolutely nothing if you click play (like a skeleton).

## Exporting the Code

This section explains how to achieve partial code functionality in the Unity Project. You will need dnSpy to see the code and Visual Studio 2017 or higher (Preferably Visual Studio 2019).

If you want only to see the game code for your game, you will only need dnSpy (Also works for other Unity games).

### Steps

1. Open the Roguelands/Roguelands\_Data/Managed folder inside your game installation.
2. Open Assembly-CSharp.dll in dnSpy.
3. Select 'File' -> 'Export to Project...'. For 'Folder', point it to the Assets/Scripts folder in your Roguelands Unity Project. For 'Version', select 'VS2017'. Make sure 'Language' is set to 'C#'. Then press 'Export'.
4. Repeat steps 2 and 3 for Assembly-UnityScript.dll in the Managed folder.
5. Then, open the Assets/Scripts/Assembly-CSharp folder, and move the 'PlayerPrefs.cs' file into the 'PreviewLabs' folder. Replace the existing file as you do so.This is necessary due to an inconsistency between the behavior of uTinyRipper and dnSpy.
6. Now, you really should be done, except dnSpy's code continues to be imperfect so you must also delete the Assets/Scripts/Assembly-CSharp/Properties folder.
7. There are two more files that need to be fixed: Assembly-CSharp/WormEgg.cs, and Assembly-UnityScript/Vignetting.cs. However, these require careful modification, so open them in Visual Studio.

* For WormEgg.cs, find the **IEnumerator Blink()** method, and replace the erroring

"`*goto IL\_D2;*`"
with
"*`yield break;`*"
* For Vignetting.cs, find the **void OnRenderImage(RenderTexture source, RenderTexture destination)** method, and replace the erroring

"*`bool flag = (Mathf.Abs(this.blur) > (float)0) ?? (Mathf.Abs(this.intensity) > (float)0);`*"
with
"*`bool flag = (Mathf.Abs(this.blur) > (float)0) || (Mathf.Abs(this.intensity) > (float)0);`*".
And now you're totally done! dnSpy's decompiled code has been added to your project, and as such it is now **partially** functional. It's important to keep in mind that, although the project is now code-complete, dnSpy is **imperfect**. As such, although the game will run and have partial functionality, it will still be severely broken. As such, don't expect to be able to just play the game in the Unity editor - that being said, it is more than good enough for testing and creating AssetBundles.

## Creating AssetBundles

AssetBundles are a collection of one or more assets stored in a file, that can be loaded by a Unity game at run-time.

In terms of Roguelands modding, this means you can create things like new enemies and such in the Unity editor, export them to a file, and then load them with your mod.

That being said, unfortunately, the Unity Editor UI doesn't provide a way to create asset bundles - you need an editor script to do it. The following steps describe how to set up this editor script, export the AssetBundle(s), and then load them with your mod using Gadget Core.

### Requirements

* A Roguelands Unity Project (created in the steps above)
* AssetBundleCreator.cs
* GadgetCore

### Steps

1. First, in your Roguelands Unity Project, create a folder named "Editor" inside the "Assets" folder, and drop the AssetBundleCreator.cs file into there.
2. Upon doing so, you can now select on the menu bar at the top of the Unity Editor window 'Assets' -> 'Build Asset Bundles'. *Do not actually select this yet, just verify the option appears.*
3. Go through the Assets folder and its subdirectories, and select all of the assets that you want to include in your AssetBundle. For each one, once selected, look at the very bottom of the Inspector panel, and select the dropdown to the right of where it says "AssetBundle".For the first asset you add, select 'New...' and then enter the name of your AssetBundle. For all later ones, just select the name of your bundle from the dropdown.
4. Once all of your desired assets have been added to the bundle, now select 'Assets' -> 'Build Asset Bundles'. After a few seconds, this will create a folder inside your project folder named "AssetBundles".
5. There will be a few files there, but the one(s) you are interested in are the one(s) that match the name(s) of the bundle(s) you specified earlier,**Do not end in .manifest**. The bundles have no file extension - normally, you would want them to end with '.assets', but in this case I have intentionally made it so that they will not. This is because if they have the '.assets' extension, UMF will try and fail to load them, and always crash the game when loading your mod.

Alright, so now your Asset Bundle(s) have been created - now you just need to actually be able to load them into the game.
6. Place the Asset Bundle(s) inside of the Assets/ModName/ folder inside your mod - just like you would textures or audio clips.
7. To load these into the game, call GadgetCoreAPI.LoadAssetBundle(string file, [bool shared = false]). Pass the name of your AssetBundle file to this method, and it will return an AssetBundle reference. To load assets from the AssetBundle, use AssetBundle.LoadAsset. Note that this uses essentially the same syntax as Resources.Load, although it requires fully-qualified asset paths. So, if you have an asset in the AssetBundle at the path 'Assets/Resources/e/customenemy', then you have to pass "Resources/e/customenemy" to AssetBundle.LoadAsset.

And you're done! Now you can create and load AssetBundles in your mod!

## Creating Mods

### Requirements

* Visual Studio Community Integrated Development Environment (IDE)
* dnSpy Game Decompiler (open Roguelands\_Data\Managed\Assembly-CSharp.dll)
* Harmony is the game patcher used by GC and UMF. (Harmony's Documentation)

### Template Mod

* GadgetCore provides a Template Mod.
* Download and open it in Visual Studio and follow Readme instructions.
* You can ask on New Roguelands Discord server for modding help.

### IDs

* Combat Chip IDs
* Item IDs
* Enemy\_IDs
* Projectile\_IDs