Bipin
Bipin

Reputation: 451

Is there any solution for runtime code compilation in Unity3D for IL2Cpp builds?

I'm trying to compile C# code at runtime in Unity3D for IL2CPP build 64 bit Android.

I have a string of code that I want to compile and execute dynamically. The things that I've tried are:

  1. https://github.com/aeroson/mcs-ICodeCompiler
  2. https://github.com/SoapCode/UCompile/tree/master/Assets/UCompile
  3. https://assetstore.unity.com/packages/tools/integration/roslyn-c-runtime-compiler-142753#reviews

But what I got to understand is that all of the above doesn't work with IL2Cpp builds.

The below code is carried out using: Second Github repo mentioned above.

CSScriptEngine engine = new CSScriptEngine();
engine.AddUsings("using UnityEngine; using System; using System.Collections.Generic;");
engine.AddOnCompilationFailedHandler(OnCompilationFail);
IScript result = engine.CompileCode(@codeToConvert);

With second plugin:

AndroidPlayer([email protected]:34999) ArgumentException: Invalid path
  at System.IO.Path.GetDirectoryName (System.String path) [0x00000] in <00000000000000000000000000000000>:0 
  at Mono.CSharp.DynamicLoader..ctor (Mono.CSharp.ReflectionImporter importer, Mono.CSharp.CompilerContext compiler) [0x00000] in <00000000000000000000000000000000>:0 
  at Mono.CSharp.Evaluator.Init () [0x00000] in <00000000000000000000000000000000>:0 
  at Mono.CSharp.Evaluator.Compile (System.String input, Mono.CSharp.CompiledMethod& compiled) [0x00000] in <00000000000000000000000000000000>:0 
  at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) [0x00000] in <00000000000000000000000000000000>:0 
  at Mono.CSharp.Evaluator.Run (System.String statement) [0x00000] in <00000000000000000000000000000000>:0 
  at UCompile.MonoEvaluator.Run (System.String scriptText) [0x00000] in <00000000000000000000000000000000>:0 
  at UCompile.CompilationUnit.Run (System.String code) [0x00000] in <00000000000000000000000000000000>:0 
  at UCompile.CSScriptEngine.AddUsings (System.String usings) [0x00000] in <00000000000000000000000000000000>:0 
  at CheckVersion.Operation_completed (UnityEngine.AsyncOperation obj) [0x00000] in <00000000000000000000000000000000>:0 
  at System.Action`1[T].Invoke (T obj) [0x00000] in <00000000000000000000000000000000>:0 
  at UnityEngine.AsyncOperation.InvokeCompletionEvent () [0x00000] in <00000000000000000000000000000000>:0

Upvotes: 3

Views: 1223

Answers (1)

dylanh724
dylanh724

Reputation: 1018

You cannot use dynamic keyword with IL2CPP - something about the way IL2CPP works. From what I hear, there will likely never be support for this with IL2CPP.

Cast to an explicit object and use that, instead, perhaps.

More here: https://forum.unity.com/threads/dynamic-type-in-il2cpp.934338/

Upvotes: 0

Related Questions