天天看點

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#");
    }
}      
local UnityEngine = CS.UnityEngine
xlua.hotfix(CS.Test1,'OnTest',function(self)
  -- body
    UnityEngine.Debug.Log("Lua")
  end
end)