scottyboy805 / dotnow-interpreter

A pure C# CIL interpreter designed to load and execute managed code on IL2CPP (Unity) platforms.
MIT License
139 stars 16 forks source link

Loading Assembly at runtime failing with Interpreter #28

Closed Ayfel closed 1 month ago

Ayfel commented 7 months ago

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?

scottyboy805 commented 7 months ago

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.