添加xLua到项目中和遇到的一些坑




1、添加xLua

  1. 在GitHub中下载腾讯xLua的最新版本

  2. 把Asset文件夹下的四个文件和Tools文件夹添加到项目中

  3. 在项目的Player Setting中添加宏 HOTFIX_ENABLE

  4. 把Unity安装目录下的3个dll文件加入刚才导入项目的xLua中
    导入dll文件
    F:UnityEditorDataManaged
    Unity.Cecil.dll
    Unity.Cecil.Mdb.dll
    Unity.Cecil.Pdb.dll
    导入到
    AssetXLuaSrcEditor

  5. 点击左上角Generate Code + Hotfix Inject In E 注入代码

    如果成功显示:

    则说明添加成功

[使用xLua时常见错误解答](https://github.com/Tencent/xLua/blob/master/Assets/XLua/Doc/faq.md)

2、注意事项

  1. 在修改了任何一处C#代码,都需要重新注入代码。更改lua文件代码则不需要。

  2. 在发布游戏时要把xLua自带的Example例子删除,然后 Clear+GC+HIIE 重新注入代码

3、我遇见的一些坑即解决方法

  1. lua虚拟机的初始化和打补丁要放在Awake函数中,不然可能补丁无效。

  2. 通过 xlua.hotfix(class, method, func) 注入代码,也要通过 xlua.hotfix(class, method, nil) 再删除掉。
    注意要在lua虚拟机释放之前完成(释放方法在OnDestroy中,则删除方法可以放在OnDestroy周期前面的OnDisable中)。

  3. lua代码没有问题,但在打包AssetBundles的时候报错:

Assets/XLua/Gen/UnityEngineLightWrap.cs(614,59): error CS1061: Type `UnityEngine.Light' does not contain a definition for `lightmapBakeType' and no extension method `lightmapBakeType' of type `UnityEngine.Light' could be found. Are you missing an assembly reference?

解决:找到xLua文件里的 ExampleGenConfig.cs 文件
把代码:

new List<string>(){"UnityEngine.Light", "lightmapBakeType"},

添加到黑名单中
效果如图:

  1. 自定义loader从本地加载文件时是使用utf8读取的,所以要注意文本的编码格式要为utf8,不然可能出问题。
System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(path));
原文地址:https://www.cnblogs.com/Fflyqaq/p/11837109.html