예제 #1
0
    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 150, 800, 400), tips);

        if (GUI.Button(new Rect(10, 10, 120, 40), "Error1"))
        {
            tips = "";
            showStack.BeginPCall();
            showStack.Push(go);
            showStack.PCall();
            showStack.EndPCall();
            showStack.Dispose();
            showStack = null;
        }
        else if (GUI.Button(new Rect(10, 60, 120, 40), "Instantiate Error"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("Instantiate");
            func.BeginPCall();
            func.Push(go);
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 110, 120, 40), "Check Error"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("TestRay");
            func.BeginPCall();
            func.PCall();
            func.CheckRay();        //返回值出错
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 160, 120, 40), "Push Error"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("TestRay");
            func.BeginPCall();
            func.Push(Instance);
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 210, 120, 40), "LuaPushError"))
        {
            //需要改lua文件让其出错
            tips = "";
            LuaFunction func = state.GetFunction("PushLuaError");
            func.BeginPCall();
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 260, 120, 40), "Check Error"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("Test5");
            func.BeginPCall();
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 310, 120, 40), "Test Resume"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("Test6");
            func.BeginPCall();
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 360, 120, 40), "out of bound"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("TestOutOfBound");
            func.BeginPCall();
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 410, 120, 40), "TestArgError"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("Test8");
            func.BeginPCall();
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 460, 120, 40), "TestFuncDispose"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("Test8");
            func.Dispose();
            func.BeginPCall();
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 510, 120, 40), "SendMessage"))
        {
            tips = "";
            gameObject.SendMessage("OnSendMsg");
        }
        else if (GUI.Button(new Rect(10, 560, 120, 40), "SendMessageInLua"))
        {
            LuaFunction func = state.GetFunction("SendMsgError");
            func.BeginPCall();
            func.Push(gameObject);
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(10, 610, 120, 40), "AddComponent"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("TestAddComponent");
            func.BeginPCall();
            func.Push(gameObject);
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(210, 10, 120, 40), "TableGetSet"))
        {
            tips = "";
            LuaTable table = state.GetTable("testev");
            int      top   = state.LuaGetTop();

            try
            {
                state.Push(table);
                state.LuaGetField(-1, "Add");
                LuaFunction func = state.CheckLuaFunction(-1);

                if (func != null)
                {
                    func.Call();
                    func.Dispose();
                }

                state.LuaPop(1);
                state.Push(123456);
                state.LuaSetField(-2, "value");
                state.LuaGetField(-1, "value");
                int n = (int)state.LuaCheckNumber(-1);
                Debugger.Log("value is: " + n);

                state.LuaPop(1);

                state.Push("Add");
                state.LuaGetTable(-2);

                func = state.CheckLuaFunction(-1);

                if (func != null)
                {
                    func.Call();
                    func.Dispose();
                }

                state.LuaPop(1);

                state.Push("look");
                state.Push(456789);
                state.LuaSetTable(-3);

                state.LuaGetField(-1, "look");
                n = (int)state.LuaCheckNumber(-1);
                Debugger.Log("look: " + n);
            }
            catch (Exception e)
            {
                state.LuaSetTop(top);
                throw e;
            }

            state.LuaSetTop(top);
        }
        else if (GUI.Button(new Rect(210, 60, 120, 40), "TestTableInCo"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("TestCoTable");
            func.BeginPCall();
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(210, 110, 120, 40), "Instantiate2 Error"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("Instantiate");
            func.BeginPCall();
            func.Push(go2);
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
        else if (GUI.Button(new Rect(210, 160, 120, 40), "Instantiate3 Error"))
        {
            tips = "";
            UnityEngine.Object.Instantiate(go2);
        }
        else if (GUI.Button(new Rect(210, 210, 120, 40), "TestCycle"))
        {
            tips = "";
            int         n    = 20;
            LuaFunction func = state.GetFunction("TestCycle");
            func.BeginPCall();
            func.Push(n);
            func.PCall();
            int c = (int)func.CheckNumber();
            func.EndPCall();

            Debugger.Log("Fib({0}) is {1}", n, c);
        }
        else if (GUI.Button(new Rect(210, 260, 120, 40), "TestNull"))
        {
            tips = "";
            Action action = () =>
            {
                LuaFunction func = state.GetFunction("TestNull");
                func.BeginPCall();
                func.PushObject(null);
                func.PCall();
                func.EndPCall();
            };

            StartCoroutine(TestCo(action));
        }
        else if (GUI.Button(new Rect(210, 310, 120, 40), "TestMulti"))
        {
            tips = "";
            LuaFunction func = state.GetFunction("TestMulStack");
            func.BeginPCall();
            func.PushObject(null);
            func.PCall();
            func.EndPCall();
        }
    }