您的位置:首页 > 移动开发 > Unity3D

初探腾讯Unity手游热更新方案xlua

2017-01-06 22:57 591 查看
最近腾讯开源了旗下Unity手游热更新方案xlua到GitHub,引发了手游圈不小的关注。Unity热更一直是一个令开发者头疼的问题,ios的限制,效率问题等等。xlua提出了一个非常好的解决方案:

1、平时用C#开发;

2、运行也是C#,性能秒杀Lua;

3、有bug的地方下发个Lua脚本fix了,下次整体更新时可以把Lua的实现换回正确的C#实现,更新时甚至可以做到不重启游戏;

哇,这个思路是不是很棒,听着是不是很心动,一起来试验一下吧。

下载源码(https://github.com/Tencent/xLua),新建一个unity project,将源码的Assets下的文件拷贝到project的Assets目录下



这样在菜单栏会生成一个XLua菜单,有Generate Code和Clear Generate Code两个选项。Generate Code生成C#与Lua之间桥接的一些代码,生成的位置就在上图的Assets/XLua/Gen,Clear Generate Code可以删除这些代码。

然后新建一个test.cs挂在main camera上

using UnityEngine;
using System.Collections;
using XLua;

[Hotfix]
public class test : MonoBehaviour {
LuaEnv luaenv = new LuaEnv();

// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
if (luaenv != null)
{
luaenv.Tick();
}
}

public int Add(int a, int b)
{
return a - b;
}

void OnGUI()
{
if (GUI.Button(new Rect(10, 100, 300, 150), "Hotfix"))
{
luaenv.DoString("require 'hotfix'");

//luaenv.DoString(@"
//    xlua.hotfix(CS.test, 'Add', function(self, a, b)
//		return a + b
//	end)
//");

int num = Add (2,1);
print (num);
}
}

void OnDestroy()
{
luaenv.Dispose();
}
}
特别注意[Hotfix]标签,它声明了test类可以通过xlua.hotfix方法来修改test类下的函数和属性,原理后面说。

要顺利跑通以上代码需要添加HOTFIX_ENABLE宏(在Unity3D的File->Build Setting->Scripting Define Symbols下添加)。另外,热更特性依赖Cecil,添加HOTFIX_ENABLE宏之后,可能会报找不到Cecil。这时你需要到Unity安装目录下找到Mono.Cecil.dll,拷贝到项目里头。

可以看到成员函数Add写错了,这时候我们需要通过加载一个名叫hotfix的lua文件来把它热更新掉(正式开发环境下,如何下载该文件就不详述了)

在Assets/Resources/下新建一个hotfix.lua.txt

xlua.hotfix(CS.test, 'Add', function(self, a, b)
return a + b
end)
这样我们在点击Button后,xlua加载了这个hotfix.lua,将Add方法重写了!

除了重写成员函数,甚至可以重写整个类
xlua.hotfix(CS.test, {
Start = function(self)

end;
Add = function(self, a, b)
return a + b
end;
Minus = function(self, a, b)
return a - b
end;
})
如果你仅仅希望用热更新来fix bug,是不是再方便不过了。

最后说说xlua.hotfix方法的原理:

xLua会在C#编译成il后插入一个处理,该处理会从il层面为每个打了Hotfix标签的类型的函数开头加入几个il指令,如果用C#描述是这样的:

加il指令前:
public int Add(int a, int b)
{
return a + b;
}
加了il指令:
static Func<object, int,="" int=""> hotfix_Add = null;

public int Add(int a, int b)
{
if (hotfix_Add != null) return hotfix_Add(this, a, b);

return a + b;

}</object,>
判断一个delegate是否为空(没用过C#的童鞋可以理解为函数指针),如果不为空就调用这个delegate,而这个delegate指向的是一个把调用转发给lua的函数。

由于是在il层面做这事,所以不会影响到源码。il2cpp是在我们处理完成后执行,所以这方案il2cpp也能行得通,mono就更不在话下了。换句话说,理论上支持所有Unity支持的平台。对函数有轻微影响,没hotfix时就一个指针为空判断而已。

感谢腾讯的开源以及chexiongsheng的Contribution。

传送门:

1.http://gad.qq.com/article/detail/7182055

2.http://gad.qq.com/article/detail/7182056





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 手游 c# 开源 腾讯