Search Unity

Use Asset Bundles for mods

Discussion in 'Asset Bundles' started by Noxalus, Apr 7, 2020.

  1. Noxalus

    Noxalus

    Joined:
    Jan 9, 2018
    Posts:
    80
    Hello everyone,

    I'm working on a game I would like to make moddable and I'm trying to find the best way to handle external contents.

    The Asset Bundles could be a good option as it would allow a lot of things, but I have an issue I don't know how to resolve.

    I've created an empty project in which I created an empty GameObject with a simple script that write something in the console and made a prefab of it. Then I put this prefab in the Asset Bundles, build them and copy them to the StreamingAssets folder of my game.

    Then in the project of my game, I have a ModLoader class with this method:

    Code (CSharp):
    1. public void LoadMods()
    2. {
    3.     var assetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "Mods/Mod1/EntryPoint"));
    4.     var prefab = assetBundle.LoadAsset<GameObject>("EntryPoint");
    5.     var instance = Instantiate(prefab);
    6.  
    7.     DontDestroyOnLoad(instance);
    8. }
    But when I look at the instantiate GameObject, the script that should write a message in the console is missing:

    upload_2020-4-7_19-49-12.png

    It's should be because the AB don't embed scripts, so I tried to load the first project DLL and load it in my game project like this before to instantiate the GameObject:

    Code (CSharp):
    1.  Assembly.LoadFrom(Path.Combine(Application.streamingAssetsPath, "Mods/Mod1/Libs/Mod1.dll"));
    But it doesn't work neither, the script is still unknown.

    Do you know why? How can I resolve this issue? Is it a good idea to use AB for modding?

    Thank in advance for your help!
     
  2. K2017

    K2017

    Joined:
    Jul 16, 2017
    Posts:
    6
    Unfortunately this isn't possible with asset bundles alone. Scripts are not included in bundles because the C# compiler cannot know which platform the script should be compiled for, and on which platform the bundle will be used.
    EDIT:
    The above given reason is not true as pointed out by Hyp-X below.

    One way to get around this is by supporting a scripting language like Lua for modding purposes. Or, if you want to go down the rabbit hole a bit, compile C# scripts at runtime. There are various assets on the asset store that provide this functionality. I can personally recommend RoslynCSharp, as it has served me quite well.

    Hope this helps.
     
    Last edited: Apr 19, 2020
    Noxalus likes this.
  3. Hyp-X

    Hyp-X

    Joined:
    Jun 24, 2015
    Posts:
    438
    Except AssetBundles are platform specific. Unity won't load a different platform's AssetBundles so this is not the reason at all.
     
  4. Hyp-X

    Hyp-X

    Joined:
    Jun 24, 2015
    Posts:
    438
    You are right AB's don't embed script, only a reference to a script.

    Now Unity can have two type of script reference:

    1. A GUID of the source file of the script.cs (and you cannot have more than one MonoBehaviour or ScriptableObject in the same source file)
    This kind of reference will only work for files that are included in source form in your project

    2. A GUID of an imported DLL file and file-id has of the class name -- for a DLL that is placed in your project and imported.

    Even if you load a DLL during run-time, Unity won't find a script in it.
     
  5. TihironRrr

    TihironRrr

    Joined:
    Jan 2, 2021
    Posts:
    4
    Please tell me, did you manage to save your prefab outside of the project/game? Does it work in a compiled game?
     
  6. TihironRrr

    TihironRrr

    Joined:
    Jan 2, 2021
    Posts:
    4
    Also, please tell me what the StreamingAssets folder looks like in the compiled game.
     
  7. TihironRrr

    TihironRrr

    Joined:
    Jan 2, 2021
    Posts:
    4
    If you help me with saving/loading the AssetBundle to/from disk, then I can share my way of creating modifications for the game
     
  8. TihironRrr

    TihironRrr

    Joined:
    Jan 2, 2021
    Posts:
    4
    I've already figured it out myself, but if you're still interested in how I created modification support in my game, then I can tell you