- Home /
Dynamic DLL + AssetBundle prefab missing component error.
AssetBundle cannot load a Monobehavior from DLL Unityccccc[2018.3.0f2]
Hi everyone, this is the first time I ask a question on this forum, 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
static byte[] loadFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
byte[] buffer = new byte[(int)fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
return buffer;
}
Assembly AddAssemblyToDomain(string AssemblyPath)
{
byte[] rawAssembly = loadFile(AssemblyPath);
return AppDomain.CurrentDomain.Load(rawAssembly);
}
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:
Type Loadertype = Array.Find(DynamicPlugin.GetTypes(), x => x.Name == "CustomPlugin");
Debug.Log("Type = {" + Loadertype.Name + "}");
var c = Activator.CreateInstance(Loadertype);
var method = Loadertype.GetMethod("AlternativeInitialize");
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 anice day!
Your answer
Follow this Question
Related Questions
How can I load in assets for a users mod? 0 Answers
Release additional content using AssetBundles? 1 Answer
Loading scene with scripts from asset bundle 1 Answer
How to import the object from server to unity 2 Answers
assets bundle for ios 4 Answers