using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.IO;
using XLua;
public class xLuaEnv
{
private static xLuaEnv _instance = null;
private xLuaEnv()
{
_env = new LuaEnv();
_env.AddLoader(ProjectLoader);
}
public static xLuaEnv Instance
{
get
{
if(_instance == null)
{
_instance = new xLuaEnv();
}
return _instance;
}
}
private LuaEnv _env;
public byte[] ProjectLoader(ref string filepath)
{
//filepath是从Lua的require("路径")
//在项目的Assets的同级目录下创建一个可写文件夹(DataPath)
//通过构造路径,将require加载的文件夹指向我们想要的路径
string path = Application.dataPath;
path = path.Substring(0, path.Length - 7) + "/DataPath/Lua" + filepath + ".lua";
Debug.Log(path);
if (File.Exists(path))
{
//将Lua文件读取为字节数组,xLua的环境会根据这个数组去执行Lua代码
return File.ReadAllBytes(path);
}
else
{
return null;
}
}
//用于释放Lua解析器,同时销毁_instance对象
public void Free()
{
_env.Dispose();
_instance = null;
}
/// <summary>
/// 用于执行Lua代码
/// </summary>
/// <param name="code">要执行的Lua代码</param>
/// <returns>Lua代码的返回值</returns>
public object[] RunLua(string code)
{
return _env.DoString(code);
}
}
Crystal‘s Lua 封装调用类
最新推荐文章于 2024-08-22 20:42:18 发布