Search Unity

Dynamic DLL + AssetBundle prefab missing component error.

Discussion in 'Scripting' started by Okais007, Dec 28, 2018.

  1. Okais007

    Okais007

    Joined:
    Jan 22, 2017
    Posts:
    3
    Hi everyone, english is not my native language so please, be indulgent.

    I am trying to make a game that has Mod support.
    This means that users can create their own assets and their own scripts.

    To create assets they can make an AssetBundle witch is pretty simple to do. To make their own scripts it is pretty hard however.


    The way I see it, you do the scripting part with 2 differents approaches:

    The First one is to have a scripting system as text file and load them inside your game objects like Moonsharp(Lua) or IronPython(Python). Your scripts will probably be imported as text assets inside your assets bundle.

    The Second one is to dynimically load DLL files inside your game and execute a function to enable your custom classes.

    I went with the second approach.


    Everything works fine if my DLL are inside my Asset forlder.

    If I remove the DLL from the asset folder however I have to load them using AddAssemblyToDomain

    Code (CSharp):
    1.      static byte[] loadFile(string filename)
    2.      {
    3.          FileStream fs = new FileStream(filename, FileMode.Open);
    4.          byte[] buffer = new byte[(int)fs.Length];
    5.          fs.Read(buffer, 0, buffer.Length);
    6.          fs.Close();
    7.          return buffer;
    8.      }
    9.      Assembly AddAssemblyToDomain(string AssemblyPath)
    10.      {
    11.          byte[] rawAssembly = loadFile(AssemblyPath);
    12.          return AppDomain.CurrentDomain.Load(rawAssembly);
    13.      }
    When I use AddAssemblyToDomain, the Classes and symbols inside my custom DLL are correcly loaded. I can find classes / functions inside this DLL and execute them correcly:

    Code (CSharp):
    1.  Type Loadertype = Array.Find(DynamicPlugin.GetTypes(), x => x.Name == "CustomPlugin");
    2.          Debug.Log("Type = {" + Loadertype.Name + "}");
    3.          var c = Activator.CreateInstance(Loadertype);
    4.          var method = Loadertype.GetMethod("AlternativeInitialize");
    5.          method.Invoke(c, new object[] { });

    however, when I load a Prefab inside an AssetBundle with a MonoBehavior from that DLL, the monobehaviour component is referencing a missing script for some reason :




    The Weird thing is that I can get add the component manualy to that gameObject.



    I am wondering why my asset bundle cannot get the monobehaviour from the DLL correcly despite the fact that I can dynamically and manually do it myself at runtime.

    I can call AddComponent function but it will be painfull when loading prefabs or entire scenes!

    Source code is provided here: https://github.com/MalisPierre/DynamicDLL

    Scenario explenation provided here: https://github.com/MalisPierre/DynamicDLL/wiki

    Thank you and have a nice day!
     
  2. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,053