Example #1
0
    void CompileTypeTest()
    {
        _engine.Reset();

        //Setup
        string typeCode =
            @"
                public class DynamicType
                {
                    public void CreateGameObject(){GameObject gob = new GameObject(""DynamicallyCreatedGO"");}
                }
            ";

        _engine.AddUsings("using UnityEngine;");

        //Action
        _engine.CompileType("TestType", typeCode);
        _engine.CompileCode(@"DynamicType dt = new DynamicType();dt.CreateGameObject();").Execute();

        //Assert
        Type       dynamicType = _engine.CompileCurrentTypesIntoAssembly().GetType("DynamicType");
        GameObject go          = GameObject.Find("DynamicallyCreatedGO");

        Assert.IsTrue(go != null && dynamicType != null);

        //TearDown
        Destroy(go);
    }
Example #2
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 #3
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 #4
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();
    }
        public void OnCompileAndAddTypeButton(CompileTypeEvent ev)
        {
            if (!Remote)
            {
                _engine.CompileType(ev.TypeID, ev.Code);
            }
            else
            {
                _engineRemote.CompileType(ev.TypeID, ev.Code);
            }

            if (_lastCompilationSucceeded)
            {
                EventManager.Instance.QueueEvent(new TypeCompilationSucceededEvent(ev));
            }
        }