Adding scripts to asset bundles - Neerhom/COM3D2.ModLoader GitHub Wiki

I've already covered the Creation of asset bundles and how to load them into game using ModLoader.

The page I want to cover how to create asset bundles with script components.

So what is a script component and why it's important? A script component, also called MonoBehavior, is like any Unity component that can be attached to GameObject , except it's behavior is defined by the code written by user. You can read more about it in Unity Manual

Since C# is the most dominant language for coding in Unity and is the only language that i somewhat know, the further explanation only covers C# side of things.

In term of C#, the script Components is a single C# class, whose methods will be executed when a GameObject ( by GameObject I mean any object in the scene) with script component is initiated. For instance the reflection in ModLoader's Mirrors is a result of a script component projecting the environment onto a texture and mapping it to a flat surface.

So why script components need additional coverage? The reason for that, is when you build an asset bundle from prefab that has a script attached to it, the built bundle won't actually have any executable code, instead it will only have a reference to class inside assembly, which it'll instantiate upon being loaded. Depending on where script was located inside you project, the built bundle will reference the Assembly-CSharp.dll or Assembly-CSharp-firstpass.dll (there might be other factors, but not something I've investigated in detail). So if referenced assembly doesn't have required class, the game will throw a warring message in console about missing reference.

So how to create bundles with scripts that game doesn't have? Well, luckily, the assembly and class references can be changed using SB3UGS which is a utility for modding Illusion games( such as Honey Select, Koikatsu) but is also usable for viewing and editing COM3D2 and CM3D2 files (namely resources.assets and .asset_bg files).

So right after build you bundle, create a folder called COM3D2x64_Data and put you bundle inside it. This is need because of SB3UGS derives game's structure from folder from which bundle is opened. Another note, is that SB3UGS doesn't recognize .asse_bg as a valid file extension and will refuse to open such files. What you want to do is for you bundle to either have no extension (all bundles are built without one) or change it to .unity3d.

Once that is done, we can launch SB3UGS and open the bundle in it ( drag'n'drop is supported).

Once you open you file, you'll see it's list of GameObjects under Animator tab, double click to open it to see what it stores.

Once you open it, you'll see that it's quite similar to object hierarchy in Unity editor. What what we want is to navigate to Object has MonoBehaivor attached. Double Click on MonoBehavior to open the editor.

As you can see, the editor shows the assembly name, namespace and name of the class which this MonoBehavior is referencing. In this case, it references class TextureLoader in Assembly-CSharp.dll. When i wrote the script, my class was not in any namespace so this field is empty. Since COM3D2's Assembly-CSharp.dll has no class with such name, the game will throw a console warning, when bundle is loaded, so what we want, is to change the assembly reference to one that has this class. In my case the assembly name will be TextureLoader.dll

Now it's just a matter of saving the bundle with File->Save or Ctrl+S shortcut. Upon Saving, the SB3UGS create a backup, so if you mess something, you don't have to panic. Another thing to note, is SB3UGS doesn't support compression of asset bundles, so the save file will grow in size.

And that's pretty much all regarding bundle side of thing, as all that is left is to change file extension and create .nei file for it.

While we changed assembly reference, we still need to create referenced TextureLoader.dll with TextureLoader class int it. Since my script (at least that how Unity calls it) is written in C#, what i need to do is to build it into .NET 3.5 class library using a C# compiler, such as Visual Studio. I won't cover this part, as this is regular programming stuff, so there should be a guide on it on the Internet. After building it, we also must load it into game's memory for bundle to reference. This can be done by loading it as UnityInjector plugin, because UnityInjector doesn't actually care what it loads.

And this is all there is to it.