Exemple #1
0
    void Awake()
    {
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);



        luaEnv.DoString(luaScript.text, "Test03", scriptEnv);


        scriptEnv.Set("accountInput", accountInput);
        scriptEnv.Set("passwordInput", passwordInput);
        scriptEnv.Set("loginBtn", loginBtn);

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Exemple #2
0
    private LuaTable scriptEnv;                   // Use this for initialization
    void Start()
    {
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();
        luaEnv.AddLoader((ref string filepath) =>
        {
            filepath = Application.dataPath + "/3RD/AutofaceTest/Examples/3.0_XluaLoading/lua/" + filepath.Replace('.', '/') + ".lua";
            if (File.Exists(filepath))
            {
                return(File.ReadAllBytes(filepath));
            }
            else
            {
                return(null);
            }
        });

        luaEnv.DoString(@"
            require 'TestLua1'
        ");

        Action Dofunc = scriptEnv.Get <Action>("dofunc1");

        if (Dofunc != null)
        {
            Dofunc();
        }
    }
Exemple #3
0
    /// <summary>
    /// 加载lua脚本
    /// </summary>
    void LoadLuaScript()
    {
        if (!isLoadedLuaScript)
        {
            if (string.IsNullOrEmpty(ScriptName))
            {
                return;
            }
            byte[] luaContent = LuaManager.Instance.LoadCustomLuaFile(ScriptName);
            LuaEnv luaEnv     = LuaManager.LuaEnv;
            scriptEnv = luaEnv.NewTable();
            LuaTable meta = luaEnv.NewTable();
            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("this", this);
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }

            luaEnv.DoString(Utility.BytesToUTF8String(luaContent), ScriptName, scriptEnv);
            isLoadedLuaScript = true;
        }
    }
Exemple #4
0
    void Awake()
    {
        scriptEnv = luaEnv.NewTable();

        // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        foreach (var injection in injections)
        {
            scriptEnv.Set(injection.name, injection.value);
        }

        luaEnv.DoString(luaScript.text, env: scriptEnv);

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);
        scriptEnv.Get("fixedupdate", out luaFixedUpdate);
        scriptEnv.Get("lateupdate", out luaLateUpdate);
        scriptEnv.Get("ondisable", out luaOnDisable);

        luaAwake?.Invoke();
    }
Exemple #5
0
    public LuaTable GetScriptEnv(MonoBehaviour view, string luaScript, out string monoName)
    {
        if (string.IsNullOrEmpty(luaScript))
        {
            monoName = string.Empty;
            return(null);
        }
        var      _scriptEnv = luaEnv.NewTable();
        LuaTable meta       = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        _scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        _scriptEnv.Set("mono", view);
        _scriptEnv.Set("gameObject", view.gameObject);
        _scriptEnv.Set("transform", view.transform);

        monoName = Path.GetFileNameWithoutExtension(luaScript);

        _watch.Restart();
        var luaAsset = CustomLoader(ref luaScript);

        _watch.Stop();
        Logger.Log(string.Format("[lua]{0} LoadLuaSync:{1}ms", monoName, _watch.ElapsedMS()));

        if (luaAsset == null)
        {
            Logger.LogError(string.Format("lua not found:{0}", luaScript));
            return(null);
        }
        luaEnv.DoString(luaAsset, monoName, _scriptEnv); //返回luatable
        return(_scriptEnv);
    }
        private void Awake()
        {
            //得到lua的环境
            luaEnv = LuaHelper.GetInstance().GetLuaEnv();
            /*  设置luaTable 的元方法 (“__index”)  */
            luaTable = luaEnv.NewTable();
            LuaTable tmpTab = luaEnv.NewTable();//临时表
            tmpTab.Set("__index", luaEnv.Global);
            luaTable.SetMetaTable(tmpTab);
            tmpTab.Dispose();
            /* 得到当前脚本所在对象的预设名称,且去除后缀(["(Clone)"]) */
            string prefabName = this.name;  //当前脚本所挂载的游戏对象的名称
            if (prefabName.Contains("(Clone)"))
            {
                prefabName = prefabName.Split(new string[] { "(Clone)" }, StringSplitOptions.RemoveEmptyEntries)[0];
            }
            /* 查找指定路径下lua文件中的方法,映射为委托 */
            luaAwake = luaTable.GetInPath<BaseLuaUIForm.delLuaAwake>(prefabName + ".Awake");
            luaStart = luaTable.GetInPath<BaseLuaUIForm.delLuaStart>(prefabName + ".Start");
            luaUpdate = luaTable.GetInPath<BaseLuaUIForm.delLuaUpdate>(prefabName + ".Update");
            luaDestroy = luaTable.GetInPath<BaseLuaUIForm.delLuaDestroy>(prefabName + ".OnDestroy");

            //调用委托
            if (luaAwake != null)
            {
                luaAwake(gameObject);
            }

        }
Exemple #7
0
    void InitLua(TextAsset luaStr)
    {
        joystick = GameObject.Find("Joystick").GetComponent <Joystick>();
        UiRoot   = GameObject.Find("UIRoot").transform;

        scriptEnv = luaEnv.NewTable();

        // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        scriptEnv.Set("UiRoot", UiRoot);

        luaEnv.DoString(luaStr.text, "GameManger", scriptEnv);

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("update", out _luaUpdate);
        scriptEnv.Get("ondestroy", out _luaOnDestroy);

        scriptEnv.Get("TurnUp", out _turnUp);
        scriptEnv.Get("TurnDown", out _turnDown);
        scriptEnv.Get("TurnLeft", out _turnLeft);
        scriptEnv.Get("TurnRight", out _turnRight);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Exemple #8
0
        void Awake()
        {
            luaTSrc = luaEnv.NewTable();
            CustomLoader.AddLoader(luaEnv);
            luaEnv.DoString("require('Utils.Global')");

            // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
            LuaTable meta = luaEnv.NewTable();

            meta.Set("__index", luaEnv.Global);
            luaTSrc.SetMetaTable(meta);
            meta.Dispose();

            luaTSrc.Set("self", this);
            foreach (var injection in injections)
            {
                luaTSrc.Set(injection.name, injection.value);
            }

            luaEnv.DoString(m_srcLua.text, m_srcLua.name, luaTSrc);
            Action luaAwake = luaTSrc.Get <Action>("Awake");

            luaTSrc.Get("Start", out luaStart);
            luaTSrc.Get("Enable", out luaEnable);
            luaTSrc.Get("Diable", out luaDisable);
            luaTSrc.Get("Update", out luaUpdate);
            luaTSrc.Get("OnAppPause", out luaOnAppPause);
            luaTSrc.Get("OnDestroy", out luaOnDestroy);

            luaAwake?.Invoke();
        }
Exemple #9
0
        private void Awake()
        {
            luaEnv    = LuaManager.LuaEnv;
            scriptEnv = luaEnv.NewTable();
            var meta = luaEnv.NewTable();

            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("self", this);
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }

            var prefabName = name;

            if (prefabName.Contains("(Clone)"))
            {
                prefabName = prefabName.Split(new[] { "(Clone)" }, StringSplitOptions.RemoveEmptyEntries)[0];
            }

            prefabName = prefabName.Replace("Panel", "");

            luaAwake     = scriptEnv.GetInPath <delLuaAwake>(prefabName + ".awake");
            luaStart     = scriptEnv.GetInPath <delLuaStart>(prefabName + ".start");
            luaUpdate    = scriptEnv.GetInPath <delLuaUpdate>(prefabName + ".update");
            luaOnDestroy = scriptEnv.GetInPath <delLuaOnDestroy>(prefabName + ".ondestroy");

            _myClass = luaEnv.Global.Get <MyClass>("MyClass");
            Debug.Log(_myClass == null);
            luaAwake?.Invoke(gameObject);
        }
Exemple #10
0
        /// <summary>
        /// Inits the gamelogic.
        /// </summary>
        private void InitGamelogic()
        {
            scriptEnv = luaEnv.NewTable();

            LuaTable meta = luaEnv.NewTable();

            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("self", this);

            luaEnv.DoString("require 'Lua/LuaMain'", "LuaBehaviour", scriptEnv);

            Action luaAwake = scriptEnv.Get <Action>("awake");

            scriptEnv.Get("start", out luaStart);
            scriptEnv.Get("update", out luaUpdate);
            scriptEnv.Get("ondestroy", out luaOnDestroy);

            if (luaAwake != null)
            {
                luaAwake();
            }
        }
Exemple #11
0
    void Awake()
    {
        luaEnv = LuaManager.luaEnv; //此处要从LuaManager上获取 全局只有一个

        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        string prefabName = name;

        if (prefabName.Contains("(Clone)"))
        {
            prefabName = prefabName.Split(new string[] { "(Clone)" }, StringSplitOptions.RemoveEmptyEntries)[0];
        }

        prefabName = prefabName.Replace("pan_", "");

        luaAwake     = scriptEnv.GetInPath <LuaBehaviour.delLuaAwake>(prefabName + ".awake");
        luaStart     = scriptEnv.GetInPath <LuaBehaviour.delLuaStart>(prefabName + ".start");
        luaUpdate    = scriptEnv.GetInPath <LuaBehaviour.delLuaUpdate>(prefabName + ".update");
        luaOnDestroy = scriptEnv.GetInPath <LuaBehaviour.delLuaOnDestroy>(prefabName + ".ondestroy");

        scriptEnv.Set("self", this);
        if (luaAwake != null)
        {
            luaAwake(gameObject);
        }
    }
Exemple #12
0
    private void Awake()
    {
        GApp.UIMgr.RegisterGameObject(gameObject);

        luaenv    = GApp.LuaMgr.GetLuaEnv();
        scriptEnv = luaenv.NewTable();
        LuaTable meta = luaenv.NewTable();

        meta.Set("__index", luaenv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        var goName = name.Contains("(Clone)") ? name.Replace("(Clone)", "") : name;

        luaAwake     = scriptEnv.GetInPath <Action <GameObject> >(goName + ".awake");
        luaStart     = scriptEnv.GetInPath <Action>(goName + ".start");
        luaUpdate    = scriptEnv.GetInPath <Action>(goName + ".update");
        luaOnDestroy = scriptEnv.GetInPath <Action>(goName + ".ondestory");
        luaOpen      = scriptEnv.GetInPath <Action <LuaTable> >(goName + ".open");
        luaClose     = scriptEnv.GetInPath <Action>(goName + ".close");

        if (luaAwake != null)
        {
            luaAwake(gameObject);
        }
        else
        {
            print("luaAwake is null");
        }
    }
Exemple #13
0
    void OnEnable()
    {
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        if (injections != null)
        {
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }
        }

        luaEnv.DoString(luaScript, "LuaBehaviour", scriptEnv);

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Exemple #14
0
    private void Awake()
    {
        luaEnv    = KELua.Instance;
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        foreach (var injection in injections)
        {
            scriptEnv.Set(injection.name, injection.value);
        }

        luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);

        Action luaAwake = scriptEnv.Get <Action>("Awake");

        scriptEnv.Get("Start", out luaStart);
        scriptEnv.Get("Update", out luaUpdate);
        scriptEnv.Get("OnDestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Exemple #15
0
    public LuaTable Inject(LuaMonoBehaviour lb)
    {
        LuaTable luaTable = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        luaTable.SetMetaTable(meta);
        meta.Dispose();

        luaTable.Set("mono", lb);
        if (lb.Injections != null && lb.Injections.Count > 0)
        {
            foreach (var injection in lb.Injections.Where(i => i.obj != null))
            {
                luaTable.Set(injection.name, injection.obj);
            }
        }
        if (lb.InjectValues != null && lb.InjectValues.Count > 0)
        {
            foreach (var injection in lb.InjectValues.Where(i => i.v != null))
            {
                luaTable.Set(injection.k, injection.v);
            }
        }
        return(luaTable);
    }
        public void Awake()
        {
            _cubeScriptsAsset = Resources.Load <TextAsset>("LuaScripts/TestLuaScript.Lua");

            _scriptEve = _luaEnv.NewTable();

            LuaTable meta = _luaEnv.NewTable();

            meta.Set("__index", _luaEnv.Global);
            _scriptEve.SetMetaTable(meta);
            meta.Dispose();

            _scriptEve.Set("Self", this);
            foreach (var injection in _injections)
            {
                _scriptEve.Set(injection, injection.Value);
            }

            _luaEnv.DoString(_cubeScriptsAsset.text, "TestLuaScript", _scriptEve);

            Action luaAwake = _scriptEve.Get <Action>("awake");

            _scriptEve.Get("start", out _luaStart);
            _scriptEve.Get("update", out _luaUpDate);
            _scriptEve.Get("ondestroy", out _luaOnDestroy);
            if (luaAwake != null)
            {
                luaAwake.Invoke();
            }
        }
    void Awake()
    {
        Debug.Log(AssetDatabase.GetAssetPath(luaScript));

        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        foreach (var injection in injections)
        {
            scriptEnv.Set(injection.name, injection.value);
        }

        // luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);
        luaEnv.DoString(luaScript.text, luaScript.name, scriptEnv); //适配 luaide 调试,但要小心 lua 文件重名

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
    void Awake()
    {
        LuaEnv luaEnv = LuaScriptManager.Instance.GetLuaEnv();

        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        foreach (var injection in injections)
        {
            scriptEnv.Set(injection.name, injection.value);
        }

        string luaScirpts = LuaScriptManager.Instance.GetLuaScriptText(luaScriptPath);

        luaEnv.DoString(luaScirpts, "LuaBehaviour", scriptEnv);

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("fixedUpdate", out luaFixedUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
        void Awake()
        {
            //获取LuaTable,要用luaEnv.NewTable(),这里会做一些排序,共用虚拟机等操作
            scriptEnv = luaEnv.NewTable();
            LuaTable meta = luaEnv.NewTable();

            //key,value的方式,虚拟机环境对应的key值一定要是这个“__index”,
            //在xlua的底层,获取LuaTable所属虚拟机的环境是get的时候,用的key是这个名字,所以不能改
            meta.Set("__index", luaEnv.Global);
            //将有虚拟机和全局环境的table绑定成他自己的metatable
            scriptEnv.SetMetaTable(meta);
            //值已经传递过去了,就释放他
            meta.Dispose();
            //这里的"self"和上面的"__index"是一个道理啦。将c#脚本绑定到LuaTable
            scriptEnv.Set("self", this);
            //在腾讯示例中,Hierarchy面板赋值的,在这里应该是约定名字和子物体的对应,绑定到luatable
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }
            //执行lua语句,三个参数的意思分别是lua代码,lua代码在c#里的代号,lua代码在lua虚拟机里的代号
            luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);
            //xlua搞了这么久,也就是为了最后这几个锤子,c#调用lua里的方法。
            //总结起来一句话,通过luatable这个类来完成c#调用Lua
            //怎样完成这一步呢?就是获取luatable对象,配置lua虚拟机,配置虚拟机环境,绑定c#代码,最后执行lua语句
            Action luaAwake = scriptEnv.Get <Action>("awake");

            scriptEnv.Get("start", out luaStart);
            scriptEnv.Get("update", out luaUpdate);
            scriptEnv.Get("ondestroy", out luaOnDestroy);
            if (luaAwake != null)
            {
                luaAwake();
            }
        }
Exemple #20
0
        void Awake()
        {
            scriptEnv = luaEnv.NewTable();

            // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
            LuaTable meta = luaEnv.NewTable();

            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("self", this);

            luaEnv.DoString(luaScript.text, "TestProfiler", scriptEnv);

            //Register Actions
            Action luaAwake = scriptEnv.Get <Action>("awake");

            scriptEnv.Get("report", out luaTakeSnap);

            if (luaAwake != null)
            {
                luaAwake();
            }
        }
Exemple #21
0
    void main()
    {
        if (!Directory.Exists(CSTools.GetPlatformPath()))
        {
            Directory.CreateDirectory(CSTools.GetPlatformPath());
            FileStream   fs   = new FileStream(CSTools.GetPlatformPath() + "/Main.lua", FileMode.Create);
            StreamWriter sw   = new StreamWriter(fs);
            string       text = "function main() print('hello LuaShow!') end";
            sw.Write(text);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
        LuaEnv luaEnv = new LuaEnv();

        CSTools.Instance.LoadText("Main.lua", (luaScript) => {
            LuaTable scriptEnv = luaEnv.NewTable();
            LuaTable meta      = luaEnv.NewTable();
            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();
            scriptEnv.Set("self", this);
            luaEnv.DoString(luaScript, "Main.lua", scriptEnv);
            Action luaMain = scriptEnv.Get <Action> ("main");
            luaMain();
        });
    }
Exemple #22
0
    void Start()
    {
        Application.logMessageReceived += this.log;

        luaEnv    = new LuaEnv();
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);

        scriptEnv.Get("GC", out mGC);
        scriptEnv.Get("JitSwitch", out mJitSwitch);
        scriptEnv.Get("Version", out mVersion);

        runCount = 1;

        testItems = new TestItem[17];
        for (int i = 0; i <= 10; ++i)
        {
            testItems[i] = new TestLua(this, i, transform);
        }
        testItems[11] = new TestEmptyFunc(this, 11);
        testItems[12] = new TestGetLuaValue(this, 12, "_V0");
        testItems[13] = new TestGetLuaValue(this, 13, "_V1");
        testItems[14] = new TestGetLuaValue(this, 14, "_V2");
        testItems[15] = new TestGetLuaValue(this, 15, "_V3");
        testItems[16] = new TestGetLuaValue(this, 16, "_V4");
    }
Exemple #23
0
        private void Awake()
        {
            Load("PackManager.lua", "luasprite.v1");
            luaEnv    = new LuaEnv();
            scriptEnv = luaEnv.NewTable();
            LuaTable metaTable = luaEnv.NewTable();

            //将lua全局环境当做表metaTable的元方法
            metaTable.Set("__index", luaEnv.Global);
            //将表metaTable设为scriptEnv的元表
            scriptEnv.SetMetaTable(metaTable);
            scriptEnv.Set("self", this);

            foreach (Injection obj in injections)
            {
                scriptEnv.Set(obj.name, obj.value);
            }

            if (luaText != string.Empty)
            {
                //执行上次热更后的版本
                luaEnv.DoString(luaText, "PackManager.Lua", scriptEnv);
            }
            else
            {
                //执行打包时的Lua脚本
                luaEnv.DoString(luaScriptFile.text, "PackManager.Lua", scriptEnv);
            }
            //将Lua中的start方法映射到当前start中
            start = scriptEnv.Get <Action>("start");
            //将Lua中的start方法映射到当前start中
            scriptEnv.Get("update", out update);
        }
    void Awake()
    {
        scriptEnv = luaEnv.NewTable();

        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        //foreach (var injection in injections)
        //{
        //    scriptEnv.Set(injection.name, injection.value);
        //}
        luaScript = Resources.Load <TextAsset>("GameManager.lua");
        luaEnv.DoString(luaScript.text, "", scriptEnv);
        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Exemple #25
0
        void Awake()
        {
            Debug.Log("awake");
            scriptEnv = luaEnv.NewTable();

            LuaTable meta = luaEnv.NewTable();

            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("self", this);
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }

            luaEnv.DoString(luaScript.text, "LuaTestScirpt", scriptEnv);

            Action luaAwake = scriptEnv.Get <Action>("awake");

            scriptEnv.Get("start", out luaStart);
            scriptEnv.Get("update", out luaUpdate);
            scriptEnv.Get("ondestory", out luaOnDesctory);

            if (luaAwake != null)
            {
                luaAwake();
            }
        }
        void Awake()
        {
            scriptEnv = luaEnv.NewTable();

            // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
            LuaTable meta = luaEnv.NewTable();

            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            scriptEnv.Set("self", this);
            foreach (var injection in injections)
            {
                scriptEnv.Set(injection.name, injection.value);
            }

            luaEnv.DoString(luaScript.text, "LuaTestScript", scriptEnv);

            Action luaAwake = scriptEnv.Get <Action>("awake");

            scriptEnv.Get("start", out luaStart);
            scriptEnv.Get("update", out luaUpdate);
            scriptEnv.Get("ondestroy", out luaOnDestroy);
            scriptEnv.Get("onTriggerEnter", out luaOnTriggerEnter);

            if (luaAwake != null)
            {
                luaAwake();
            }
        }
Exemple #27
0
    public LuaTable DoFile(string filePath, Base bindClass = null)
    {
        LuaTable scriptEnv = _luaEnv.NewTable();
        LuaTable meta      = _luaEnv.NewTable();

        meta.Set("__index", _luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        //将C# this绑定到 lua self中
        if (bindClass != null)
        {
            scriptEnv.Set("self", bindClass);
        }

        string path     = LuaConst.luaDir;
        string realPath = path + "/" + filePath + ".lua";

        byte[] fileBytes = System.IO.File.ReadAllBytes(realPath);

        //注意这里第二个参数要传全路径(否则zb编辑器无法激活对应的lua文本)
        _luaEnv.DoString(fileBytes, realPath, scriptEnv);

        return(scriptEnv);
    }
Exemple #28
0
        private void Awake()
        {
            scriptEnv = luaEnv.NewTable();
            // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
            // 初始化本地表
            LuaTable meta = luaEnv.NewTable();

            // 把lua化境交给本地表
            meta.Set("__index", luaEnv.Global);
            // 将本地表作为元表,给全局表
            scriptEnv.SetMetaTable(meta);
            //释放本地表
            meta.Dispose();

            //------------------------运行脚本

            //luaEnv.DoString("print(888)");
            //绑定lua的生命周期
            var luaAwake = scriptEnv.Get <Action>("awake");

            scriptEnv.Get("start", out luaStart);
            scriptEnv.Get("update", out luaUpdate);
            scriptEnv.Get("ondestroy", out luaOnDestroy);

            if (luaAwake != null)
            {
                luaAwake();
            }
        }
Exemple #29
0
        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            luaEnv = LuaManager.luaEnv; //此处要从LuaManager上获取 全局只有一个

            scriptEnv = luaEnv.NewTable();

            LuaTable meta = luaEnv.NewTable();

            meta.Set("__index", luaEnv.Global);
            scriptEnv.SetMetaTable(meta);
            meta.Dispose();

            string prefabName = name;

            if (prefabName.Contains("(Clone)"))
            {
                prefabName = prefabName.Split(new string[] { "(Clone)" }, StringSplitOptions.RemoveEmptyEntries)[0] + "View";
            }

            onInit         = scriptEnv.GetInPath <OnInitHandler>(prefabName + ".OnInit");
            onOpen         = scriptEnv.GetInPath <OnOpenHandler>(prefabName + ".OnOpen");
            onClose        = scriptEnv.GetInPath <OnCloseHandler>(prefabName + ".OnClose");
            onBeforDestroy = scriptEnv.GetInPath <OnBeforDestroyHandler>(prefabName + ".OnBeforDestroy");

            scriptEnv.Set("self", this);
            if (onInit != null)
            {
                onInit(userData);
            }
        }
Exemple #30
0
    void Awake()
    {
        scriptEnv = luaEnv.NewTable();

        // 为每个脚本设置一个独立的环境,可一定程度上防止脚本间全局变量、函数冲突
        LuaTable meta = luaEnv.NewTable();

        meta.Set("__index", luaEnv.Global);
        scriptEnv.SetMetaTable(meta);
        meta.Dispose();

        scriptEnv.Set("self", this);
        foreach (var injection in injections)
        {
            scriptEnv.Set(injection.name, injection.value);
        }
        luaEnv.AddBuildin("cmsgpack", XLua.LuaDLL.Lua.LoadCMsgPack);

        luaEnv.DoString(luaScript.text, "LuaBehaviour", scriptEnv);
        // luaEnv.AddBuildin("rapidjson", XLua.LuaDLL.Lua.LoadRapidJson);

        Action luaAwake = scriptEnv.Get <Action>("awake");

        scriptEnv.Get("start", out luaStart);
        scriptEnv.Get("update", out luaUpdate);
        scriptEnv.Get("ondestroy", out luaOnDestroy);

        if (luaAwake != null)
        {
            luaAwake();
        }
    }
Exemple #31
0
    public static IEnumerable<CustomGenTask> GetTasks(LuaEnv lua_env, UserConfig user_cfg)
    {
        LuaTable data = lua_env.NewTable();
        var assembly_infos = (from type in user_cfg.ReflectionUse
                              group type by type.Assembly.GetName().Name into assembly_info
                              select new { FullName = assembly_info.Key, Types = assembly_info.ToList()}).ToList();
        data.Set("assembly_infos", assembly_infos);

        yield return new CustomGenTask
        {
            Data = data,
            Output = new StreamWriter(GeneratorConfig.common_path + "/link.xml",
            false, Encoding.UTF8)
        };
    }