xlua的官网:https://github.com/Tencent/xLua.
xlua的说明:http://gad.qq.com/article/detail/42303.
C#中调用Lua中的函数,用lua中的table对应C#中的类:
C#中的代码:
public class Test : MonoBehaviour
{
public TextAsset luaScripts;//lua脚本
internal static LuaEnv luaEnv = new LuaEnv();//全局唯一
private Action luaStart;
private Action luaUpdate;
private Action luaOnDestroy;
private LuaTable scriptTable;
private void Awake()
{
scriptTable = luaEnv.NewTable();//新建一张表
LuaTable mat = luaEnv.NewTable();
mat.Set("__index",luaEnv.Global);
scriptTable.SetMetaTable(mat);//设置元表
mat.Dispose();//释放
scriptTable.Set("self",this);
luaEnv.DoString(luaScripts.text,"test",scriptTable);
Action luaAwake = scriptTable.Get<Action>("awake");
scriptTable.Get("start", out luaStart);
scriptTable.Get("update",out luaUpdate);
scriptTable.Get("onDestroy",out luaOnDestroy);
if (luaAwake!=null)
{
luaAwake();
}
}
void Start()
{
if (luaStart!=null)
{
Debug.Log("c# start");
luaStart();
}
}
void Update()
{
if (luaUpdate!=null)
{
Debug.Log("c# update");
luaUpdate();
}
}
private void OnDestroy()
{
if (luaOnDestroy!=null)
{
Debug.Log("c# ondestroy");
luaOnDestroy();
}
}
}
Lua代码:
function awake()
print("lua awake")
print("name:"..self.name)
end
function start()
print("lua start")
end
function update()
print("lua update")
end
function onDestroy()
print("lua ondestroy")
end
private byte[] MyLoadFrom(ref string filePath)
{
string loadFile = Application.streamingAssetsPath + "/"+filePath + ".lua.txt";
return Encoding.UTF8.GetBytes(File.ReadAllText(loadFile));
}
xlua可以在类上面加上[Hotfix]的方式是非常不建议的,此做法会导致代码非常的混乱,你不能一眼看出哪些代码加入了[Hotfix]
Xlua官方建议在一个static类的static字段或者属性里头配置一个列表。属性可以用于实现的比较复杂的配置,比如根据Namespace做白名单。
两种白名单打标签的方式:
用属性的方式打标签可以添加一些判断条件,也可以对Namespace做白名单。
[Hotfix]
public static List<Type> hotfixList
{
get
{
string[] allClassName = new string[] {
"BallSkinUI",
"Item"
};
return (from type in Assembly.Load("Assembly-CSharp").GetTypes()
where allClassName.Contains(type.Name)
select type).ToList();
}
}
[Hotfix]
public static List<Type> UIhotfixList = new List<Type>
{
typeof(BallSkinUI),
typeof(Item)
};
LuaEnv lua;
void Start()
{
lua = new LuaEnv();
lua.AddLoader(MyLoadFrom);
lua.DoString("require'Test'");
}
private byte[] MyLoadFrom(ref string filePath)
{
string loadFile = Application.streamingAssetsPath + "/"+filePath + ".lua.txt";
return Encoding.UTF8.GetBytes(File.ReadAllText(loadFile));
}
private void OnDisable()
{
lua.DoString("require'TestDisable'");
}
private void OnDestroy()
{
lua.Dispose();
}
我们需要在PlayerSetting>>OtherSetting>>Scripting Define Symbols中增加HOTFIX_ENABLE参数(打开宏)
编写热更代码后,点击菜单的XLua/Hotfix Inject In Editor打印了“hotfix inject finish!”或者“had injected!”即为成功驻入!
local unity=CS.UnityEngine
local skinCondtion=CS.SkinCondition
xlua.hotfix(CS.BallSkinUI,'InitCreateSkin',function(self)
for i=0,29,1 do
local go=unity.GameObject.Instantiate(self.skinPanel)
go.name="luaName"..i
go.transform:SetParent(self.skinParent)
local item=go:GetComponent('Item')
if(i==0) then
item.ItemSkin=skinCondtion.USE
go.transform:GetChild(5):GetChild(1).gameObject:SetActive(true)
end
item.ID=i
self.AllSkinItem[i]=item
go.transform:GetChild(1):GetComponent('Image').sprite=self.skinSprite[i]
end
end)