热补丁
一、普通函数
1.基础语法
xlua.hotfix(类,“函数名”,lua 函数)
2.实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [Hotfix, LuaCallCSharp ] public class HotFixMain : MonoBehaviour { void Start () { LuaMgr.Instance.Init(); LuaMgr.Instance.DoLuaFile("Main" ); Add(10 , 20 ); Speak("哈哈哈" ); } public int Add (int a, int b ) { return 0 ; } public static void Speak (string str ) { Debug.Log("hhhhh" ); } }
1 2 3 4 5 6 xlua.hotfix(CS.HotFixMain,"Add" ,function (self,a,b) return a+b end )xlua.hotfix(CS.HotFixMain,"Speak" ,function (a) print (a) end )
注意点:
直接写好代码 然后执行 是会报错的 解决步骤
加特性 [Hotfix]
加宏 File -> BilidingSetting -> PlayerSetting -> OtherSettings -> ScriptingDefineSymbols 中添加“HOTFIX_ENABLE”这个宏
生成代码
hotfix 注入 注入时需要官方那边下载的 Tool 包
热补丁的缺点
只要我们修改了热补丁类的代码就要修改 就要重新执行第 4 步
如果我们为打了 Hotfix 特性的 C#类新加了函数内容 不能只注入 必须要先生成代码 再注入 不然注入会报错
二、多函数替换
1.基础语法
xlua.hotfix(类,{函数名 1 = lua 函数 1,函数名 2 = lua 函数 2,···})
2.实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [Hotfix ] public class HotFixMain : MonoBehaviour { void Start () { LuaMgr.Instance.Init(); LuaMgr.Instance.DoLuaFile("Main" ); Add(10 , 20 ); Speak("哈哈哈" ); } public int Add (int a, int b ) { return 0 ; } public static void Speak (string str ) { Debug.Log("hhhhh" ); } }
1 2 3 4 5 6 7 8 9 10 11 xlua.hotfix(CS.HotFixMain,{ Update = function (self) print (os .time ()) end , Add = function (self,a,b) return a+b end , Speak = function (a) print (a) end })
三、构造函数和析构函数
1 2 3 4 5 6 7 8 9 10 11 12 public class HotfixTest { public HotfixTest () { Debug.Log("HotfixTest构造函数" ); } public void Speak (string str ) { Debug.Log(str); } ~HotfixTest() { } }
1 2 3 4 5 6 7 8 9 10 11 12 13 xlua.hotfix(CS.HotfixTest,{ [".ctor" ] = function () print ("Lua热补丁构造函数" ) end , Speak = function (self,a) print ("热补丁修改后打印" ..a) end , Finalize = function () end })
四、协程函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [Hotfix ] public class HotFixMain : MonoBehaviour { HotfixTest hotfixTest; void Start () { LuaMgr.Instance.Init(); LuaMgr.Instance.DoLuaFile("Main" ); StartCoroutine(TestCoroutine()); } public IEnumerator TestCoroutine () { while (true ) { yield return new WaitForSeconds (1f ) ; Debug.Log("C#协程函数打印" ); } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 util = require ("xlua.util" ) xlua.hotfix(CS.HotFixMain,{ TestCoroutine = function (self) return util.cs_generator(function () while true do coroutine .yield (CS.UnityEngine.WaitForSeconds(1 )) print ("打补丁后的协程函数" ) end end ) end })
五、索引器和属性的替换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class HotFixMain : MonoBehaviour { private int [] array = new int [] { 1 , 2 , 3 }; public int Age { get { return 0 ; } set { Debug.Log(value ); } } public int this [int index] { get { if (index >= array.Length || index < 0 ) { Debug.Log("溢出" ); return 0 ; } return array[index]; } set { array[index] = value ; } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 xlua.hotfix(CS.HotFixMain,{ set_Age = function (self,v) print ("lua重定向的属性" ..v) end , get_Age = function (self) return 10 end , set_Item = function (self,index,v) print ("Lua重定向设置索引器,索引" ..index.."值" ..v) end , get_Item = function (self) print ("Lua重定向获取索引器" ) return 999 end })
六、事件加减
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [Hotfix ] public class HotFixMain : MonoBehaviour { public event UnityAction myEvent; void Start () { LuaMgr.Instance.Init(); LuaMgr.Instance.DoLuaFile("Main" ); myEvent += Test; myEvent -= Test; } private void Test () { } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 xlua.hotfix(CS.HotFixMain,{ add_myEvent = function (self,del) print (del) print ("添加事件函数" ) end , remove_myEvent = function (self,del) print (del) print ("删除事件函数" ) end })
注意: 在事件加键的重定向 lua 函数中 千万不要把传入的委托往事件里面存 否则会死循环 一般都是存入 lua 中的一张 table 中 让逻辑让 lua 处理 存到 lua 容器中
七、泛型类替换
1 2 3 4 5 6 7 8 [Hotfix ] public class HotfixTest2 <T >{ public void Test <T >(T t ) { Debug.Log(t); } }
1 2 3 4 5 6 7 8 xlua.hotfix(CS.HotfixTest2(typeof(CS.System.String)),{ Test = function (self,str) print ("lua中为String打的补丁" ..str) end })