//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! }
void CompileCodeTest() { //Setup string code = @" GameObject gob = new GameObject(""DynamicallyCreatedGO""); "; _engine.AddUsings("using UnityEngine;"); //Action _engine.CompileCode(code); _engine.ExecuteLastCompiledCode(); //Assert GameObject go = GameObject.Find("DynamicallyCreatedGO"); Assert.IsTrue(go != null); //TearDown Destroy(go); }
//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); }
public void OnCompileButton(CompilationEvent cmpEv) { if (!Remote) { if (_currentlyCompilingCode) { _lastCompiledScript = _engine.CompileCode(cmpEv.Code); } else if (_currentlyCompilingCoroutine) { _lastCompiledCoroutine = _engine.CompileCoroutine(cmpEv.Code); } } else { if (_currentlyCompilingCode) { _engineRemote.CompileCode(cmpEv.Code); } } }