# HotFixFramework **Repository Path**: raycaster/HotFixFramework ## Basic Information - **Project Name**: HotFixFramework - **Description**: 一个基于ilruntime和kbengine的unity热更新方案 - **Primary Language**: C# - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2019-05-21 - **Last Updated**: 2021-11-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # HotFixFramework #### Description 一个基于ilruntime和kbengine的unity热更新方案 #### Software Architecture HotFixPlugins:启动并初始化热更新模块 ILRTPlugins:与ILRuntime相关的模块 KBEnginePlugins: KBEngine的客户端插件模块,负责与KBEngine服务器通信 GamePlugins: 提供UI支持,游戏策划配置数据初始 依赖关系: KBEnginePlugins ---> ILRTPlugins ---> HotFixPlugins GamePlugins ---> ILRTPlugins ---> HotFixPlugins #### Installation 直接将HotFixFramework放入unity assets下即可 #### Instructions 1.使用方式如下: 场景中构建一个空物体,编写一个如下所示GameStart脚本继承MonoBehaviour并将其挂载在空物体上,即可完成热更流程。 using System.Collections; using System.Collections.Generic; using UnityEngine; using HotFixFramework; public class GameStart : MonoBehaviour { public GameObject GameMain; //资源更新或游戏初始化进度条 private UISlider updateSlider; //资源更新下载速度 private UILabel speedLabel; //记录上个时间点已下载的资源大小,用于计算下载速度 private int preDownloadedBytes; //两次资源下载记录大小的时间间隔 private float deltaTime = 0; // Use this for initialization void Start() { updateSlider = GameMain.transform.Find("Background/SlideSlot").GetComponent(); speedLabel = GameMain.transform.Find("Background/UpdateSpeed").GetComponent(); var config = new GlobalConfig() { #if UNITY_EDITOR ResServerUrl = "file:///E:/MyProjectX/Client/GtCards/GameGems/Bundles/EditorAssetBundles/", #else ResServerUrl = "https://xxxxxxx/AndroidAssetBundles/", #endif ScriptPath = "dll/HotFix_Project", ConfigPath = "configs", ConfigRootFile = "json_content.json", IsDebug = true, EnableHotFix = true, }; HotFixMain.RegisterUpdateNotify(ShowUpdateNotify); HotFixMain.RegisterDownloadProcess(DownloadProcess); HotFixMain.Trigger.AddTriggerEvent(TriggerType.Notify, (o1, o2) =>{ string message = (string)o1; VisualLog.LogNotify(message); }); HotFixMain.Trigger.AddTriggerEvent(TriggerType.Error, (o1, o2) =>{ string message = (string)o1; VisualLog.LogError(message); }); HotFixMain.Trigger.AddTriggerEvent(TriggerType.Warnning, (o1, o2) =>{ string message = (string)o1; VisualLog.LogWarning(message); }); HotFixMain.Startup(config, (o1, o2) =>{ Debug.Log("Init All Finish: Start Gaming!!!!"); VisualLog.LogNotify("Init All Finish: Start Gaming!!!!"); updateSlider.value = 1; StartCoroutine(SwitchScene()); }); } // Update is called once per frame void Update() { } private void ShowUpdateNotify(object o1, object o2) { int totalUpdate = (int)o1; var udpatePopup = GameMain.transform.Find("Update"); udpatePopup.gameObject.SetActive(true); var label = GameMain.transform.Find("Update/Tips").GetComponent(); label.text = string.Format("有{0}的资源需要更新", FormatBytes(totalUpdate)); speedLabel.gameObject.SetActive(true); speedLabel.text = "0 B/s"; } private void DownloadProcess(object o1, object o2) { int totalUpdate = (int)o1; int downloadedBytes = (int)o2; updateSlider.value = (float)downloadedBytes/(float)totalUpdate; deltaTime += Time.deltaTime; //每隔0.5秒更新一次下载速度 if(deltaTime > 0.5f) { int deltaBytes = downloadedBytes - preDownloadedBytes; if(deltaBytes == 0) { speedLabel.text = "0 B/s"; return; } preDownloadedBytes = downloadedBytes; float speed = (float)deltaBytes / deltaTime; speedLabel.text = string.Format("{0}/s", FormatBytes((int)speed)); deltaTime = 0; } } private string FormatBytes(int bytes) { if (bytes < 1000) { return string.Format("{0} B", bytes); } else if (bytes >= 1000 && bytes < 1000000) { return string.Format("{0:F2} KB", (float)bytes / 1024); } else { return string.Format("{0:F2} MB", (float)bytes / (1024 * 1024)); } } private IEnumerator SwitchScene() { yield return new WaitForSeconds(1); AsyncOperation async = SceneManager.Ins.LoadSceneAsync("1_Login"); yield return async; } } 2.说明: 默认热更新打包资源路径为assets下的_bundles文件夹下,可在HotFixConfigs下配置。 创建GloalConfigs对象: ResServerUrl: 热更新资源服务器地址 ScriptPath: _bundles目录下,ilruntime生成热更dll的相对路径 ConfigPath: _bundles目录下,游戏策划配置的相对路径(可选) ConfigRootFile: 游戏策划配置的根文件(可选) EnableHotFix : true 则资源加载时,是从PersistentDataPath(从ResServerUrl下载资源的本地存储路径)中读取。 false 则资源加载是,从_bundles文件夹下读取。 可选:(调用HotFixMain.Startup()之前可以注册一些函数) 1)注册下载进度回调,参数1,需要下载的资源的总字节数。 参数2,目前已下载的字节数 HotFixMain.RegisterDownloadProcess((Action action)) 2)注册初始化进度回调,参数1,初始化进度百分比 HotFixMain.RegisterInitProcess(Action action) 3)注册更新提示,参数1,需要下载的资源的总字节数 HotFixMain.RegisterUpdateNotify(Action action) 注意,如果注册此函数,则有新的热更包,会回调注册函数,如果玩家点击确定更新,需要手动调用HotFixMain.ConfirmUpdate(); 如果未注册此函数,则每次有新的热更新包,会自动进行下载更新。 调用HotFixConfig.Startup(GlobalConfig config, ActionfinishCallBack),将先前创建的GlobalConfig对象传入, 并指定热更完成的回调,一般为进入或者切换到游戏主场景