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?
Answer by der_wolf
Jun 03, 2012 at 02:22 PM
Open Monodevelop and create C#Library project
Add UnityEngine.dll to References
Compile your scripts
Copy bin/.dll from your library project folder to Assets of your unity project and change extension from .dll to .bytes
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 ?
@Ekta Mehta D.
"Scripts" are not assets from the runtime's point of view. Unity compiles all scripts into an assembly which is included directly in the build. As mentioned above and in the docs you can use reflection to load a custom assembly at runtime. However the classes inside that assembly aren't recognised as "assets".
There's this old blog post of AngryAnt on that topic. I haven't tried loading an assembly recently. In the past i got it working but without using assetbundles at all. It might be possible to load an assetbundle with attached components from an external assembly as long as you load the assembly before loading the assetbundle as the classes should already be available when Unity loads the bundle. So either use two assetbundles (one for the assembly and one for the assets) or simply load the dll file seperately.
As i said i'm not sure if that will work but i think it should. Be careful to not "overlap" classes in your assemblies. So you should not include the same class in two assemblies. So if you have a class / script already in your unity project you can't include it in the custom assembly. If you need access to certain classes from your actual project, those classes should be compiled into a seperate "shared" assembly. That assembly would be included in the actual project. The advantage is that when you create your custom assembly you can simply reference the common assembly and have access to those classes.
Also note that once an assembly is loaded into memory it can't be "unloaded" at runtime only by restarting the application. That's a limitation of .NET / Mono. The only way to do something like that is using a seperate AppDomain which can be unloaded entirely as a whole. Using AppDomains can be a real pain and they won't work for MonoBehaviours. Also when i was testing this stuff i noticed that creating an AppDomain on Android seems to be not supported as the app always crashes at that point. Maybe this has been fixed already
@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. ?
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,
build / build PROJECTNAME
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 ^^.
Answer by rohankad
Sep 01, 2015 at 01:41 PM
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.
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.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
7 People are following this question.
How to import the object from server to unity
The name 'Joystick' does not denote a valid type ('not found')
yield on WWW? or AssetBundleRequest? or both?
Copy the file from assets and paste it out side the assets folder
Dynamically loading scripts