hotfix接入,请查看上一篇:xLua-Hotfix接入(4)
修补各种类型的方法
注意:
- 首次需要hotfix的类名,要加到
HotfixList
中。
- lua 要访问的类名
1 2 3 4 5 6 7 8 9 10 11
| public class Animal { private string mName; public string Name { get { return "error"; } set { mName = "error"; } } public Animal(string name) { mName = name; } public void Congrate(){ Debug.Log(mName + "打招呼"); } }
|
普通方法
1 2 3
| xlua.hotfix(CS.Animal, 'Congrate', function(self) print('inject success') end)
|
修复特殊的方法
构造函数
构造函数的lua方法名是.ctor
,此外构造函数的热补丁并不是替换,而是执行原有逻辑后再调用lua。
1 2 3
| xlua.hotfix(CS.Animal, '.ctor', function(self, name) self.mName = '[['..name..']]' end)
|
索引器
索引器热修复对应lua方法是get_Item
/set_Item
。
参数为:
- get_Item(self, key) 返回value
- set_Item(self, key, value)
1 2 3
| xlua.hotfix(CS.Animal, 'get_Item', function(self, key)
end)
|
get/set 属性
get/set属性的lua方法名对应是get_属性名
/set_属性名
.
参数为:
- get_属性名(self) 返回value
- set_属性名(self, value)
1 2 3 4 5 6 7
| xlua.hotfix(CS.Animal, 'get_Name', function(self) return self.mName end)
xlua.hotfix(CS.Animal, 'set_Name', function(self, value) self.mName = value end)
|
+/- 事件
事件的+/-操作对应lua的方法是:add_事件名
/remove_事件名
析构函数
析构函数对应lua的方法名是Finalize
, 传一个self参数.
泛型
泛型类型实例化后都是一个新的类型,不能直接对泛型打补丁,只能对实例化后的类型打补丁。
Unity协程
1 2 3 4 5 6 7 8 9
| IEnumerator Start() { while (true) { yield return new WaitForSeconds(3); Debug.Log("Wait for 3 seconds"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| util = require("xlua.util") xlua.hotfix(CS.HotFixSubClass,{ Start = function(self) return util.cs_generator(function() while true do coroutine.yield(CS.UnityEngine.WaitForSeconds(1)) print('Wait for 1 seconds') end end) end; })
|
多方法修复/整个脚本代码修复
1 2 3 4 5 6 7 8 9 10 11 12 13
| xlua.hotfix(CS.Animal, { [.ctor] = function(self)
end, Start = function(self) end, Finalize=function(self) end })
|