Search Unity

Scripts in Assetbundles Not Supported

Discussion in 'Asset Bundles' started by aer0ace, May 23, 2018.

  1. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I'm a little annoyed that you can't store Monobehaviour scripts in AssetBundles. What I'm trying to do is make a driver program that can load Assetbundles and run that assetbundle (well, pair, because Scenes can't be contained in the same bundle as other assets) as a standalone mini game, such that I can simply create new assetbundles that can be downloaded for more minigames, sort of like a growing list of vignette games. This requires scripts to be accompanied with the assets, but I'm pretty sure that it's not supported for security reasons, but the only way I see being able to add custom behavior to a self contained AssetBundle is to add a scripting language on top of Unity C#, and that sounds like a lot of work.

    Does anyone have any other ideas?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    as long as you don't deploy to AOT platforms (e.g. iOS) and use Mono (not IL2CPP) as a scripting backend, you can include your DLL as a '.bytes' file in your bundle, then load the assembly from the content with 'System.Reflection.Assembly.Load(byte[])'.

    you then must load your scripts from code, with 'assembly.GetType("full name including namespace")'.
    your scene can't reference those scripts directly.

    adding your own DSL may be safer though, as you can control what you can access/do from it
     
  3. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Thanks for the feedback. That's a pretty slick way of handling MonoBehaviours. You're probably right though, with the DSL, which I can only guess is "<Something> Scripting Language". It turns out I'm starting to use MiniScript from the Asset Store, and I may use that in this instance.
     
  4. plunntic

    plunntic

    Joined:
    Nov 28, 2012
    Posts:
    12
    Why don't you just use IronPython? It's like 10 minutes of work to integrate it and you get a real programming language instead of some sort of roll-your-own scripting-thing.

    Anyway, my point is - IronPython is stable, mature, can be compiled on-the-fly (although i don't know if compilation will work on mobiles and other restricted platforms - probably not) and you have your problem solved in a couple of minutes.

    Here's how to integrate IronPython - http://techartsurvival.blogspot.com/2013/12/embedding-ironpython-in-unity-tech-art.html

    And here's how to write your own unity importer for *.py files (so Unity can see those as TextAsset's in the editor) - https://docs.unity3d.com/Manual/ScriptedImporters.html

    Disclaimer: Never used it on production, so it's always possible that i'm wrong - but i've integrated it in my own forever-work-in-progress game and it is working without any problems.
     
  5. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    I've already evaluated and integrated MiniScript into another project of mine, so I'm comfortable with using it for this other assetbundle-specific project. I need something super light-weight, and MIniScript foots the bill. This is indeed a mobile project that I'm working with using assetbundles. While Python and Lua are fantastic scripting languages, it's overkill for what I'm doing.