//Remember, you can't compile MonoBehaviours with CSSCriptEngineRemote, stick to //plain C# classes for safety void RemoteCompileType() { CSScriptEngineRemote engineRemote = new CSScriptEngineRemote(); engineRemote.LoadDomain();//Important! engineRemote.AddUsings("using UnityEngine;"); string typeCode = @" public class SimpleType { public void PrintHey() { Debug.Log(""Hey!""); } } "; engineRemote.CompileType("SimpleType", typeCode); engineRemote.CompileCode(@" SimpleType sm = new SimpleType();sm.PrintHey(); "); engineRemote.ExecuteLastCompiledCode(); engineRemote.UnloadDomain();//Important! }
//With CSScriptEngineRemote you must always invoke LoadDomain before using it, //and always invoke Unloaddomain or Dispose when you're done using it void RemoteCompileCode() { //Let's check that no dynamic assemblies were loaded in main appDomain Debug.Log("Assemblies currently loaded in main appdomain: " + AppDomain.CurrentDomain.GetAssemblies().Length); //You can ensure dispose call by utilizing using block, for example using (CSScriptEngineRemote engineRemote = new CSScriptEngineRemote()) { engineRemote.LoadDomain(); engineRemote.AddUsings("using UnityEngine;"); engineRemote.CompileCode(@"Debug.Log(""Hey!"");"); engineRemote.ExecuteLastCompiledCode(); } Debug.Log("Assemblies currently loaded in main appdomain: " + AppDomain.CurrentDomain.GetAssemblies().Length); }
void Awake() { _engine = new CSScriptEngine(); _engineRemote = new CSScriptEngineRemote(); }