Xlua热更新

---恢复内容开始---

1.当配置好xlua环境后,依次点击编辑栏的Xlua选项,如下:

 2.在工程文件中找到AssetsXluaResources,在此目录下新建两个txt文本,命名格式为xxx.lua.txt

3.在unity中创建编译脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;

public class XluaStudy : MonoBehaviour {
        //定义一个Xlua虚拟机
        private LuaEnv ev;
        // Use this for initialization
        void Start () {
        ev = new LuaEnv();
        //添加一个资源加载器
        ev.AddLoader(MyLoader);
        //执行对应文本文件
        ev.DoString("require'XluaMytest'");
    }
     //加载器
     private  byte[] MyLoader(ref string filePath) {
        //路径
        string path = @"D:Unity566f2unityWorksunityworkFishMaster_Study AssetsXLuaResources"
                        +filePath+".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
    }
    //在不用时释放虚拟机
    private void OnDisable() {
       //之前的赋值置空
        ev.DoString("require'XluaDispose'");
        //释放
        ev.Dispose();
    }
}

4.给第二步中的两个文本写入更新操作

第一个文本XluaMytest.lua.txt():

 print('hollow123456')

 xlua.hotfix(CS.GameManager, 'ClickBigTimerBtn', function(self)

                     self:AddPlayerGold(1) 

                    self. _bigTimer = self.bigTime

                    self:BigTimerBtnInteractable(false)

end)

第二个文本XluaDispose.lua.txt(用于置空虚拟机运行的方法):

xlua.hotfix(CS.GameManager, 'ClickBigTimerBtn', nil)

5.需要注意的是,需要更新的脚本需要引用Xlua命名空间,类的前面加上标签[Hotfix],;类中需要更新的方法加上标签[LuaCallCSharp]

 (如果原项目的脚本有改动,需要执行本文的第1步操作)

被更新的方法

---恢复内容结束---

原文地址:https://www.cnblogs.com/RainPaint/p/10115320.html