Пример #1
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);
    }
        void OnEnable()
        {
            _engine.AddOnCompilationSucceededHandler(OnCompilationSucceededAction);
            _engine.AddOnCompilationFailedHandler(OnCompilationFailedAction);
            _engineRemote.AddOnCompilationSucceededHandler(OnCompilationSucceededAction);
            _engineRemote.AddOnCompilationFailedHandler(OnCompilationFailedAction);

            EventManager.Instance.AddListener <CompilationEvent>(OnCompileButton);
            EventManager.Instance.AddListener <ExecutionEvent>(OnExecuteButton);
            EventManager.Instance.AddListener <CompileTypeEvent>(OnCompileAndAddTypeButton);
            EventManager.Instance.AddListener <DeleteTypeEvent>(OnDeleteTypeButton);
            EventManager.Instance.AddListener <CurrentlyCompilingAnimationEvent>(OnAnimationToggleValueChanged);
            EventManager.Instance.AddListener <CurrentlyCompilingCodeEvent>(OnCodeToggleValueChanged);
        }
Пример #3
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);
    }
Пример #4
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);
    }
Пример #5
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);
    }
 void OnEnable()
 {
     _engine.AddOnCompilationFailedHandler(OnCompilationFailedAction);
     _engine.AddOnCompilationSucceededHandler(OnCompilationSucceededAction);
 }