Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

热补丁

一、普通函数

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)

注意点:

  • 直接写好代码 然后执行 是会报错的 解决步骤

    1. 加特性 [Hotfix]
    2. 加宏 File -> BilidingSetting -> PlayerSetting -> OtherSettings -> ScriptingDefineSymbols 中添加“HOTFIX_ENABLE”这个宏
    3. 生成代码
    4. hotfix 注入 注入时需要官方那边下载的 Tool 包
  • 热补丁的缺点

    1. 只要我们修改了热补丁类的代码就要修改 就要重新执行第 4 步
    2. 如果我们为打了 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;
// Start is called before the first frame update
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
--在lua中要使用协程 必须要配合xlua中的xlua.util这个文件
util = require("xlua.util")

xlua.hotfix(CS.HotFixMain,{
TestCoroutine = function (self)
--返回一个 xlua处理过的lua函数
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_属性名
--get_属性名
set_Age = function (self,v)
print("lua重定向的属性"..v)
end,
get_Age = function (self)
return 10
end,
--索引器热补丁
--固定写法
--set_Item 索引器设置
--get_Item 索引器获取
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_事件名 代表事件+=操作
--remove_事件名 代表事件-=操作
add_myEvent = function (self,del)
print(del)
print("添加事件函数")
--注意:
--在事件加键的重定向lua函数中
--千万不要把传入的委托往事件里面存
--例如:self:mySelf("+",del)
--否则会死循环
--一般都是存入lua中的一张table中
end,
remove_myEvent = function (self,del)
print(del)
print("删除事件函数")
--注意:
--在事件加键的重定向lua函数中
--千万不要把传入的委托往事件里面存
--例如:self:mySelf("+",del)
--否则会死循环
--一般都是存入lua中的一张table中
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
--泛型类 T 是可以变化的
--所以 lua中的替换 要一个类型一个类型的来
--如果 不修改对应类型,则不会进行修改
xlua.hotfix(CS.HotfixTest2(typeof(CS.System.String)),{
Test = function (self,str)
print("lua中为String打的补丁"..str)
end
})

评论