unity3d:xlua hotfix 热补丁修改c#脚本bug

先在场景中挂载脚本,加载自定义Loader

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
using UnityEngine.Networking;

public class HotFixScript : MonoBehaviour {

    private LuaEnv luaEnv;

    private void Awake()
    {
        luaEnv = new LuaEnv();
        luaEnv.AddLoader(MyLoader);
        luaEnv.DoString("require 'xxx'");
    }

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }


    private byte[] MyLoader(ref string filePath)
    {
        string absPath = @"xxx\" + filePath+".lua.txt";
        return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
    }

    private void OnDisable()
    {
        luaEnv.DoString("require 'xxxDispose'");
    }

    private void OnDestroy()
    {
        luaEnv.Dispose();
    }

要修复的C#脚本类上打上
[Hotfix]
这个类要修复的函数打上
[LuaCallCSharp]

例如

[Hotfix]
public class Test1: MonoBehaviour
{

[LuaCallCSharp]
    private void OnTest()
    {
    Debug.Log("C#");
    }
}

lua代码如下:

local UnityEngine = CS.UnityEngine
xlua.hotfix(CS.Test1,'OnTest',function(self)
    -- body
        UnityEngine.Debug.Log("Lua")
    end
end)

猜你喜欢

转载自blog.csdn.net/luoyikun/article/details/80220472