Ejemplo n.º 1
0
    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);
            }
        }
    }
Ejemplo n.º 2
0
    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);
            }
        }
    }