1. 程式人生 > 實用技巧 >Unity熱更新03-C#呼叫XLua-010-LuaMgr

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
    }

}