上篇文章:
Unity XLua学习笔记(三):Lua调用C#
热补丁主要作用是用Lua替换C#中的方法
在做热补丁前,需要将GitHub上下载的XLua工程中的Tools文件夹导入到项目中
导入时注意路径,需要放在Assets文件的同级路径下
回到Unity中,打开PlayerSettings,做如下设置
输入:HOTFIX_ENABLE
C#测试代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using XLua;
[Hotfix]
public class HotfixTestClass
{
//构造函数
public HotfixTestClass()
{
Debug.Log("HotfixTest构造函数");
}
public void Speak(string str)
{
Debug.Log(str);
}
//析构函数
~HotfixTestClass()
{
}
}
[Hotfix]
public class HotfixTest : MonoBehaviour
{
HotfixTestClass hotTest;
// Start is called before the first frame update
public int[] array=new int[] {1,2,3};
event UnityAction myEvent;
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
{
if(index>=array.Length||index<0)
{
Debug.Log("索引不正确");
return;
}
array[index]=value;
}
}
void Start()
{
XLuaMgr.GetInstance().Init();
XLuaMgr.GetInstance().DoLuaFile("Main");
Debug.Log( Add(10,20));
Speak("dkjfalsdk");
hotTest=new HotfixTestClass();
hotTest.Speak("嘻嘻嘻嘻嘻");
//StartCoroutine(TestCoroutine());
// this.Age=100;
// Debug.Log(this.Age);
// this[99]=100;
// Debug.Log(this[9999999]);
// myEvent+=TestTest;
// myEvent-=TestTest;
}
private void TestTest()
{
}
// Update is called once per frame
void Update()
{
}
IEnumerator TestCoroutine()
{
while(true)
{
yield return new WaitForSeconds(1f);
Debug.Log("C#协程打印一次");
}
}
public int Add(int a,int b)
{
return 0;
}
public static void Speak(string str)
{
Debug.Log("哈哈哈哈");
}
}
Lua代码
print("************第一个热补丁****************")
--直接写好代码 运行 时会报错的
--我们必须做4个非常重要的操作
--1.加特性
--2.加宏 第一次开发热补丁需要加
--3.生成代码
--4.hotfix注入 注入时可能报错 提示你要引入Tools
--热补丁的缺点:只要我们修改了热补丁类的代码,我们就需要重新执行第4步
--需要重新点击 注入
--lua当中 热补丁代码固定写法
--xlua.hotfix(类,"函数名",lua函数)
xlua.hotfix(CS.HotfixTest,"Add",function(self,a,b)
return a+b
end)
--静态函数 不用传第一个参数
xlua.hotfix(CS.HotfixTest,"Speak",function(a)
print(a)
end)
在运行前,一定注意所有要进行热补丁的C#类,都要加上[Hotfix],同时执行XLua----->Generate Code,最后执行XLua-------->Hotfix Inject in Editor
只要这些被打伤特性的C#代码发生了改变,就必须重新执行XLua-------->Hotfix Inject in Editor
首先执行这两句
对应的Lua代码和C#代码
运行程序,C#中的方法被Lua替换了
Lua热补丁多函数替换
Lua代码
构造和析构函数和别的函数不同 不是替换 是先调用原逻辑 再调用Lua逻辑
xlua.hotfix(CS.HotfixTest,{
-- Update=function(self)
-- print(os.time())
-- end,
Add=function(self,a,b)
return a+b
end,
Speak=function(a)
print(a)
end
})
xlua.hotfix(CS.HotfixTestClass,{
--构造函数 热补丁 固定写法!!!
--构造和析构函数和别的函数不同 不是替换 是先调用原逻辑 再调用Lua逻辑
[".ctor"]=function()
print("Lua热补丁构造函数")
end,
Speak=function(self,a)
print("我说"..a)
end,
--析构函数固定写法
Finalize=function()
end
})
C#代码
HotfixTestClass类
[Hotfix]
public class HotfixTestClass
{
//构造函数
public HotfixTestClass()
{
Debug.Log("HotfixTest构造函数");
}
public void Speak(string str)
{
Debug.Log(str);
}
//析构函数
~HotfixTestClass()
{
}
}
HotfixTest类
[Hotfix]
public class HotfixTest : MonoBehaviour
{
HotfixTestClass hotTest;
// Start is called before the first frame update
public int[] array=new int[] {1,2,3};
event UnityAction myEvent;
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
{
if(index>=array.Length||index<0)
{
Debug.Log("索引不正确");
return;
}
array[index]=value;
}
}
void Start()
{
XLuaMgr.GetInstance().Init();
XLuaMgr.GetInstance().DoLuaFile("Main");
Debug.Log( Add(10,20));
Speak("dkjfalsdk");
hotTest=new HotfixTestClass();
hotTest.Speak("嘻嘻嘻嘻嘻");
//StartCoroutine(TestCoroutine());
// this.Age=100;
// Debug.Log(this.Age);
// this[99]=100;
// Debug.Log(this[9999999]);
// myEvent+=TestTest;
// myEvent-=TestTest;
}
private void TestTest()
{
}
// Update is called once per frame
void Update()
{
}
IEnumerator TestCoroutine()
{
while(true)
{
yield return new WaitForSeconds(1f);
Debug.Log("C#协程打印一次");
}
}
public int Add(int a,int b)
{
return 0;
}
public static void Speak(string str)
{
Debug.Log("哈哈哈哈");
}
}
unity运行打印
Lua热补丁替换协程函数
C#代码
lua代码
运行Unity打印,协程被替换
Lua热补丁,属性和索引器替换
C#测试代码
Lua代码
运行Unity打印
Lua热补丁,事件±替换
C#测试代码
Unity运行打印