void AddUsingTest() { string usingString = "using UnityEngine;"; _engine.AddUsings(usingString); //Don't forget about already and permamently referenced executing Assembly Assert.IsTrue(_engine.AllUsings.Count == 1 && _engine.AllUsings.Contains(usingString) && _engine.AllReferencedAssemblies.Count == 2); }
//------------------------------------------------------------------------------------------------- //Compile custom type, and then invoke it's method via CompileCode void CompileType() { CSScriptEngine engine = new CSScriptEngine(); engine.AddUsings("using UnityEngine;"); string typeCode = @" public class SimpleType { public void PrintHey() { Debug.Log(""Hey!""); } } "; engine.CompileType("SimpleType", typeCode); IScript result = engine.CompileCode(@" SimpleType sm = new SimpleType();sm.PrintHey(); "); result.Execute(); }
//------------------------------------------------------------------------------------------------- //Add using of custom namespace, this way you can restrict access of resources to only your custom namespace void AddCustomNamespaceUsings() { CSScriptEngine engine = new CSScriptEngine(); //Here we add using UnityEngine to system, so everything in this namespace is accessible now engine.AddUsings("using SomeCustomNamespace;"); engine.CompileCode("HeyPrinter hp = new HeyPrinter(); hp.PrintHey();").Execute(); }
//------------------------------------------------------------------------------------------------- //Compile simple methodless code void CompileCode() { CSScriptEngine engine = new CSScriptEngine(); engine.AddUsings("using UnityEngine;"); IScript result = engine.CompileCode(@"Debug.Log(""Hey!"");"); result.Execute(); }
//------------------------------------------------------------------------------------------------- //Coroutine compilation is similar to methodless code compilation, but with some nuances. void CompileCoroutine() { CSScriptEngine engine = new CSScriptEngine(); engine.AddUsings("using UnityEngine;"); //CompileCoroutine returns IEnumerable object, so you need to use GetEnumerator on it in order to //be able to pass it to StartCoroutine IEnumerator coroutine = engine.CompileCoroutine(@"yield return new WaitForSeconds(1f);Debug.Log(""Hey!"");").GetEnumerator(); StartCoroutine(coroutine); }
void CompileCodeTest() { //Setup string code = @" GameObject gob = new GameObject(""DynamicallyCreatedGO""); "; _engine.AddUsings("using UnityEngine;"); //Action _engine.CompileCode(code).Execute(); //Assert GameObject go = GameObject.Find("DynamicallyCreatedGO"); Assert.IsTrue(go != null); //TearDown Destroy(go); }
//------------------------------------------------------------------------------------------------- //add using directives to CSScriptEngine, to control what is exposed to dynamic code void AddUsings() { CSScriptEngine engine = new CSScriptEngine(); engine.AddOnCompilationFailedHandler(OnCompilationFailedAction); //There's no usings right now in system, so nothing, apart from basic types is accesible to dynamic code engine.CompileCode("Debug.Log(\"This will result in an error, because Debug is a part of UnityEngine namespace\");"); //Here we add using UnityEngine to system, so everything in this namespace is accessible now engine.AddUsings("using UnityEngine;"); engine.CompileCode("Debug.Log(\"Now compilation of this code will succeed\");").Execute(); engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction); }
//------------------------------------------------------------------------------------------------- //Removes usings from system, so it's namespace resources become inaccessible for dynamic code void RemoveUsings() { CSScriptEngine engine = new CSScriptEngine(); engine.AddOnCompilationFailedHandler(OnCompilationFailedAction); engine.AddUsings("using SomeCustomNamespace;"); engine.CompileCode("HeyPrinter hp = new HeyPrinter(); hp.PrintHey();").Execute(); engine.RemoveUsings("using SomeCustomNamespace;"); //Now this will result in error engine.CompileCode("HeyPrinter hp = new HeyPrinter(); hp.PrintHey();"); engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction); }
//------------------------------------------------------------------------------------------------- //Remove compiled type from system, now this type is inaccessible to dynamic code void RemoveType() { CSScriptEngine engine = new CSScriptEngine(); engine.AddUsings("using UnityEngine;"); engine.AddOnCompilationFailedHandler(OnCompilationFailedAction); string typeCode = @" public class SimpleType { public void PrintHey() { Debug.Log(""Hey!""); } } "; engine.CompileType("SimpleType", typeCode); string anotherTypeCode = @" public class AnotherSimpleType { public void InvokeSimpleTypesPrintHey() { Debug.Log(""Greetings from AnotherSimpleType! Invoking SimpleTypes PrintHey method...""); SimpleType sm = new SimpleType(); sm.PrintHey(); } } "; engine.CompileType("AnotherSimpleType", anotherTypeCode); engine.RemoveTypes("AnotherSimpleType"); //This will cause a compilation error, beacause we removed AnotherSimpleType engine.CompileCode(@" AnotherSimpleType sm = new AnotherSimpleType(); "); engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction); }
//------------------------------------------------------------------------------------------------- //Compile custom type, and then compile another type, using previously compiled type in its code, and then //execute last compiled types method via CompileCode void CompileMultipleTypes() { CSScriptEngine engine = new CSScriptEngine(); engine.AddUsings("using UnityEngine;"); string typeCode = @" public class SimpleType { public void PrintHey() { Debug.Log(""Hey!""); } } "; engine.CompileType("SimpleType", typeCode); string anotherTypeCode = @" public class AnotherSimpleType { public void InvokeSimpleTypesPrintHey() { Debug.Log(""Greetings from AnotherSimpleType! Invoking SimpleTypes PrintHey method...""); SimpleType sm = new SimpleType(); sm.PrintHey(); } } "; engine.CompileType("AnotherSimpleType", anotherTypeCode); IScript result = engine.CompileCode(@" AnotherSimpleType sm = new AnotherSimpleType();sm.InvokeSimpleTypesPrintHey(); "); result.Execute(); }
void Start() { _engine.AddUsings("using WhitelistAPI;"); _engine.AddUsings("using UnityEngine;"); }