1.创建C#脚本HotFilx(关联脚本_挂相机上了)
2.编辑脚本HotFilx脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
/// <summary>
/// 搭建lua虚拟环境
/// </summary>
public class HotFix : MonoBehaviour {
//lua环境变量
private LuaEnv luaenv;
// Use this for initialization
void Awake() {
//实例化lua环境变量
luaenv = new LuaEnv();
//加载程序(自定义Loader)
luaenv.AddLoader(MyLoader);
//执行 fish.lua.txt 文件
luaenv.DoString("require'fish'");
}
//Loader
private byte[] MyLoader(ref string filePath)
{
//绝对路径
//string absPath = @"E:\MyTest\Xlua_HotUpdate\PlayerGamePackage\" + filePath + ".lua.txt";
//相对路径
string absPath = Application.streamingAssetsPath+ @"\PlayerGamePackage\"+filePath +".lua.txt";
//读取lua脚本内容
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
}
//不用的时候释放
private void OnDestroy()
{
luaenv.Dispose();
}
}
3.Lua脚本fish.lua.txt

4.lua脚本位置(这里用相对路径)

5.运行测试


网友评论