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