Exemplo n.º 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);
    }
Exemplo n.º 2
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);
    }
Exemplo n.º 3
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);
    }
Exemplo n.º 4
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 OnDisable()
        {
            //OnDisable later then EventManager's OnApplicationQuit where It's Instance property gets nullified
            //That causes null reference exception, so let's add this check to prevent that
            if (EventManager.Instance != null)
            {
                _engine.RemoveOnCompilationSucceededHandler(OnCompilationSucceededAction);
                _engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);
                _engineRemote.RemoveOnCompilationSucceededHandler(OnCompilationSucceededAction);
                _engineRemote.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);

                EventManager.Instance.RemoveListener <CompilationEvent>(OnCompileButton);
                EventManager.Instance.RemoveListener <ExecutionEvent>(OnExecuteButton);
                EventManager.Instance.RemoveListener <CompileTypeEvent>(OnCompileAndAddTypeButton);
                EventManager.Instance.RemoveListener <DeleteTypeEvent>(OnDeleteTypeButton);
            }
            if (Remote)
            {
                if (_engineRemote.RemoteDomain != null)
                {
                    _engineRemote.Dispose();
                }
            }
        }
 void OnDisable()
 {
     _engine.RemoveOnCompilationFailedHandler(OnCompilationFailedAction);
     _engine.RemoveOnCompilationSucceededHandler(OnCompilationSucceededAction);
 }