Example #1
0
    //-------------------------------------------------------------------------------------------------

    //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();
    }
Example #2
0
    //-------------------------------------------------------------------------------------------------

    //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();
    }
Example #3
0
    //-------------------------------------------------------------------------------------------------

    //Compile simple methodless code
    void CompileCode()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddUsings("using UnityEngine;");

        IScript result = engine.CompileCode(@"Debug.Log(""Hey!"");");

        result.Execute();
    }
Example #4
0
    //-------------------------------------------------------------------------------------------------

    //Here we determine which method is going to automatically be invoked when compilation ends without errors
    void AddCompilationSucceededHandler()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddOnCompilationSucceededHandler(OnCompilationSucceededAction);

        engine.CompileCode("string warningCauser = \"This will result in warning, but compillation succeeds\";");

        engine.RemoveOnCompilationSucceededHandler(OnCompilationSucceededAction);
    }
Example #5
0
    //-------------------------------------------------------------------------------------------------

    //Here we determine which method is going to automatically be invoked when compilation ends with errors
    void AddCompilationFailedHandler()
    {
        CSScriptEngine engine = new CSScriptEngine();

        engine.AddOnCompilationFailedHandler(OnCompilationFailedAction);

        engine.CompileCode("This will result in error");

        engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);
    }
Example #6
0
    //-------------------------------------------------------------------------------------------------

    //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);
    }
Example #7
0
    //-------------------------------------------------------------------------------------------------

    //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);
    }
Example #8
0
    //-------------------------------------------------------------------------------------------------

    //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);
    }
Example #9
0
    //-------------------------------------------------------------------------------------------------

    //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);
    }
Example #10
0
    //-------------------------------------------------------------------------------------------------

    //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 Awake()
 {
     _engine       = new CSScriptEngine();
     _engineRemote = new CSScriptEngineRemote();
 }
 // Use this for initialization
 void Awake()
 {
     _engine = new CSScriptEngine();
 }