xlua-使用自定義的lua文件加載器加載lua

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
/*
 * Author:W
 * 自定義Loader加載器,加載指定目錄下的lua文件
 */
public class LuaDefineLoader : MonoBehaviour
{
    private LuaEnv luaEnv;
    // Start is called before the first frame update
    void Start()
    {
        luaEnv = new LuaEnv();

        //添加自定義的加載器:先從自定義的加載器中加載,如果加載到了lua文件,則直接執行,否則繼續使用
        //內置的加載器尋找加載
        luaEnv.AddLoader(MyLoader);

        luaEnv.DoString("require 'MyLoader'");
    }

    /// <summary>
    /// 自定義Loader
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    private byte[] MyLoader(ref string filePath)
    {
        string path = Application.streamingAssetsPath + "/" + filePath + ".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
    }

    private void OnDestroy()
    {
        luaEnv.Dispose();
    }
}

在這裏插入圖片描述

運行結果截圖如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章