繼XLua後騰訊又開源了InjectFix熱更新修復方案,可以實現在Unity線上客戶端內,不用迭代新版本,就能快速修復遊戲的線上bug,InjectFix開源地址:https://github.com/Tencent/InjectFix
接下來我們簡單熟悉下例子工程,首先clone出來我們的工程,在Source/VSProj中雙擊build_for_unity來編譯工程
unity打開對應的工程,打開HelloWorld場景,未來更方便我們查看運行結果,在HelloWorld類中添加如下代碼:
private void OnGUI()
{
GUI.TextField(new Rect(0, 0, 100, 50), "10 + 9 = " + new IFix.Test.Calculator().Add(10, 9));
}
運行
結果錯誤,經過查看Calculator中計算錯誤,現在我們按照文檔,將Calculator的add函數的標籤[Patch]打開,如圖:
執行菜單InjectFix/Fix,此時會在工程目錄下生成補丁集,如圖:
將該文件拷貝到Resources目錄,將代碼回滾,執行Inject注入,運行可以看到正確結果
當然在真正的項目中我們是不可能把patch放到工程文件裏面,因爲需要我們熱更出去,將文件拷貝到Application.persistentDataPath目錄下,修改Helloworld代碼,如下:
if (File.Exists(Application.persistentDataPath + "/Assembly-CSharp.patch.bytes"))
{
//var patchData = new FileStream(Application.persistentDataPath + "/Assembly-CSharp.patch.bytes", FileMode.Open);
//if (patch != null)
//{
UnityEngine.Debug.Log("loading Assembly-CSharp.patch ...");
var sw = Stopwatch.StartNew();
//PatchManager.Load(new MemoryStream(patchData.));
PatchManager.Load(Application.persistentDataPath + "/Assembly-CSharp.patch.bytes");
UnityEngine.Debug.Log("patch Assembly-CSharp.patch, using " + sw.ElapsedMilliseconds + " ms");
//}
}