一,準備工作
首先將附件內IFixToolKit工具放在和Assets同級目錄下。之後將附件內IIFix文件夾放在Assets子目錄內。最後將IFix.Core.dll放在Plugins目錄下。
二,代碼部分
IFix\Editor文件夾下放入自己遊戲所需的配置文件,代碼如下:
using System.Collections.Generic;
using IFix;
using System;
using System.Reflection;
using System.Linq;
//配置標籤
[Configure]
public class GameConfig1
{
[IFix]
static IEnumerable<Type> hotfix
{
get
{
UnityEngine.Debug.Log("GameConfig1GameConfig1");
return (from type in Assembly.Load("Assembly-CSharp").GetTypes()
where type.Namespace == "YourGameName"
select type).ToList();
}
}
}
注意配置類必須打上[Configure]標籤,屬性必須打IFix標籤並且聲明爲 **static** 。
IFixEditor.cs代碼內主要涉及兩個方法,一個是fix生成patch文件的的方法,一個是inject注入版本的方法。默認的patch文件生成路徑和core文件路徑可以修改,位置如下:
public static void Patch()
{
foreach (var assembly in injectAssemblys)
{
GenPatch(assembly, string.Format("./Library/ScriptAssemblies/{0}.dll", assembly),
IFIXCORE_PATH, string.Format(PATCH_PATH+"{0}.patch.bytes", assembly));
}
}
三,生成補丁
在需要生成補丁的類的響應方法前,加上[IFix.Patch]標籤,之後將代碼邏輯修改爲正確邏輯,點擊InjectFix/Fix按鈕,會在上面配置好的路徑下生成Assembly-CSharp.patch.bytes補丁文件。然後將代碼重新改回錯誤邏輯,點擊點擊InjectFix/Inject注入版本,此時運行項目,執行以下方法,加載響應的補丁(前提是Assembly-CSharp.patch.bytes文件放在了待加載路徑),會發現執行結果是修改後的正確邏輯,加載方法如下:
public static class IFixManager
{
const string PATCH_PATH = "Assets/SharedAssets/GamePatch/";
//遊戲加載時調用
public static void LoadPatches(Framework.Resource.ResourceManager m_ResManager)
{
var patch = m_ResManager.Load<TextAsset>(PATCH_PATH+"Assembly-CSharp.patch.bytes");
if (patch != null)
{
UnityEngine.Debug.Log("loading Assembly-CSharp.patch ...");
var sw = Stopwatch.StartNew();
PatchManager.Load(new MemoryStream(patch.bytes));
UnityEngine.Debug.Log("patch Assembly-CSharp.patch, using " + sw.ElapsedMilliseconds + " ms");
}
patch = m_ResManager.Load<TextAsset>(PATCH_PATH + "Assembly-CSharp-firstpass.patch.bytes");
if (patch != null)
{
UnityEngine.Debug.Log("loading Assembly-CSharp-firstpass ...");
var sw = Stopwatch.StartNew();
PatchManager.Load(new MemoryStream(patch.bytes));
UnityEngine.Debug.Log("patch Assembly-CSharp-firstpass, using " + sw.ElapsedMilliseconds + " ms");
}
}
}
具體插件和相關描述文件都是由XLua作者開發的,也開源了,文件地址如下:
https://download.csdn.net/download/qq302756113/11819787
原文鏈接如下:
https://segmentfault.com/a/1190000020375313
------------------------------------------------------------------------------------------------------------------------------------------------------------------
假如補丁內有A類A方法,但是配置文件裏面並沒有配置這個A類:
在Editor下運行,程序是會修復的,也就是執行補丁內的A方法;但是打包至手機端,則會報錯。
個人猜測在Editor下C#應該會通過反射去補丁內找到該方法,但是手機端則不會。