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();
    }
}

在这里插入图片描述

运行结果截图如下:
在这里插入图片描述

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