在场景中新建一个HotFixScript的C#的脚本,并把这个脚本挂载到场景中的任何一个游戏物体上面,然后打开该脚本,并把以下的代码复制到脚本中去!!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
public class HotFixScript : MonoBehaviour {
private LuaEnv luaenv;
void Start()
{
//创建Lua的虚拟机,也就是lua环境
luaenv = new LuaEnv();
//添加自定义装载机Loader
luaenv.AddLoader(MyLoader);
//该语句会把lua要加载的文件名fish作为MyLoader的参数传递进去
//当在Lua代码里调用了request使,request里的参数将传给回调函数里的参数'
//如果我们没定义Loader方法,系统默认是在Unity的Resource文件夹进行查找
luaenv.DoString("require 'fish' ");
}
private byte[] MyLoader(ref string filePath)
{
//lua脚本都放在电脑绝对路径D:\Xlua hot update project\XluaProjects\XluaProjects\FishingJoy\UnityPackageManager里面
string absPath = @"D:\Xlua hot update project\XluaProjects\XluaProjects\FishingJoy\UnityPackageManager\" + filePath + ".lua.txt";
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
}
private void OnDestroy()
{
luaenv.Dispose();
}
}