xlua中hotfix简单实用

tolua每次修改C#代码,tolua都需要生成代码,xlua无需生成,但是在最后实际发布时,xlua需要生成代码

这章主要是写一下hotfix实用

这个特性默认是关闭的,实用时需要在UNITY中添加HOTFIX_ENABLE宏,打开步骤(在Unity3D的File->Build Setting->Scripting Define Symbols下添加)

这个热补丁还依赖Cecil,添加HOTFIX_ENABLE宏之后,可能会报找不到Cecil。这时你需要到Unity安装目录下找到Mono.Cecil.dll,拷贝到项目里头。而HOTFIX_DEBUG_SYMBOLS则依赖Mono.Cecil.Pdb.dll,Mono.Cecil.Mdb.dll。

热补丁需要执行XLua/Generate Code才能正常运行。

不支持静态构造函数。

目前只支持Assets下代码的热补丁,不支持引擎,c#系统库的热补丁。

注意:要等打印了hotfix inject finish!后才运行例子,否则会类似xlua.access, no field __Hitfix0_Update的错误

将windows 下UNITY安装路径下C:UnityEditorDataManaged(Mono.Cecil.DLL等带有Cecil的dll文件)拷贝到Unity游戏项目文件夹中

using UnityEngine;
using System.Collections;
using XLua;

[Hotfix]
public class HotfixExample : MonoBehaviour {
    LuaEnv luaenv = new LuaEnv();
     public int tick = 0; //如果是private的,在lua设置xlua.private_accessible(CS.HotfixTest)后即可访问
   
    
    // Use this for initialization
    void Start () 
    {
    }

    // Update is called once per frame
    void Update () 
    {
	    if (++tick % 50 == 0)
        {
            Debug.Log(">>>>>>>>Update in C#, tick = " + tick);
        }
	}

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 100, 300, 150), "Hotfix"))
        {
            luaenv.DoString(@"
                xlua.hotfix(CS.HotfixTest, 'Update', function(self)
                    self.tick = self.tick + 1
                    if (self.tick % 50) == 0 then
                        print('<<<<<<<<Update in lua, tick = ' .. self.tick)
                    end
                end)
            ");
        }
    }
}

  

原文地址:https://www.cnblogs.com/unity3d-Yang/p/6410255.html