ToLua学习笔记(十) Example 10 Enum
转载请注明出处四川包邮
这一篇文章主要是通过Example讲解ToLua中枚举的用法
代码
using UnityEngine;
using System;
using LuaInterface;
// 枚举
public class AccessingEnum : MonoBehaviour
{
string script =
@"
space = nil
function TestEnum(e)
print('Enum is:'..tostring(e))
--获取Int值
if space:ToInt() == 0 then
print('enum ToInt() is ok')
end
--这里原来例子有一点误导
--if not space:Equals(0) then
if not space:Equals(UnityEngine.Space.World) 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)
if space == s then
print('IntToEnum change type is ok')
end
end
--切换光源类型,把原来的点光换方向光
function ChangeLightType(light, type)
print('change light type to Directional')
light.type = UnityEngine.LightType.Directional
end
";
void Start ()
{
LuaState state = new LuaState();
state.Start();
LuaBinder.Bind(state);
state.DoString(script);
// Unity自己的枚举赋值给Lua中
state["space"] = Space.World;
LuaFunction func = state.GetFunction("TestEnum");
func.BeginPCall();
func.Push(Space.World);
func.PCall();
func.EndPCall();
func.Dispose();
func = null;
GameObject go = GameObject.Find("/Light");
Light light = go.GetComponent<Light>();
func = state.GetFunction("ChangeLightType");
func.BeginPCall();
func.Push(light);
func.Push(LightType.Directional);
func.PCall();
func.EndPCall();
func.Dispose();
func = null;
state.CheckTop();
state.Dispose();
state = null;
}
}
lua中一些用法
space:ToInt()
获取枚举的int值
if not space:Equals(0) then的问题
这是原来例子中的
但是下面的输出print('enum compare int is ok')
但是实际上这里和Equals(int)是无法直接和int比较得到结果的
我这里修改成了if not space:Equals(UnityEngine.Space.World) then
UnityEngine.Space.IntToEnum(0)
Int转枚举