欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群
原工程地址:https://download.csdn.net/download/qq_37310110/11812518
之前记录了XLua工程的导入及简单用法,这次就记录一下Xlua的热更标签逻辑
一:环境初始化1.在PlayerSettings里面添加宏信息
HOTFIX_ENABLE
2.执行菜单生成命令
XLua>Generate Code,会生成Wrip文件存放在Xlua/Gen目录
3.执行菜单注入命令
XLua>HotFix Inject In Editor成功后会有输出日志提示,如果有报错提示“please install the tools”,就需要把Xlua原工程里面的Tools文件夹拷贝到项目里跟Assets文件夹同级别的位置
二:基础案例1.HotFix特性标签
在使用c#开发的时候需要后续进行“热补丁修复”的类,需要在类的头部添加一个特性标签:[HotFix],表示该类可以被Xlua热修复
2.HotFix语法
xlua.hotfix(CS.类名,‘方法名’,lua方法);这个是lua代码结构,需要使用lua虚拟机对象中的Dostring方法执行,说白了就是某个类中的某个方法用lua方法进行修复
3.案例
演示阶段我们每次修改完c#都需要执行一次“注入指令”;真实开发时lua代码肯定是和c#代码分离的.
a.修复无参普通方法
b.修复有参普通方法:有参函数修复的时候,需要传递当前脚本对象this,在lua中用self代替function(self,a,b)
--lua代码
xlua.hotfix(CS.MyHotFix,'Hello',function()
print('Lua Hello...')
end)
xlua.hotfix(CS.MyHotFix,'Add',function(self,a,b)
print('Lua: a+b='..a+b)
end)
using XLua;
[Hotfix]
public class MyHotFix : MonoBehaviour
{
private LuaEnv luaEnv;
// Start is called before the first frame update
void Start()
{
luaEnv = new LuaEnv();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
Hello();
Add(5,10);
}
if (Input.GetKeyDown(KeyCode.W))
{
luaEnv.DoString("require 'HotFix'");
Debug.Log("注入修复lua...");
}
}
private void Hello()
{
Debug.Log("C# hello...");
}
private void Add(int a,int b)
{
Debug.Log("C#: a+b="+(a+b));
}
}
c#原始逻辑生成100个cube
[Hotfix]
public class CreateWall : MonoBehaviour
{
private GameObject prefabsCube;
// Start is called before the first frame update
void Start()
{
prefabsCube = Resources.Load("Cube");
Createwall(prefabsCube);
}
private void Createwall(GameObject prefab)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
GameObject.Instantiate(prefab,new Vector3(i,j,0),Quaternion.identity);
}
}
}
}
lua更新逻辑生成16个cube
local GameObject = CS.UnityEngine.GameObject
local Vector3 = CS.UnityEngine.Vector3
local Quaternion = CS.UnityEngine.Quaternion
xlua.hotfix(CS.CreateWall,'Createwall',function(self,prefab)
for i = 0 ,3,1 do
for j = 0,3,1 do
GameObject.Instantiate(prefab,Vector3(i,j,0),Quaternion.identity);
end
end
end)
print('Xlua HotFix Over...')
欢迎加入Unity业内qq交流群:956187480
qq扫描二维码加群