Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Including scripts in AssetBundles

Discussion in 'Scripting' started by Totaaaaaa, Mar 8, 2017.

  1. Totaaaaaa

    Totaaaaaa

    Joined:
    Jan 22, 2017
    Posts:
    1
    Hi
    I'm working in a simple game that has multi levels. Player can use just level 1 and then he has to download other levels from server.
    Every level is an asset and I used AssetBundles for downloading assets from server.
    but I can't include the scripts in my assetBundle.
    Can anyone give me an example about including scripts in assetBundles or any way to download my scripts.
    Note: I try to use the code here But it's not working with me.
     
  2. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    Short answer: you can't.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,469
    Including scripts in assetbundles is very difficult. Even Apple doesn't allow it...however, there is one exception to this. This only applies to new scripts. For example, I can't write a script, attach it to a prefab, include that prefab in an asset bundle and then delete the script. The game doesn't allow that as it will look for the script in the project. (it doesn't actually get included in the bundle).

    So, if the script is still in your project, you're good. But you're not going to be able to include a new script with an assetbundle that isn't in your release. So, for your example, as long as you aren't including a new script with every level, you are just fine including existing scripts into your asset bundles.
     
    snobalpaul likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    7,954
    You can have prefabs that have scripts as components, but the scripts themselves must be included in the original build in order to be able to be compiled. This is only a problem if you're writing additional scripts after the release of your game; scripts take up a negligible amount of space in your build.

    It's possible, but extremely difficult, to load compiled DLL's after booting the game. However, this only works on certain platforms (desktops, mostly), and it's extremely advanced level coding. If you want to give this a shot, research how Kerbal Space Program does it; there are a few public talks about KSP's support for mods. I'm fairly sure their build process involves modifying the built binaries after Unity builds them. TBH, this is probably of the level of difficulty of, "if you're asking how to do this on a forum, you're not capable of pulling it off". Hell, it's beyond my abilities, and I've been working professionally in Unity for 10 years.
     
    Joe-Censored likes this.
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    10,685
    Another commonly used approach is to use a runtime interpreter to allow modders and level designers to script custom behavior. You can include these as TextAssets in your asset bundle and feed them to something like MoonSharp at runtime. Compile your high performance code in your build, and expose functionality to the interpreter that your level designers can use in the runtime interpreter.
     
    snobalpaul likes this.
  6. Inter-Illusion

    Inter-Illusion

    Joined:
    Jan 5, 2014
    Posts:
    497
    There is no reliable way of adding a new script to the AssetBundle and having it compile in all platforms.
    What I have been using to do a similar thing, its to use an scripted language (e.g. lua) or a flow graph (e.g FlowCanvas, PlayMaker, etc).

    That way, you can add your lua text to the AssetBundle and run it in the device, or add your playmaker FlowCanvas object and run the graph inside it.
     
    Psyco92 likes this.
  7. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    234
    It is possible, but not directly. And it only works on platforms that support Assembly.Load (Windows, Linux, OS X and Android). There are two requirements.
    1. The scripts have to be compiled to a dll and assets have to reference the dll's Types instead of script files.
    2. The dll has to be loaded before the AssetBundle is loaded.
    The easiest way to do this is to do all of the scripting in a VS or Monodevelop project, that you compile and import into the project. If you then build an AssetBundle, the assets inside will reference that dll. The game can then load that same dll (with Assembly.Load) before loading the AssetBundle.
     
    Psyco92, Kender and FlightOfOne like this.
  8. cyuxi

    cyuxi

    Joined:
    Apr 21, 2014
    Posts:
    43
    I have no experiences in these issues, but regarding to such topic, i think If you are developing for android devices, you can try to build an apk as the main entry for your game, but build additional levels as separate apk respectively(maybe you should hide these level apk's icons by editing the launch activity in the android manifest , make them invisible so they won't disturb people) .
    By doing so, the main game apk is run as a service process which maintains a list of available levels,when an apk of level is downloaded as DLC and run successfully, the main game process will be switched to the background and run as a service.
    Whatever player is quit or finished, you just simply switch the processes between the main game and the level. For instance, If player has done and no longer need the current level you can just kill the process and avoke the main game process.
     
  9. srishtim

    srishtim

    Joined:
    Jul 29, 2017
    Posts:
    1
    Can
    Is that possible to do that without installing the downloaded apk ?
     
  10. zainnn

    zainnn

    Joined:
    May 6, 2019
    Posts:
    10
    Still looking for a legitimate solution...
     
    taguados likes this.
  11. CodeInMotion

    CodeInMotion

    Joined:
    Jun 1, 2018
    Posts:
    2
    so, is there still no solution to this?
     
  12. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    242
    Solution to what? You can't use coded classes or assemblies that do not exist in the distributed binary on ahead of time compiled platforms (il2cpp).

    This means you can reference any scripts or code that existed in the binary.

    Use PlayMaker or another interpreted scripting solution for these assets.
     
  13. drew55

    drew55

    Joined:
    Dec 13, 2017
    Posts:
    24
    Exactly, @AliMagsman and @zainnn -- you're talking about a very tall order to have compiled C# end up on multiple platforms, etc. As @doctorpangloss mentioned, you have two options: an interpreted script (Moon script is a solid choice), or you've got a .NET compiled dll authored as a library project in VS (that you drop in your asset project and also in your deployed project). Two solid choices!
     
    Psyco92 likes this.
  14. Mjbgtaad56

    Mjbgtaad56

    Joined:
    Aug 1, 2015
    Posts:
    3
    It'd be nice to be able to update a game without having to replace the whole binary each time, though I suppose the majority of the game is in the textures and stuff anyway, typically.

    Also, as a sort of solution to this, if you needed to add custom scripts to levels, you could implement a script engine in your game that would parse and run a file. I'd just recommend sticking with internal scripts that could be added to a level as needed though.
     
  15. RaviVohra

    RaviVohra

    Joined:
    Mar 8, 2019
    Posts:
    5


    This guy runs the asset bundle scene that has object with script attached , please tell me if he can do that so why can't we.
     
    r3dok1 likes this.
  16. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,469
    Not going to watch a 50+ min video myself, but...

    If the script is brand new and not part of the original project, it doesn't work. If it's in the build and you create new asset bundles with the scripts, they work just fine. And note, this will work in the editor because the editor has the script in it, so downloading the asset bundle with the same script is just going to work.

    Now, there is a way around this on certain platforms. Such as https://docs.unity3d.com/550/Documentation/Manual/scriptsinassetbundles.html
     
    Zenix likes this.
unityunity