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();
}
}
运行结果截图如下: