using System;
using System.IO;
using System.Text;
using UnityEngine;
using XLua;
public class HelloWorld01 : MonoBehaviour
{
private LuaEnv luaEnv;
void Start()
{
luaEnv = new LuaEnv();//全局唯一
luaEnv.AddLoader(CustomLoader);//添加自定义路径
luaEnv.DoString("require '007' ");//在自定义路径中查找007文件
Action creatFun= luaEnv.Global.Get<Action>("init");//调用Lua中的init方法
creatFun();
}
private byte[] CustomLoader(ref string filepath)
{
string path = Application.dataPath + "/Scripts/Xlua/" + filepath + ".lua";//自定义Lua文件路径
return Encoding.UTF8.GetBytes(File.ReadAllText(path));//返回对应文件的byte[]数据
}
void OnDestroy()
{
luaEnv.Dispose();
luaEnv = null;
}
}
–