How to compile script to include it to AssetBundle?

According to this manual I can include scripts as binary data and load them via Reflection.
I understand all steps, except one: how can I compile script to get binary data for stroring it in .binary file?
Is there any tutorial/help?

2 Answers

2

Solution:

  1. Open Monodevelop and create C#Library project
  2. Add UnityEngine.dll to References
  3. Compile your scripts
  4. Copy bin/.dll from your library project folder to Assets of your unity project and change extension from .dll to .bytes

That’s all.

The only problem with this approach is with MonoBehaviours. Suppose you have some script (i.e. component) attached to GameObject. And you want to stream both in AssetBundle. You store the assembly (as TextAsset) and the GameObject in the AssetBundle, and load them in run time. But loading the assembly will not re-connect the script to the GameObject. You'll need to do it manually, with AddComponent. And that means you lose all the parameters you configured for the script. Which is bad. If there's some solution, it will be appreciated.

@Terikon did you get any solution for loading script in assets bundle ?

@Bunny83 i am trying to add script as a component of gameobject in assets bundle. I followed above points. but i stuck at 3 rd point that is compile your script . I followed 2 steps and now i have confusion like how it will have reference of my script attached to game object after i put .bytes of that .dll in library project. ?

@Ekta Mehta D. Well, all you have to do in MonoDevelop is to click build / build PROJECTNAME in the menu or simply press F7 that will actually compile your project and it should create a DLL file in that project folder. This DLL contains your compiled classes, Personally i only use Visual Studio (Express) which is far more stable and reliable than MonoDevelop. However the process of building your project is almost identical ^^.

@Bunny83 can you tell me exact location of the dll where i can find ? I found dll in library folder of that project. In that ScriptAssemblies folder i have and there are dlls available. I try to add those dlls but its not working.

We used to load asset bundles, in which the scripts were attached the objects.
It worked for us.
Unity automatically compiles the script attached to the GameObjects.

~Thanks

That's simply because the scripts aren't included in the Assetbundle but are already part of your actual application. So when you load an asset from an assetbundle it has no problems finding the scripts. The question is more about adding those scripts later along with the assetbundle.