- Home /
Can we make a script an Addressable in Unity ?
I am able to create Prefab using Addressable . If i am creating new prefab and upload addressable on server . It generated in already made game build . But when i attach script on prefab . Script does not generated in game . It only generate with prefab with script if we make build of game .Same if we edit script of prefab . Please let me know can we make script addressable?
Hello @ramkesh have you find any solutions on including scripts with addressable?
Answer by Bunny83 · Jun 06, 2020 at 10:36 AM
No, you can't ^^. All code has to be compiled into assemblies and depending on your build platform might even be completely AOT compiled into native code. The Unity engine runtime does not even ship with the C# compiler.
Depending on the platform and scripting backend there are ways to load compiled assemblies dynamically at runtime. However that has to be done manually through reflection. However this is of course not possible on any AOT platform (WebGL / iOS / IL2CPP / ...).
Answer by DanVioletSagmiller · Oct 08, 2021 at 09:51 PM
I was able to include scripts in bundles that would download separatly and import live, which ran just I had to do something to trigger the code to start, and used a Locator service, so the script could embed itself and replace the current pieces it wanted (and use existing ones)
As far as I was aware, Addressables is a glorified topper to bundles. I have not tested this in Addressables itself, but the premise worked in Unity 2017.
[UPDATE] This is invalid. After some discussion I went back and tested further. The script was included in the bundle, BUT, it was also in the initial build, and so when requested to be loaded by a scene element, it was already part of the assembly. Any attempts push new code after the initial build failed. A long time ago I tried to create a test case specifically for this, but I wrote a bad test with a false positive. :(
Have you actually read my answer? A Unity build usually does not ship with a C# compiler and C# scripts don't magically compile themselfs into assemblies. This is done in the editor. You can not load individual script files. As I said, there are ways to dynamically load pre-compiled assemblies through reflection. However that's not possible on any ahead-of-time compiled platform. Are you sure, what the scripts you try to load from bundles aren't part of the Unity project when you build it? Unity generally includes all script files in your project when your project is compiled. So you may had the impression that you somehow load it dynamically when in reality the script was already part of the build.
(I am wrong, see next reply from me) Yes, I read your post. But I have actually done this. My statement wasn't based on conjecture. Why would you assume a script included in an asset bundle wouldn't get compiled? Do you think that unity produces only one assembly to run the game from? I have done this with IL2CPP before, an if I recall right a WebGL. I couldn't use reflection to load it, reflection couldn't even pull a list of assemblies that were loaded at the start, but when the object being loaded included a reference to the script held inside, that was loaded with it, it triggered it anyway, allowing the new script to take over. I cannot claim to know why it works, but it does. I used this as a functional plugin system to allow live plugins to an app. I literally used this to replace scripts inside a locator service.
However, I keep seeing posts that defaulting to saying you can't with various reasons. And I haven't seen anyone else doing it. So, I'll plan on creating a video proof for it. Given your consideration that maybe the script was in the original assembly as a possibility of why my script worked, I cannot validate, as I don't have the code in front of me. However, I'll post back here with a humble belief, or a proof. :)
Dan, we're all rooting for you to be right but there are valid doubts that need to be dispelled. For example, what Bunny83 said is exactly what Unity manual once stated explicitly:
Including scripts in AssetBundles
AssetBundles can contain scripts as TextAssets but as such they will not be actual executable code. If you want to include code in your AssetBundles that can be executed in your application it needs to be pre-compiled into an assembly and loaded using the Mono Reflection class (...)
https://docs.unity3d.com/550/Documentation/Manual/scriptsinassetbundles.html