dotnow-interpreter icon indicating copy to clipboard operation
dotnow-interpreter copied to clipboard

Loading Assembly at runtime failing with Interpreter

Open Ayfel opened this issue 11 months ago • 1 comments

I am using dotnow together with Roslyn C# Runtime asset and I am trying to load an assembly at runtime. Using ScriptAssembly assembly = domain.LoadAssembly("Path/CustomCode.dll", securityMode); everything works well but when using the interpreted version of the method ScriptAssembly assembly = domain.LoadAssemblyInterpreted("Path/CustomCode.dll", securityMode) then I have missing scripts in my gameobjects. Could you help me understand what is failing here?

Ayfel avatar Mar 19 '24 17:03 Ayfel

dotnow is not fully automatic like that unfortunately and cannot take advantage of Unity serialization. Instead you need to manually add components at runtime from code, something like this:

// Compile the code for use with dotnow
ScriptType mainType = _domain.CompileAndLoadMainSourceInterpreted(someSourceString);

// Create an instance and specify a game object - This will `AddComponent` internally if the type is a mono behaviour
mainType.CreateInstance(gameObject);

If you are a decent coder then it can probably be possible to create something where you can add a script in the editor and it will load the equivalent dotnow scripts at runtime, maybe with serialization too if you are brave enough. For the moment though it is a manual process.

Hope that explains the issue in this case.

scottyboy805 avatar Mar 20 '24 08:03 scottyboy805