- Home /
Load Script in AssetBundle at Runtime
Hey, I try to load scripts in AssetBundle in runtime. I created an asset bundle that contain a prefab. This prefab has some components. The scripts of these components are not in the bundle (Because of a bundle cant contain scripts) . When I download/load the bundle, prefab can not load the scripts because of these scripts are not in the project folder(not compiled). So I compile (create dll file) these scripts and put the dll file to bundle (as a bytes file). Now I can access the dll file (Assembly.load) and I can addComponent to and a gameObject. That is ok. I know that I need to load the dll before creating the prefab. But when I create a prefab, still prefab can not load the script. I can not add a new component to the created object because component has some referances. What can I do? Thanks
Answer by mikewarren · Dec 11, 2018 at 06:41 PM
You might want to look here. It was quite a while ago, but I was well into trying to create prefabs with scripts from Asset bundles at the time.
https://answers.unity.com/questions/987827/how-to-resolve-script-references-in-scene-bundles.html
What I learned is that it can be done, but it takes some effort on your part. The serialization system has references to the file (assembly) which a script comes from. I read something on it recently, but can't recall exactly how it works. Anyway, it's not enough to have the compiled code for the script, but it must be in the same assembly from which it was referenced originally (EG. in the editor). Before assembly definition files, that was the Assembly-CSharp.dll file that the editor creates and is packaged into players.
The easier thing I've found is to create a separate Visual Studio project and build all the scripts into an Assembly and copy it into your project as a plugin. You can reference scripts from that plugin within the editor to create your prefab. When you instantiate a prefab from the asset bundle, as you noted, you'll just need to load the plugin first. It's not very convenient.
You might try using Assembly Definition Files to package your prefab scripts into an assembly of your choosing. On the loading side, try loading that assembly first, then instantiating the prefab. (You could probably even package the plugin as an asset within the bundle.) I haven't tried it, but it may keep you from having to have separate code projects.
Good luck.
Mike
Hey. Thaks for answer. But I couldnt get it. I download a bundle at Runtime and instantiate a prefab. But components/scripts of this prefab is not in the project folder so prefab can not find these component. This is normal. So I compiled the scripts and created a dll file and then changed the format (dll to bytes) and created the bundle that contains this dll. Now can download the bıundle and add a component that in the bundle to a gameObject. But when I try to instantiate a prefab, the prefab still cant find its components. How it works Assebly.Load() ? Why I cant use all scripts in the bundle when I load the dll? And also dll's can not be in project folder in my project. I need to build my app one times and when I want to add new object to my app, I will not update the app. Just create a new bundle and $$anonymous$$y app will downlaod and use this bunde. Can I do this?
I'll spend a little time investigating. It's been a while since I looked at this, and things may have changed. If I come up with a working example, I'll post it.
Sorry. I can't get it working again either. Haven't tried it since Unity 5.x, so the system must have changed since then.
Pretty good post on serialization here. https://unity3d.com/learn/tutorials/topics/best-practices/assets-objects-and-serialization
I need more insight on the serialization system. If I understand, there's a file id and local id associated with each asset. Somewhere there must be an association between the file id and an assembly (Persistent$$anonymous$$anager, maybe?). I'd have thought the Assembly Definition Files might add some support for run-time asset loading through bundling, but I don't see it.
Your answer
Follow this Question
Related Questions
Runtime loaded assemblies 1 Answer
How to load images/object at runtime and/or create assets server-side? 0 Answers
How do I access a script component added to a gameobject from an assembly at run-time 0 Answers
How to import the object from server to unity 2 Answers
Create Assetbundles in final build? 0 Answers