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();
}
}
運行結果截圖如下: