///// <summary> ///// 获取变量 ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="param"></param> ///// <returns></returns> //public T GetValue<T>(string param) //{ // var top = state_.state.GetTop(); // obj_.Push(); // // var L = state_.state; // L.GetField(-1, param); // top = state_.state.GetTop(); // if(L.IsNil(-1)) // { // L.Pop(2); // top = state_.state.GetTop(); // return default(T); // } // else // { // var retn = Lua.State.GetObject<T>(L, -1); // L.Pop(2); // top = state_.state.GetTop(); // return retn; // } //} ///// <summary> ///// 获取变量 ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="param"></param> ///// <returns></returns> //public object GetValue(string param) //{ // var top = state_.state.GetTop(); // obj_.Push(); // // var L = state_.state; // L.GetField(-1, param); // top = state_.state.GetTop(); // if(L.IsNil(-1)) // { // L.Pop(2); // top = state_.state.GetTop(); // return null; // } // else // { // var retn = Lua.State.GetOneobject(L, -1); // L.Pop(2); // top = state_.state.GetTop(); // return retn; // } //} private void DoFunction(Lua.Function func) { if (func == null) { return; } func.Start(); if (!func.Call()) { LuaMono.LogError(func.last_error_string); } func.ClearCall(); }
/// <summary> /// 调用Lua函数 /// </summary> /// <typeparam name="T">返回类型</typeparam> /// <param name="methodname">函数名</param> /// <param name="ps">参数数组</param> /// <returns>返回,函数异常时返回default(T)</returns> public T Call <T>(string methodname, params object[] ps) { var retn = default(T); var dict = GetFunctionDict(); if (dict == null) { return(retn); } Lua.Function func = null; dict.TryGetValue(methodname, out func); if (func == null) { LuaMono.Log("'" + methodname + "' is not found in '" + gameObject.name + "'"); return(retn); } func.Start(); if (ps != null && ps.Length > 0) { int length = ps.Length; for (var i = 0; i < length; ++i) { func.Push(ps[i]); } } if (!func.Call()) { LuaMono.LogError(func.last_error_string); } if (func.GetReturnCount() > 0 && typeof(T) != typeof(DontCareReturn)) { retn = func.GetObject <T>(1); } func.ClearCall(); return(retn); }
///// <summary> ///// 获取变量 ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="param"></param> ///// <returns></returns> //public T GetValue<T>(string param) //{ // var top = state_.state.GetTop(); // obj_.Push(); // // var L = state_.state; // L.GetField(-1, param); // top = state_.state.GetTop(); // if(L.IsNil(-1)) // { // L.Pop(2); // top = state_.state.GetTop(); // return default(T); // } // else // { // var retn = Lua.State.GetObject<T>(L, -1); // L.Pop(2); // top = state_.state.GetTop(); // return retn; // } //} ///// <summary> ///// 获取变量 ///// </summary> ///// <typeparam name="T"></typeparam> ///// <param name="param"></param> ///// <returns></returns> //public object GetValue(string param) //{ // var top = state_.state.GetTop(); // obj_.Push(); // // var L = state_.state; // L.GetField(-1, param); // top = state_.state.GetTop(); // if(L.IsNil(-1)) // { // L.Pop(2); // top = state_.state.GetTop(); // return null; // } // else // { // var retn = Lua.State.GetOneobject(L, -1); // L.Pop(2); // top = state_.state.GetTop(); // return retn; // } //} private void DoFunction(Lua.Function func) { if (func == null || error_pause) { return; } try { func.Start(); if (!func.Call()) { LuaMono.LogError(func.last_error_string); OnError(func.last_error_string, false); } func.ClearCall(); } catch (System.Exception e) { var err = e.ToString(); OnError(err, true); Debug.LogError(err); } }
void Prepare() { // 状态机检查 if (state_ != null) { return; } var script = Lua.Resource.Load( string.Concat(Lua.Singleton.kStartLuaScript, gameObject.name)); if (!string.IsNullOrEmpty(script)) { state_ = Lua.Singleton.newthread(); state_.register("require", param => { var state = param.state; var top = state.GetTop(); var name = param.GetString(1); var s = Lua.Resource.Load(name); if (string.IsNullOrEmpty(s)) { return; } if (state_.dostring(s, name) != Lua.Error.LUA_OK) { OnError(state_.last_error_string, false); Debug.LogError(state_.last_error_string); return; } var d = state.GetTop() - top; List <Lua.Ref> refs = null; if (d > 0) { refs = new List <Lua.Ref>(); for (var i = 0; i < d; ++i) { refs.Add(new Lua.Ref(state)); } } state.Pop(1); if (refs != null) { for (var i = 0; i < refs.Count; ++i) { refs[i].Push(); } } param.ReturnCount = d; }); state_.register("include", Include); state_.register("using", Include); state_.dostring( kLuaScript[0] + script + kLuaScript[1], gameObject.name + ".lua"); if (!state_.state.IsNil(-1)) { using (var func = new Lua.Function(state_.state)) { func.Start(); if (!func.Call()) { LuaMono.LogError(func.last_error_string); func.ClearCall(); enabled = false; return; } if (func.GetReturnCount() == 0) { Debug.Log("LuaMono Not Return"); } else { obj_ = func.GetRef(1); } func.ClearCall(); func.Dispose(); } var dict = GetFunctionDict(); dict.TryGetValue("Awake", out awake_); dict.TryGetValue("Start", out start_); dict.TryGetValue("Update", out update_); dict.TryGetValue("FixedUpdate", out fixed_update_); dict.TryGetValue("LateUpdate", out late_update_); dict.TryGetValue("OnGUI", out on_gui_); dict.TryGetValue("OnEnable", out on_enable_); dict.TryGetValue("OnDisable", out on_disable_); dict.TryGetValue("OnDestroy", out on_destroy_); dict.TryGetValue("UpdateCoroutine", out update_coroutine_); CallWithoutReturn("SetGameObject", gameObject); CallWithoutReturn("SetMonoBehaviour", this); } } }
void Prepare() { // 状态机检查 if (state_ != null) { return; } var script = Lua.Resource.Load(gameObject.name); if (!string.IsNullOrEmpty(script)) { state_ = Lua.Singleton.newthread(); state_.dostring( kLuaScript[0] + script + kLuaScript[1], gameObject.name + ".lua"); if (!state_.state.IsNil(-1)) { using (var func = new Lua.Function(state_.state)) { func.Start(); if (!func.Call()) { LuaMono.LogError(func.last_error_string); func.ClearCall(); enabled = false; return; } if (func.GetReturnCount() == 0) { Debug.Log("LuaMono Not Return"); } else { obj_ = func.GetRef(1); } func.ClearCall(); func.Dispose(); } var dict = GetFunctionDict(); dict.TryGetValue("Awake", out awake_); dict.TryGetValue("Start", out start_); dict.TryGetValue("Update", out update_); dict.TryGetValue("FixedUpdate", out fixed_update_); dict.TryGetValue("LateUpdate", out late_update_); dict.TryGetValue("OnGUI", out on_gui_); dict.TryGetValue("OnEnable", out on_enable_); dict.TryGetValue("OnDisable", out on_disable_); dict.TryGetValue("OnDestroy", out on_destroy_); dict.TryGetValue("UpdateCoroutine", out update_coroutine_); CallWithoutReturn("SetGameObject", gameObject); CallWithoutReturn("SetMonoBehaviour", this); } } }