- 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();
}
}
}