Unity Lua Enum

3 篇文章 0 订阅
  1. Unity向Lua传入参数问题
    GameObject go = GameObject.Find("/Light");
            Light light = go.GetComponent<Light>();
            LuaFunction func = state.GetFunction("ChangeLightType");
            func.BeginPCall();
            func.Push(light);
            LightType type = (LightType)(count++ % 4);
            func.Push(type);
            func.PCall();
            func.EndPCall();
            func.Dispose(); //释放

以上是在AccessingEnum中脚本。这边获取一个函数。然后通过func.Push(传入的参数)(多个Push可以多个传入)

2.如下功能可以int 和 enum 比较,可以枚举变成整数比较 ,还可以通过传入参数改变类型(有一个比较注意的点是Lua 里面的枚举输出是:userdata:0x12B09C10和 space = nil 输出的是一样。所以也可以直接比较 代码如下)

public class AccessingEnum : MonoBehaviour 
{
    string script =
        @"
            space = nil

            function TestEnum(e)        
                print('Enum is:'..tostring(e))        

                if space:ToInt() == 0 then
                    print('enum ToInt() is ok')                
                end

                if e==space then
                  print('e  ========================== space')
                end


                if e:ToInt() == 0 then
                    print('enum To Int is OK --------------------------------')
                end   
                if not space:Equals(0) then
                    print('enum compare int is ok')                
                end

                if space == e then
                    print('enum compare enum is ok')
                end

                local s = UnityEngine.Space.IntToEnum(0)
                print ('//',s)
                print ('//',e)
                if space == s then
                    print('IntToEnum change type is ok')
                end
            end

            function ChangeLightType(light, type)
                print('change light type to '..tostring(type))
                light.type = type
            end

            function ChangeMyParameter(a,b)
                  print('Input Parameter A ,B '..a..b)
            end

        ";

    LuaState state = null;

    void Start () 
    {
#if UNITY_5
        Application.logMessageReceived += ShowTips;
#else
        Application.RegisterLogCallback(ShowTips);
#endif
        new LuaResLoader();
        state = new LuaState();
        state.Start();
        LuaBinder.Bind(state);

        state.DoString(script);
        state["space"] = Space.World;

        LuaFunction func = state.GetFunction("TestEnum");
        func.BeginPCall();
        func.Push(Space.World);
        func.PCall();
        func.EndPCall();
        func.Dispose();        
        func = null;
    }
    void OnApplicationQuit()
    {
        state.CheckTop();
        state.Dispose();
        state = null;

#if UNITY_5
        Application.logMessageReceived -= ShowTips;
#else
        Application.RegisterLogCallback(null);
#endif        
    }

    string tips = "";
    int count = 1;

    void ShowTips(string msg, string stackTrace, LogType type)
    {
        tips += msg;
        tips += "\r\n";
    }

    void OnGUI()
    {
        GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 200, 600, 400), tips);

        if (GUI.Button(new Rect(0, 60, 120, 50), "ChangeType"))
        {
            GameObject go = GameObject.Find("/Light");
            Light light = go.GetComponent<Light>();
            LuaFunction func = state.GetFunction("ChangeLightType");
            func.BeginPCall();
            func.Push(light);
            LightType type = (LightType)(count++ % 4);
            func.Push(type);
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }

        if(GUI.Button(new Rect(130,60,120,50), "ChangeMyParameter"))
        {
            LuaFunction func = state.GetFunction("ChangeMyParameter");
            func.BeginPCall();
            func.Push(10);
            func.Push("100");
            func.PCall();
            func.EndPCall();
            func.Dispose();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值