zoukankan      html  css  js  c++  java
  • Unity热更新03-C#调用XLua-010-LuaMgr

    using System.IO;
    using UnityEngine;
    using XLua;
    
    namespace DSFramework {
    
        /// <summary>
        /// Lua管理器
        /// 保证解析器的唯一性
        /// </summary>
        public class DSLuaMgr : DSingleMono<DSLuaMgr> {
            //lua解析器
            private LuaEnv _luaEnv = null;
    
            /// <summary>
            /// 初始化lua解析器
            /// </summary>
            private void Init() {
                if (_luaEnv != null)
                    return;
                _luaEnv = new LuaEnv();
    
                //允许我们自定义加载lua的规则
                // 会自动执行我们传入的回调函数
                _luaEnv.AddLoader(MyCustomLoader);
                _luaEnv.AddLoader(MyCustomABLoader);
            }
    
            #region 加载资源目录下的文件
    
            //自动执行
            private byte[] MyCustomLoader(ref string filepath) {
                //通过函数中的逻辑加载lua文件
                //Application.dataPath 获取到 Asset目录所在的 路径
                string path = Application.dataPath + "/Scripts/LuaScript/" + filepath + ".lua";
    
                //有路径就去加载文件
                //判断路径是否存在
                if (File.Exists(path)) {
                    //返回当前文件的路径
                    return File.ReadAllBytes(path);
                } else {
                    Debug.Log("重定向失败,文件名为:" + filepath);
                }
    
                return null;
            }
    
            #endregion
    
            #region 加载AB包下的文件
    
            // Lua脚本会放在AB包中
            // 最终会通过加载AB包来加载其中的lua脚本资源
            // AB 包不能识别 lua 脚本, 必须以 txt 为后缀
            //自动执行
            private byte[] MyCustomABLoader(ref string filepath) {
                //加载 AB包中的 lua 脚本
                TextAsset txtLua = DSABMgr.Instance.LoadRes<TextAsset>("lua", filepath + ".lua");
    
                if (_luaEnv != null) return txtLua.bytes;
                else Debug.Log($"MyCustomABLoader重定向加载失败,文件名:{filepath}");
                return null;
    
                // Debug.Log("进入AB包重定向");
                //
                // //从AB包中加载lua文件
                // //加载AB包
                // string path = Application.streamingAssetsPath + "/lua";
                // AssetBundle ab = AssetBundle.LoadFromFile(path);
                //
                // //加载lua文件 并 返回
                // TextAsset tx = ab.LoadAsset<TextAsset>(filepath + ".lua");
                //
                // //加载Lua文件,byte数组
                // return tx.bytes;
            }
    
            #endregion
    
            #region Lua 方法
    
            /// <summary>
            /// 得到lua中的 _G 表(总表,所有的全局变量都在此存储)
            /// </summary>
            public LuaTable Global { get { return _luaEnv.Global; } }
    
            /// <summary>
            /// 加载lua文件
            /// </summary>
            /// <param name="fileName"></param>
            public void DoLuaFile(string fileName) {
                Init();
                _luaEnv.DoString($"require('{fileName}')");
            }
    
            /// <summary>
            /// 调用lua脚本
            /// </summary>
            /// <param name="str"></param>
            public void DoString(string str) {
                Init();
                _luaEnv.DoString(str);
            }
    
            /// <summary>
            /// 回收垃圾
            /// </summary>
            public void Tick() {
                Init();
                _luaEnv.Tick();
            }
    
            /// <summary>
            /// 销毁解析器
            /// </summary>
            public void Dispose() {
                Init();
                _luaEnv.Dispose();
            }
    
            #endregion
        }
    
    }
    
  • 相关阅读:
    Python 基础【第三篇】输入和输出
    把linux可执行程序做成一个服务[转]
    linux 下启动程序的时候会显示坏的解释器,或者没有那个文件
    利用GDB进行多线程调试
    两个结构体ifconf和ifreq
    centos系统修改网络配置注意事项
    yum错误:rpmdb: BDB0113 Thread/process 4227/139813012539200 failed: BDB1507 Thread died in Berkeley DB library
    CentOs安装MySql
    周末遐想(计算最长英语单词链)
    单词词频统计(12组)
  • 原文地址:https://www.cnblogs.com/unitysir/p/13891040.html
Copyright © 2011-2022 走看看