This commit is contained in:
2025-11-12 07:04:31 +08:00
parent 33a4742904
commit f615d8ddb0
68 changed files with 1388 additions and 1478 deletions

View File

@@ -6,83 +6,84 @@ using System;
using System.Collections.Generic;
using UnityEditor.Build.Pipeline;
public class BuildTool
namespace Tuan.GameFramework
{
[MenuItem("Tools/打包Preload")]
public static void BuildPreload()
public class BuildTool
{
CopyHotDll.CopyPreloadDll2Byte();
ExecuteBuild("Preload",EBuildPipeline.ScriptableBuildPipeline, EditorUserBuildSettings.activeBuildTarget,EFileNameStyle.BundleName,EBuildinFileCopyOption.ClearAndCopyAll);
Debug.Log($"打包Preload结束");
}
[MenuItem("Tools/打包Main %G")]
public static void BuildMain()
{
CopyHotDll.CopyMainDll2Byte();
ExecuteBuild("Main", EBuildPipeline.ScriptableBuildPipeline, EditorUserBuildSettings.activeBuildTarget, EFileNameStyle.BundleName, EBuildinFileCopyOption.None);
Debug.Log($"打包Main结束");
}
[MenuItem("Tools/全部打包")]
public static void BuildAll()
{
BuildPreload();
BuildMain();
}
[MenuItem("Tools/打包Preload")]
public static void BuildPreload()
{
CopyHotDll.CopyPreloadDll2Byte();
ExecuteBuild("Preload", EBuildPipeline.ScriptableBuildPipeline, EditorUserBuildSettings.activeBuildTarget, EFileNameStyle.BundleName, EBuildinFileCopyOption.ClearAndCopyAll);
Debug.Log($"打包Preload结束");
}
[MenuItem("Tools/打包Main %G")]
public static void BuildMain()
{
CopyHotDll.CopyMainDll2Byte();
ExecuteBuild("Main", EBuildPipeline.ScriptableBuildPipeline, EditorUserBuildSettings.activeBuildTarget, EFileNameStyle.BundleName, EBuildinFileCopyOption.None);
Debug.Log($"打包Main结束");
}
[MenuItem("Tools/全部打包")]
public static void BuildAll()
{
BuildPreload();
BuildMain();
}
public static void ExecuteBuild(string PackageName, EBuildPipeline BuildPipeline, BuildTarget BuildTarget, EFileNameStyle fileNameStyle, EBuildinFileCopyOption buildinFileCopyOption)
{
var buildinFileCopyParams = AssetBundleBuilderSetting.GetPackageBuildinFileCopyParams(PackageName, BuildPipeline.ToString());
var compressOption = AssetBundleBuilderSetting.GetPackageCompressOption(PackageName, BuildPipeline.ToString());
var clearBuildCache = AssetBundleBuilderSetting.GetPackageClearBuildCache(PackageName, BuildPipeline.ToString());
var useAssetDependencyDB = AssetBundleBuilderSetting.GetPackageUseAssetDependencyDB(PackageName, BuildPipeline.ToString());
var builtinShaderBundleName = GetBuiltinShaderBundleName(PackageName);
public static void ExecuteBuild(string PackageName, EBuildPipeline BuildPipeline, BuildTarget BuildTarget, EFileNameStyle fileNameStyle, EBuildinFileCopyOption buildinFileCopyOption)
{
var buildinFileCopyParams = AssetBundleBuilderSetting.GetPackageBuildinFileCopyParams(PackageName, BuildPipeline.ToString());
var compressOption = AssetBundleBuilderSetting.GetPackageCompressOption(PackageName, BuildPipeline.ToString());
var clearBuildCache = AssetBundleBuilderSetting.GetPackageClearBuildCache(PackageName, BuildPipeline.ToString());
var useAssetDependencyDB = AssetBundleBuilderSetting.GetPackageUseAssetDependencyDB(PackageName, BuildPipeline.ToString());
var builtinShaderBundleName = GetBuiltinShaderBundleName(PackageName);
ScriptableBuildParameters buildParameters = new ScriptableBuildParameters();
buildParameters.BuildOutputRoot = AssetBundleBuilderHelper.GetDefaultBuildOutputRoot();
buildParameters.BuildinFileRoot = AssetBundleBuilderHelper.GetStreamingAssetsRoot();
buildParameters.BuildPipeline = BuildPipeline.ToString();
buildParameters.BuildBundleType = (int)EBuildBundleType.AssetBundle;
buildParameters.BuildTarget = BuildTarget;
buildParameters.PackageName = PackageName;
buildParameters.PackageVersion = GetPackageVersion();
buildParameters.EnableSharePackRule = true;
buildParameters.VerifyBuildingResult = true;
buildParameters.FileNameStyle = fileNameStyle;
buildParameters.BuildinFileCopyOption = buildinFileCopyOption;
buildParameters.BuildinFileCopyParams = buildinFileCopyParams;
buildParameters.CompressOption = compressOption;
buildParameters.ClearBuildCacheFiles = clearBuildCache;
buildParameters.UseAssetDependencyDB = useAssetDependencyDB;
buildParameters.BuiltinShadersBundleName = builtinShaderBundleName;
buildParameters.EncryptionServices = CreateEncryptionInstance(PackageName, BuildPipeline);
ScriptableBuildParameters buildParameters = new ScriptableBuildParameters();
buildParameters.BuildOutputRoot = AssetBundleBuilderHelper.GetDefaultBuildOutputRoot();
buildParameters.BuildinFileRoot = AssetBundleBuilderHelper.GetStreamingAssetsRoot();
buildParameters.BuildPipeline = BuildPipeline.ToString();
buildParameters.BuildBundleType = (int)EBuildBundleType.AssetBundle;
buildParameters.BuildTarget = BuildTarget;
buildParameters.PackageName = PackageName;
buildParameters.PackageVersion = GetPackageVersion();
buildParameters.EnableSharePackRule = true;
buildParameters.VerifyBuildingResult = true;
buildParameters.FileNameStyle = fileNameStyle;
buildParameters.BuildinFileCopyOption = buildinFileCopyOption;
buildParameters.BuildinFileCopyParams = buildinFileCopyParams;
buildParameters.CompressOption = compressOption;
buildParameters.ClearBuildCacheFiles = clearBuildCache;
buildParameters.UseAssetDependencyDB = useAssetDependencyDB;
buildParameters.BuiltinShadersBundleName = builtinShaderBundleName;
buildParameters.EncryptionServices = CreateEncryptionInstance(PackageName, BuildPipeline);
ScriptableBuildPipeline pipeline = new ScriptableBuildPipeline();
var buildResult = pipeline.Run(buildParameters, true);
if (buildResult.Success)
EditorUtility.RevealInFinder(buildResult.OutputPackageDirectory);
ScriptableBuildPipeline pipeline = new ScriptableBuildPipeline();
var buildResult = pipeline.Run(buildParameters, true);
if (buildResult.Success)
EditorUtility.RevealInFinder(buildResult.OutputPackageDirectory);
}
public static string GetPackageVersion()
{
int totalMinutes = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
return DateTime.Now.ToString("yyyy-MM-dd") + "-" + totalMinutes;
}
private static string GetBuiltinShaderBundleName(string PackageName)
{
var uniqueBundleName = AssetBundleCollectorSettingData.Setting.UniqueBundleName;
var packRuleResult = DefaultPackRule.CreateShadersPackRuleResult();
return packRuleResult.GetBundleName(PackageName, uniqueBundleName);
}
public static IEncryptionServices CreateEncryptionInstance(string PackageName, EBuildPipeline BuildPipeline)
{
var encyptionClassName = AssetBundleBuilderSetting.GetPackageEncyptionServicesClassName(PackageName, BuildPipeline.ToString());
var encryptionClassTypes = EditorTools.GetAssignableTypes(typeof(IEncryptionServices));
var classType = encryptionClassTypes.Find(x => x.FullName.Equals(encyptionClassName));
if (classType != null)
return (IEncryptionServices)Activator.CreateInstance(classType);
else
return null;
}
}
public static string GetPackageVersion()
{
int totalMinutes = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
return DateTime.Now.ToString("yyyy-MM-dd") + "-" + totalMinutes;
}
private static string GetBuiltinShaderBundleName(string PackageName)
{
var uniqueBundleName = AssetBundleCollectorSettingData.Setting.UniqueBundleName;
var packRuleResult = DefaultPackRule.CreateShadersPackRuleResult();
return packRuleResult.GetBundleName(PackageName, uniqueBundleName);
}
public static IEncryptionServices CreateEncryptionInstance(string PackageName, EBuildPipeline BuildPipeline)
{
var encyptionClassName = AssetBundleBuilderSetting.GetPackageEncyptionServicesClassName(PackageName, BuildPipeline.ToString());
var encryptionClassTypes = EditorTools.GetAssignableTypes(typeof(IEncryptionServices));
var classType = encryptionClassTypes.Find(x => x.FullName.Equals(encyptionClassName));
if (classType != null)
return (IEncryptionServices)Activator.CreateInstance(classType);
else
return null;
}
}
}

View File

@@ -2,58 +2,60 @@ using UnityEditor;
using UnityEngine;
using System.IO;
public class CopyHotDll
namespace Tuan.GameFramework
{
[MenuItem("Tools/更新生成PreloadDll")]
public static void CopyPreloadDll2Byte()
public class CopyHotDll
{
HybridCLR.Editor.Commands.CompileDllCommand.CompileDllActiveBuildTarget();
string sourceDir = $"{Application.dataPath.Replace("/Assets", "")}/HybridCLRData/HotUpdateDlls/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/GameScripts.Preload.dll";
string destDir = $"{Application.dataPath}/GameRes/Preload/HotUpdateDll/GameScripts.Preload.bytes";
if (File.Exists(destDir))
[MenuItem("Tools/更新生成PreloadDll")]
public static void CopyPreloadDll2Byte()
{
File.Delete(destDir);
}
File.Copy(sourceDir, destDir);
AssetDatabase.Refresh();
Debug.Log($"copy {sourceDir} to {destDir}");
}
[MenuItem("Tools/更新生成MainDll")]
public static void CopyMainDll2Byte()
{
HybridCLR.Editor.Commands.CompileDllCommand.CompileDllActiveBuildTarget();
string sourceDir = $"{Application.dataPath.Replace("/Assets", "")}/HybridCLRData/HotUpdateDlls/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/GameScripts.Main.dll";
string destDir = $"{Application.dataPath}/GameRes/Main/HotUpdateDll/GameScripts.Main.bytes";
if (File.Exists(destDir))
{
File.Delete(destDir);
}
File.Copy(sourceDir, destDir);
AssetDatabase.Refresh();
Debug.Log($"copy {sourceDir} to {destDir}");
}
[MenuItem("Tools/更新生成补充数据源")]
public static void CopyDepDll2Byte()
{
HybridCLR.Editor.Commands.CompileDllCommand.CompileDllActiveBuildTarget();
string sourceDir = $"{Application.dataPath.Replace("/Assets", "")}/HybridCLRData/AssembliesPostIl2CppStrip/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/";
string destDir = $"{Application.dataPath}/GameRes/Main/HotUpdateDll/";
foreach (string dll in HotDllLoader.Inst.DepDlls)
{
string sourcePath = $"{sourceDir}/{dll}";
string destPath = $"{destDir}/{dll}.bytes";
if (File.Exists(sourcePath))
HybridCLR.Editor.Commands.CompileDllCommand.CompileDllActiveBuildTarget();
string sourceDir = $"{Application.dataPath.Replace("/Assets", "")}/HybridCLRData/HotUpdateDlls/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/GameScripts.Preload.dll";
string destDir = $"{Application.dataPath}/GameRes/Preload/HotUpdateDll/GameScripts.Preload.bytes";
if (File.Exists(destDir))
{
if (File.Exists(destPath))
{
File.Delete(destPath);
}
File.Copy(sourcePath, destPath);
AssetDatabase.Refresh();
Debug.Log($"copy {sourcePath} to {destPath}");
File.Delete(destDir);
}
File.Copy(sourceDir, destDir);
AssetDatabase.Refresh();
Debug.Log($"copy {sourceDir} to {destDir}");
}
[MenuItem("Tools/更新生成MainDll")]
public static void CopyMainDll2Byte()
{
HybridCLR.Editor.Commands.CompileDllCommand.CompileDllActiveBuildTarget();
string sourceDir = $"{Application.dataPath.Replace("/Assets", "")}/HybridCLRData/HotUpdateDlls/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/GameScripts.Main.dll";
string destDir = $"{Application.dataPath}/GameRes/Main/HotUpdateDll/GameScripts.Main.bytes";
if (File.Exists(destDir))
{
File.Delete(destDir);
}
File.Copy(sourceDir, destDir);
AssetDatabase.Refresh();
Debug.Log($"copy {sourceDir} to {destDir}");
}
[MenuItem("Tools/更新生成补充数据源")]
public static void CopyDepDll2Byte()
{
HybridCLR.Editor.Commands.CompileDllCommand.CompileDllActiveBuildTarget();
string sourceDir = $"{Application.dataPath.Replace("/Assets", "")}/HybridCLRData/AssembliesPostIl2CppStrip/{UnityEditor.EditorUserBuildSettings.activeBuildTarget}/";
string destDir = $"{Application.dataPath}/GameRes/Main/HotUpdateDll/";
foreach (string dll in HotDllLoader.Inst.DepDlls)
{
string sourcePath = $"{sourceDir}/{dll}";
string destPath = $"{destDir}/{dll}.bytes";
if (File.Exists(sourcePath))
{
if (File.Exists(destPath))
{
File.Delete(destPath);
}
File.Copy(sourcePath, destPath);
AssetDatabase.Refresh();
Debug.Log($"copy {sourcePath} to {destPath}");
}
}
Debug.Log("copy over");
}
Debug.Log("copy over");
}
}
}

View File

@@ -0,0 +1,15 @@
using Cysharp.Threading.Tasks;
using UnityEngine;
using YooAsset;
namespace Tuan.GameFramework
{
public class Asset : MonoBehaviour
{
async UniTask LoadAsync(string name)
{
AssetHandle handle = YooAssets.LoadAssetAsync(name);
await handle.ToUniTask();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: d9170e9f660395d47b0f02466e5fd091

View File

@@ -2,34 +2,37 @@ using Cysharp.Threading.Tasks;
using UnityEngine;
using YooAsset;
public class Boot : MonoBehaviour
namespace Tuan.GameFramework
{
public Camera MainCamera;
public EPlayMode PlayMode = EPlayMode.EditorSimulateMode;
void Awake()
public class Boot : MonoBehaviour
{
#if UNITY_EDITOR
//PlayerPrefs.DeleteAll();
#endif
Application.targetFrameRate = 60;
Application.runInBackground = true;
DontDestroyOnLoad(MainCamera);
}
async void Start()
{
bool updateSuccess = await PatchManager.Inst.StartOperation(PlayMode);
if (updateSuccess)
await EnterGame();
}
private async UniTask EnterGame()
{
Debug.Log("EnterGame");
var assetHandle = YooAssets.TryGetPackage("Main").LoadSceneAsync("Test");
await assetHandle.ToUniTask();
if (assetHandle.Status == EOperationStatus.Succeed)
public Camera MainCamera;
public EPlayMode PlayMode = EPlayMode.EditorSimulateMode;
void Awake()
{
assetHandle.ActivateScene();
PatchEvent.ClosePatchWindow();
#if UNITY_EDITOR
//PlayerPrefs.DeleteAll();
#endif
Application.targetFrameRate = 60;
Application.runInBackground = true;
DontDestroyOnLoad(MainCamera);
}
async void Start()
{
bool updateSuccess = await PatchManager.Inst.StartOperation(PlayMode);
if (updateSuccess)
await EnterGame();
}
private async UniTask EnterGame()
{
Debug.Log("EnterGame");
var assetHandle = YooAssets.TryGetPackage("Main").LoadSceneAsync("Test");
await assetHandle.ToUniTask();
if (assetHandle.Status == EOperationStatus.Succeed)
{
assetHandle.ActivateScene();
PatchEvent.ClosePatchWindow();
}
}
}
}
}

View File

@@ -2,87 +2,90 @@ using System.Collections.Generic;
using System;
using UnityEngine;
public static class EventBus
namespace Tuan.GameFramework
{
private static readonly Dictionary<Type, object> _eventHandlers = new Dictionary<Type, object>();
private static readonly object _lock = new object();
public static void Register<TEvent>(IEventHandler<TEvent> handler) where TEvent : IEvent
public static class EventBus
{
lock (_lock)
{
Type eventType = typeof(TEvent);
if (!_eventHandlers.ContainsKey(eventType))
{
_eventHandlers[eventType] = new List<IEventHandler<TEvent>>();
}
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
if (handler != null && !handlers.Contains(handler))
{
handlers.Add(handler);
}
}
}
private static readonly Dictionary<Type, object> _eventHandlers = new Dictionary<Type, object>();
private static readonly object _lock = new object();
public static void Unregister<TEvent>(IEventHandler<TEvent> handler) where TEvent : IEvent
{
lock (_lock)
public static void Register<TEvent>(IEventHandler<TEvent> handler) where TEvent : IEvent
{
Type eventType = typeof(TEvent);
if (_eventHandlers.ContainsKey(eventType))
lock (_lock)
{
Type eventType = typeof(TEvent);
if (!_eventHandlers.ContainsKey(eventType))
{
_eventHandlers[eventType] = new List<IEventHandler<TEvent>>();
}
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
handlers?.Remove(handler);
if (handlers != null && handlers.Count == 0)
if (handler != null && !handlers.Contains(handler))
{
_eventHandlers.Remove(eventType);
handlers.Add(handler);
}
}
}
}
public static void Publish<TEvent>(TEvent eventData) where TEvent : IEvent
{
List<IEventHandler<TEvent>> handlersToInvoke = null;
lock (_lock)
public static void Unregister<TEvent>(IEventHandler<TEvent> handler) where TEvent : IEvent
{
Type eventType = typeof(TEvent);
if (_eventHandlers.ContainsKey(eventType))
lock (_lock)
{
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
if (handlers != null && handlers.Count > 0)
Type eventType = typeof(TEvent);
if (_eventHandlers.ContainsKey(eventType))
{
handlersToInvoke = new List<IEventHandler<TEvent>>(handlers);
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
handlers?.Remove(handler);
if (handlers != null && handlers.Count == 0)
{
_eventHandlers.Remove(eventType);
}
}
}
}
// 在锁外执行事件处理,避免死锁
if (handlersToInvoke != null)
public static void Publish<TEvent>(TEvent eventData) where TEvent : IEvent
{
foreach (var handler in handlersToInvoke)
List<IEventHandler<TEvent>> handlersToInvoke = null;
lock (_lock)
{
try
Type eventType = typeof(TEvent);
if (_eventHandlers.ContainsKey(eventType))
{
handler?.HandleEvent(eventData);
var handlers = _eventHandlers[eventType] as List<IEventHandler<TEvent>>;
if (handlers != null && handlers.Count > 0)
{
handlersToInvoke = new List<IEventHandler<TEvent>>(handlers);
}
}
catch (Exception e)
}
// 在锁外执行事件处理,避免死锁
if (handlersToInvoke != null)
{
foreach (var handler in handlersToInvoke)
{
Debug.LogError($"Event handling error in {handler.GetType().Name}: {e}");
try
{
handler?.HandleEvent(eventData);
}
catch (Exception e)
{
Debug.LogError($"Event handling error in {handler.GetType().Name}: {e}");
}
}
}
}
}
public static void Clear()
{
lock (_lock)
public static void Clear()
{
_eventHandlers.Clear();
lock (_lock)
{
_eventHandlers.Clear();
}
}
}
}
}

View File

@@ -1,27 +1,30 @@
public interface IEvent { }
public interface IEventHandler<TEvent> where TEvent : IEvent
namespace Tuan.GameFramework
{
void HandleEvent(TEvent eventData);
}
public interface IEvent { }
public struct ApplicationFocusEvent : IEvent
{
public bool HasFocus;
}
public struct SceneLoadEvent : IEvent
{
public string SceneName;
public float Progress;
}
public abstract class EventHandler<TEvent> : IEventHandler<TEvent> where TEvent : IEvent
{
public void HandleEvent(TEvent eventData)
public interface IEventHandler<TEvent> where TEvent : IEvent
{
OnEvent(eventData);
void HandleEvent(TEvent eventData);
}
protected abstract void OnEvent(TEvent eventData);
}
public struct ApplicationFocusEvent : IEvent
{
public bool HasFocus;
}
public struct SceneLoadEvent : IEvent
{
public string SceneName;
public float Progress;
}
public abstract class EventHandler<TEvent> : IEventHandler<TEvent> where TEvent : IEvent
{
public void HandleEvent(TEvent eventData)
{
OnEvent(eventData);
}
protected abstract void OnEvent(TEvent eventData);
}
}

View File

@@ -5,7 +5,8 @@
"GUID:e34a5702dd353724aa315fb8011f08c3",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:3fe1a3e70da50184f9897101cad7e4f2",
"GUID:13ba8ce62aa80c74598530029cb2d649"
"GUID:13ba8ce62aa80c74598530029cb2d649",
"GUID:928f8a513cd12e84cb0d3c0a21a84e2f"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@@ -1,6 +1,9 @@
using UnityEngine;
public class GameManager : Singleton<GameManager>
namespace Tuan.GameFramework
{
public class GameManager : Singleton<GameManager>
{
}
}

View File

@@ -1,15 +1,18 @@
using UnityEngine;
public class MainUICanvas : SingletonMono<MainUICanvas>
namespace Tuan.GameFramework
{
public GameObject InitBg;
public RectTransform Top;
public RectTransform Medium;
public RectTransform Bottom;
public Camera UICamera;
private void Awake()
public class MainUICanvas : SingletonMono<MainUICanvas>
{
DontDestroyOnLoad(gameObject);
DontDestroyOnLoad(UICamera);
public GameObject InitBg;
public RectTransform Top;
public RectTransform Medium;
public RectTransform Bottom;
public Camera UICamera;
private void Awake()
{
DontDestroyOnLoad(gameObject);
DontDestroyOnLoad(UICamera);
}
}
}
}

View File

@@ -3,84 +3,87 @@ using UnityEngine.UI;
using System;
using System.Collections.Generic;
public class MessageBox : MonoBehaviour
namespace Tuan.GameFramework
{
[SerializeField] private GameObject panel;
[SerializeField] private Text titleText;
[SerializeField] private Text contentText;
[SerializeField] private Transform buttonsParent;
[SerializeField] private GameObject buttonPrefab;
private static readonly List<MessageBox> hiddenMessageBoxes = new List<MessageBox>();
private readonly List<Button> createdButtons = new List<Button>();
public static MessageBox Show()
public class MessageBox : MonoBehaviour
{
[SerializeField] private GameObject panel;
[SerializeField] private Text titleText;
[SerializeField] private Text contentText;
[SerializeField] private Transform buttonsParent;
[SerializeField] private GameObject buttonPrefab;
if (hiddenMessageBoxes.Count > 0)
private static readonly List<MessageBox> hiddenMessageBoxes = new List<MessageBox>();
private readonly List<Button> createdButtons = new List<Button>();
public static MessageBox Show()
{
MessageBox box = hiddenMessageBoxes[0];
hiddenMessageBoxes.RemoveAt(0);
box.gameObject.SetActive(true);
box.panel.SetActive(true);
return box;
if (hiddenMessageBoxes.Count > 0)
{
MessageBox box = hiddenMessageBoxes[0];
hiddenMessageBoxes.RemoveAt(0);
box.gameObject.SetActive(true);
box.panel.SetActive(true);
return box;
}
var prefab = Resources.Load<GameObject>("MessageBox");
if (!prefab)
{
Debug.LogError("MessageBox prefab not found in Resources");
return null;
}
var go = Instantiate(prefab, MainUICanvas.Inst.Top);
go.name = "MessageBox";
var messageBox = go.GetComponent<MessageBox>();
return messageBox;
}
var prefab = Resources.Load<GameObject>("MessageBox");
if (!prefab)
public void Hide()
{
Debug.LogError("MessageBox prefab not found in Resources");
return null;
panel.SetActive(false);
ClearButtons();
gameObject.SetActive(false);
hiddenMessageBoxes.Add(this);
}
var go = Instantiate(prefab, MainUICanvas.Inst.Top);
go.name = "MessageBox";
var messageBox = go.GetComponent<MessageBox>();
return messageBox;
}
public void Hide()
{
panel.SetActive(false);
ClearButtons();
gameObject.SetActive(false);
hiddenMessageBoxes.Add(this);
}
private void ClearButtons()
{
foreach (var button in createdButtons)
private void ClearButtons()
{
if (button != null) Destroy(button.gameObject);
foreach (var button in createdButtons)
{
if (button != null) Destroy(button.gameObject);
}
createdButtons.Clear();
}
createdButtons.Clear();
}
public MessageBox SetTitle(string title)
{
titleText.text = title;
return this;
}
public MessageBox SetContent(string content)
{
contentText.text = content;
return this;
}
public MessageBox AddButton(string text, Action<MessageBox> onClick = null)
{
var button = Instantiate(buttonPrefab, buttonsParent).GetComponent<Button>();
button.GetComponentInChildren<Text>().text = text;
button.onClick.AddListener(() =>
public MessageBox SetTitle(string title)
{
onClick?.Invoke(this);
Hide();
});
titleText.text = title;
return this;
}
createdButtons.Add(button);
button.gameObject.SetActive(true);
return this;
public MessageBox SetContent(string content)
{
contentText.text = content;
return this;
}
public MessageBox AddButton(string text, Action<MessageBox> onClick = null)
{
var button = Instantiate(buttonPrefab, buttonsParent).GetComponent<Button>();
button.GetComponentInChildren<Text>().text = text;
button.onClick.AddListener(() =>
{
onClick?.Invoke(this);
Hide();
});
createdButtons.Add(button);
button.gameObject.SetActive(true);
return this;
}
}
}

View File

@@ -0,0 +1,46 @@
using Cysharp.Threading.Tasks;
using HybridCLR;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using YooAsset;
namespace Tuan.GameFramework
{
public class HotDllLoader : Singleton<HotDllLoader>
{
public List<string> DepDlls = new List<string>()
{
"mscorlib.dll",
"System.dll",
"System.Core.dll",
};
public async UniTask LoadDll(ResourcePackage package, string dll)
{
if (package.GetAssetInfo(dll).Error == string.Empty)
{
AssetHandle handle = package.LoadAssetAsync<TextAsset>(dll);
await handle.ToUniTask();
#if UNITY_EDITOR
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == dll.Replace(".dll", ""));
#else
Assembly hotUpdateAss = Assembly.Load((handle.AssetObject as TextAsset).bytes);
#endif
Debug.Log($"加载{dll}");
}
}
public async UniTask LoadDepDll(ResourcePackage package)
{
foreach (string dll in DepDlls)
{
if (package.GetAssetInfo(dll).Error == string.Empty)
{
AssetHandle handle = package.LoadAssetAsync<TextAsset>(dll);
await handle.ToUniTask();
RuntimeApi.LoadMetadataForAOTAssembly((handle.AssetObject as TextAsset).bytes, HomologousImageMode.SuperSet);
}
}
}
}
}

View File

@@ -0,0 +1,65 @@
using Cysharp.Threading.Tasks;
using YooAsset;
namespace Tuan.GameFramework
{
public class MainOperation
{
PatchOperationData data;
PatchOperation operation;
bool autoDownload;
public MainOperation(EPlayMode playMode, bool autoDownload = false)
{
data = new PatchOperationData();
data.packageName = "Main";
data.playMode = playMode;
data.useBuildinFileSystem = false;
data.downloadingMaxNum = 10;
data.failedTryAgain = 3;
data.downloadUpdate = OnDownloadUpdate;
data.downloadFinish = OnDownloadFinish;
data.downloadError = OnDownloadError;
operation = new PatchOperation(data);
this.autoDownload = autoDownload;
}
public async UniTask Execute()
{
PatchEvent.UpdateProgress(0f);
if (!await operation.InitializePackage()) return;
if (!await operation.RequestPackageVersion()) return;
if (!await operation.UpdatePackageManifest()) return;
if (operation.CreateDownloader())
{
if (autoDownload)
{
if (!await operation.DownloadPackageFiles()) return;
}
else
{
if (!await operation.CheckDownloadOrSkip()) return;
}
}
if (!await operation.ClearCacheBundle()) return;
operation.SaveVersionToCache();
YooAssets.SetDefaultPackage(operation.package);
}
private void OnDownloadUpdate(DownloadUpdateData downloadUpdateData)
{
float progress = (float)downloadUpdateData.CurrentDownloadBytes / downloadUpdateData.TotalDownloadBytes;
string sizeText = $"{operation.FormatFileSize(downloadUpdateData.CurrentDownloadBytes)} / {operation.FormatFileSize(downloadUpdateData.TotalDownloadBytes)}";
PatchEvent.UpdateProgress(progress);
PatchEvent.UpdateDownloadSize(sizeText);
PatchEvent.UpdateStatus($"{data.packageName} 资源下载中...");
}
private void OnDownloadFinish(DownloaderFinishData downloaderFinishData)
{
PatchEvent.UpdateStatus("下载完成");
}
private void OnDownloadError(DownloadErrorData downloadErrorData)
{
PatchEvent.UpdateStatus($"下载失败:{downloadErrorData.FileName}\n{downloadErrorData.ErrorInfo}");
}
}
}

View File

@@ -0,0 +1,31 @@
using System;
namespace Tuan.GameFramework
{
public static class PatchEvent
{
public static event Action<string> OnStatusUpdate;
public static event Action<float> OnProgressUpdate;
public static event Action<string> OnDownloadSizeUpdate;
public static event Action OnClosePatchWindow;
public static void UpdateStatus(string status)
{
OnStatusUpdate?.Invoke(status);
}
public static void UpdateProgress(float progress)
{
OnProgressUpdate?.Invoke(progress);
}
public static void UpdateDownloadSize(string sizeText)
{
OnDownloadSizeUpdate?.Invoke(sizeText);
}
public static void ClosePatchWindow()
{
OnClosePatchWindow?.Invoke();
}
}
}

View File

@@ -0,0 +1,22 @@
using Cysharp.Threading.Tasks;
using YooAsset;
namespace Tuan.GameFramework
{
public class PatchManager : Singleton<PatchManager>
{
public async UniTask<bool> StartOperation(EPlayMode playMode)
{
YooAssets.Initialize();
PreloadOperation _preloadOperation = new PreloadOperation(playMode);
await _preloadOperation.Execute();
MainOperation _mainOperation = new MainOperation(playMode);
await _mainOperation.Execute();
await HotDllLoader.Inst.LoadDepDll(YooAssets.GetPackage("Main"));
await HotDllLoader.Inst.LoadDll(YooAssets.GetPackage("Main"), "GameScripts.Main");
return true;
}
}
}

View File

@@ -0,0 +1,362 @@
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;
using static YooAsset.DownloaderOperation;
namespace Tuan.GameFramework
{
public class PatchOperationData
{
public string packageName;
public EPlayMode playMode;
public bool useBuildinFileSystem;
public DownloadError downloadError;
public DownloaderFinish downloadFinish;
public DownloadUpdate downloadUpdate;
public int downloadingMaxNum = 10;
public int failedTryAgain = 3;
}
public class PatchOperation
{
public PatchOperationData data;
public ResourcePackage package;
public ResourceDownloaderOperation downloader;
public string packageVersion;
public string PkgVersionKey;
public PatchOperation(PatchOperationData data)
{
this.data = data;
PkgVersionKey = $"{Application.productName}_{data.packageName}";
#if !UNITY_EDITOR
PkgVersionKey = $"Editor_{Application.productName}_{data.packageName}";
#endif
}
#region
public async UniTask<bool> InitializePackage()
{
package = YooAssets.TryGetPackage(data.packageName);
if (package == null)
package = YooAssets.CreatePackage(data.packageName);
InitializationOperation initializationOperation = null;
switch (data.playMode)
{
case EPlayMode.EditorSimulateMode:
initializationOperation = InitializeEditorMode(package);
break;
case EPlayMode.OfflinePlayMode:
initializationOperation = InitializeOfflineMode(package);
break;
case EPlayMode.HostPlayMode:
initializationOperation = InitializeHostMode(package);
break;
case EPlayMode.WebPlayMode:
initializationOperation = InitializeWebPlayMode(package);
break;
case EPlayMode.CustomPlayMode:
break;
default:
break;
}
await initializationOperation.ToUniTask();
if (initializationOperation.Status != EOperationStatus.Succeed)
{
MessageBox.Show()
.SetTitle($"{data.packageName}初始化")
.SetContent($"{initializationOperation.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
else
{
PatchEvent.UpdateStatus($"初始化成功{data.packageName}");
Debug.Log($"初始化成功{data.packageName}");
}
return initializationOperation.Status == EOperationStatus.Succeed;
}
private InitializationOperation InitializeEditorMode(ResourcePackage package)
{
var buildResult = EditorSimulateModeHelper.SimulateBuild(data.packageName);
var packageRoot = buildResult.PackageRootDirectory;
var createParameters = new EditorSimulateModeParameters();
createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeOfflineMode(ResourcePackage package)
{
var createParameters = new OfflinePlayModeParameters();
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeHostMode(ResourcePackage package)
{
FileSystemParameters buildinFileSystemParams = null;
if (data.useBuildinFileSystem)
{
// 注意设置参数COPY_BUILDIN_PACKAGE_MANIFEST可以初始化的时候拷贝内置清单到沙盒目录
buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
}
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
// 注意设置参数INSTALL_CLEAR_MODE可以解决覆盖安装的时候将拷贝的内置清单文件清理的问题。
var cacheFileSystemParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
cacheFileSystemParams.AddParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, EOverwriteInstallClearMode.ClearAllManifestFiles);
var createParameters = new HostPlayModeParameters();
createParameters.BuildinFileSystemParameters = buildinFileSystemParams;
createParameters.CacheFileSystemParameters = cacheFileSystemParams;
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeWebPlayMode(ResourcePackage package)
{
#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR
var createParameters = new WebPlayModeParameters();
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
string packageRoot = $"{WeChatWASM.WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; //注意:如果有子目录,请修改此处!
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateFileSystemParameters(packageRoot, remoteServices);
return package.InitializeAsync(createParameters);
#else
var createParameters = new WebPlayModeParameters();
createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
return package.InitializeAsync(createParameters);
#endif
}
private string GetHostServerURL()
{
string hostServerIP = $"https://home.gtuantuan.online:9444/{Application.productName}";
string appVersion = "v1";
#if UNITY_EDITOR
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
// return $"{hostServerIP}/CDN/Android/{packageName}/{appVersion}";
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
return $"{hostServerIP}/CDN/IPhone/{data.packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
return $"{hostServerIP}/CDN/WebGL/{data.packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
#else
if (Application.platform == RuntimePlatform.Android)
return $"{hostServerIP}/CDN/Android/{data.packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.IPhonePlayer)
return $"{hostServerIP}/CDN/IPhone/{data.packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.WebGLPlayer)
return $"{hostServerIP}/CDN/WebGL/{data.packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
#endif
}
private class RemoteServices : IRemoteServices
{
private readonly string _defaultHostServer;
private readonly string _fallbackHostServer;
public RemoteServices(string defaultHostServer, string fallbackHostServer)
{
_defaultHostServer = defaultHostServer;
_fallbackHostServer = fallbackHostServer;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return $"{_defaultHostServer}/{fileName}";
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return $"{_fallbackHostServer}/{fileName}";
}
}
#endregion
#region
public async UniTask<bool> RequestPackageVersion(bool showBox = true)
{
var operation = package.RequestPackageVersionAsync(true, 5);
await operation.ToUniTask();
if (operation.Status != EOperationStatus.Succeed)
{
if (showBox)
{
if (await CheckUseLocalVersion(operation))
{
string cachedVersion = GetCachedPackageVersion();
if (!string.IsNullOrEmpty(cachedVersion))
{
packageVersion = cachedVersion;
return true;
}
}
}
}
else
{
packageVersion = operation.PackageVersion;
PatchEvent.UpdateStatus($"获取版本成功{data.packageName}");
Debug.Log($"获取版本成功{data.packageName}{packageVersion}");
}
return operation.Status == EOperationStatus.Succeed;
}
public async UniTask<bool> CheckUseLocalVersion(RequestPackageVersionOperation operation)
{
var completionSource = new UniTaskCompletionSource<bool>();
MessageBox.Show()
.SetTitle($"{data.packageName}请求版本")
.SetContent($"{operation.Error}")
.AddButton("继续", (box) =>
{
completionSource.TrySetResult(true);
})
.AddButton("退出", (box) =>
{
completionSource.TrySetResult(false);
Application.Quit();
});
bool shouldContinue = await completionSource.Task;
return shouldContinue;
}
public async Task<string> GetBuildinPackageVersion()
{
var operation = new GetBuildinPackageVersionOperation(data.packageName);
YooAssets.StartOperation(operation);
await operation;
if (operation.Status == EOperationStatus.Succeed)
{
return operation.PackageVersion;
}
else
{
return null;
}
}
public string GetCachedPackageVersion()
{
if (PlayerPrefs.HasKey(PkgVersionKey))
{
return PlayerPrefs.GetString(PkgVersionKey);
}
return null;
}
public void SaveVersionToCache()
{
PlayerPrefs.SetString(PkgVersionKey, packageVersion);
PlayerPrefs.Save();
PatchEvent.UpdateStatus($"更新完成{data.packageName}");
Debug.Log($"更新{data.packageName}完成,版本号{packageVersion}");
}
public async UniTask<bool> UpdatePackageManifest(bool showBox = true)
{
var operation = package.UpdatePackageManifestAsync(packageVersion, 10);
await operation.ToUniTask();
if (operation.Status != EOperationStatus.Succeed)
{
if (showBox)
{
MessageBox.Show()
.SetTitle($"{data.packageName}更新清单")
.SetContent($"{operation.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
}
else
{
PatchEvent.UpdateStatus($"获取资源清单成功{data.packageName}");
Debug.Log($"获取资源清单成功{data.packageName}");
}
return operation.Status == EOperationStatus.Succeed;
}
#endregion
#region
public bool CreateDownloader()
{
downloader = package.CreateResourceDownloader(data.downloadingMaxNum, data.failedTryAgain);
downloader.DownloadErrorCallback = data.downloadError;
downloader.DownloadFinishCallback = data.downloadFinish;
downloader.DownloadUpdateCallback = data.downloadUpdate;
if (downloader.TotalDownloadCount == 0) return false;
return true;
}
public async UniTask<bool> CheckDownloadOrSkip()
{
var completionSource = new UniTaskCompletionSource<bool>();
MessageBox.Show()
.SetTitle($"{data.packageName}发现更新")
.SetContent($"发现资源更新\n{GetCachedPackageVersion()}=>{packageVersion}: {FormatFileSize(downloader.TotalDownloadBytes)}")
.AddButton("下载", async (box) =>
{
bool success = await DownloadPackageFiles();
completionSource.TrySetResult(success);
})
.AddButton("跳过", async (box) =>
{
downloader.CancelDownload();
packageVersion = GetCachedPackageVersion();
await UpdatePackageManifest();
completionSource.TrySetResult(true);
})
.AddButton("退出", (box) =>
{
downloader.CancelDownload();
completionSource.TrySetResult(false);
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
});
bool shouldContinue = await completionSource.Task;
return shouldContinue;
}
public async UniTask<bool> DownloadPackageFiles(bool showBox = true)
{
if (downloader.TotalDownloadCount == 0)
return true;
Debug.Log($"{data.packageName} DownloadPackageFiles {downloader.TotalDownloadCount}");
downloader.BeginDownload();
await downloader.ToUniTask();
if (downloader.Status != EOperationStatus.Succeed)
{
if (showBox)
{
MessageBox.Show()
.SetTitle($"{data.packageName}下载文件")
.SetContent($"{downloader.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
return false;
}
return downloader.Status == EOperationStatus.Succeed;
}
public async UniTask<bool> ClearCacheBundle(bool showBox = true)
{
var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearUnusedBundleFiles);
await operation.ToUniTask();
if (operation.Status != EOperationStatus.Succeed)
{
if (showBox)
{
MessageBox.Show()
.SetTitle($"{data.packageName}清除缓存")
.SetContent($"{operation.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
}
return operation.Status == EOperationStatus.Succeed;
}
public string FormatFileSize(long size)
{
if (size < 1024 * 1024)
return $"{(size / 1024f):F1}KB";
else
return $"{size / 1024f / 1024f:F1}MB";
}
#endregion
}
}

View File

@@ -0,0 +1,91 @@
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;
namespace Tuan.GameFramework
{
public class PreloadOperation
{
PatchOperationData data;
PatchOperation operation;
public PreloadOperation(EPlayMode playMode)
{
data = new PatchOperationData();
data.packageName = "Preload";
data.playMode = playMode;
data.useBuildinFileSystem = true;
data.downloadingMaxNum = 10;
data.failedTryAgain = 3;
operation = new PatchOperation(data);
}
public async UniTask Execute()
{
#if !UNITY_EDITOR
CheckIsOffline();
#endif
if (!await operation.InitializePackage()) return;
var version = await GetBestPackageVersion();
#if UNITY_EDITOR
if (!await operation.RequestPackageVersion()) return;
version = operation.packageVersion;
#endif
//获取版本失败
if (version == null)
{
MessageBox.Show()
.SetTitle($"{operation.data.packageName}获取版本")
.SetContent("获取版本失败")
.AddButton("退出", (box) => { Application.Quit(); });
return;
}
operation.packageVersion = version;
if (!await operation.UpdatePackageManifest()) return;
await HotDllLoader.Inst.LoadDll(YooAssets.GetPackage("Preload"), "GameScripts.Preload");
await LoadAndShowPatchWindow();
MainUICanvas.Inst.InitBg.SetActive(false);
_ = UpdatePreloadPackage();
}
void CheckIsOffline()
{
if (string.IsNullOrEmpty(operation.GetCachedPackageVersion()))
{
operation.data.playMode = EPlayMode.OfflinePlayMode;
}
}
async Task<string> GetBestPackageVersion()
{
string cachedVersion = operation.GetCachedPackageVersion();
if (!string.IsNullOrEmpty(cachedVersion))
{
return cachedVersion;
}
string buildinVersion = await operation.GetBuildinPackageVersion();
if (!string.IsNullOrEmpty(buildinVersion))
{
return buildinVersion;
}
return null;
}
private async UniTask LoadAndShowPatchWindow()
{
var assetHandle = operation.package.LoadAssetAsync<GameObject>("PatchWindow");
await assetHandle.ToUniTask();
if (assetHandle.Status == EOperationStatus.Succeed)
GameObject.Instantiate(assetHandle.AssetObject, MainUICanvas.Inst.Medium);
Debug.Log("创建热更信息界面");
}
private async UniTask UpdatePreloadPackage()
{
if (!await operation.RequestPackageVersion(false)) return;
if (!await operation.UpdatePackageManifest(false)) return;
if (operation.CreateDownloader())
{
if (!await operation.DownloadPackageFiles()) return;
}
if (!await operation.ClearCacheBundle()) return;
operation.SaveVersionToCache();
}
}
}

View File

@@ -1,43 +0,0 @@
using Cysharp.Threading.Tasks;
using HybridCLR;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
using YooAsset;
public class HotDllLoader : Singleton<HotDllLoader>
{
public List<string> DepDlls = new List<string>()
{
"mscorlib.dll",
"System.dll",
"System.Core.dll",
};
public async UniTask LoadDll(ResourcePackage package, string dll)
{
if (package.GetAssetInfo(dll).Error == string.Empty)
{
AssetHandle handle = package.LoadAssetAsync<TextAsset>(dll);
await handle.ToUniTask();
#if UNITY_EDITOR
Assembly hotUpdateAss = System.AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == dll.Replace(".dll", ""));
#else
Assembly hotUpdateAss = Assembly.Load((handle.AssetObject as TextAsset).bytes);
#endif
Debug.Log($"加载{dll}");
}
}
public async UniTask LoadDepDll(ResourcePackage package)
{
foreach (string dll in DepDlls)
{
if (package.GetAssetInfo(dll).Error == string.Empty)
{
AssetHandle handle = package.LoadAssetAsync<TextAsset>(dll);
await handle.ToUniTask();
RuntimeApi.LoadMetadataForAOTAssembly((handle.AssetObject as TextAsset).bytes, HomologousImageMode.SuperSet);
}
}
}
}

View File

@@ -1,62 +0,0 @@
using Cysharp.Threading.Tasks;
using YooAsset;
public class MainOperation
{
PatchOperationData data;
PatchOperation operation;
bool autoDownload;
public MainOperation(EPlayMode playMode, bool autoDownload = false)
{
data = new PatchOperationData();
data.packageName = "Main";
data.playMode = playMode;
data.useBuildinFileSystem = false;
data.downloadingMaxNum = 10;
data.failedTryAgain = 3;
data.downloadUpdate = OnDownloadUpdate;
data.downloadFinish = OnDownloadFinish;
data.downloadError = OnDownloadError;
operation = new PatchOperation(data);
this.autoDownload = autoDownload;
}
public async UniTask Execute()
{
PatchEvent.UpdateProgress(0f);
if (!await operation.InitializePackage()) return;
if (!await operation.RequestPackageVersion()) return;
if (!await operation.UpdatePackageManifest()) return;
if (operation.CreateDownloader())
{
if (autoDownload)
{
if (!await operation.DownloadPackageFiles()) return;
}
else
{
if (!await operation.CheckDownloadOrSkip()) return;
}
}
if (!await operation.ClearCacheBundle()) return;
operation.SaveVersionToCache();
YooAssets.SetDefaultPackage(operation.package);
}
private void OnDownloadUpdate(DownloadUpdateData downloadUpdateData)
{
float progress = (float)downloadUpdateData.CurrentDownloadBytes / downloadUpdateData.TotalDownloadBytes;
string sizeText = $"{operation.FormatFileSize(downloadUpdateData.CurrentDownloadBytes)} / {operation.FormatFileSize(downloadUpdateData.TotalDownloadBytes)}";
PatchEvent.UpdateProgress(progress);
PatchEvent.UpdateDownloadSize(sizeText);
PatchEvent.UpdateStatus($"{data.packageName} 资源下载中...");
}
private void OnDownloadFinish(DownloaderFinishData downloaderFinishData)
{
PatchEvent.UpdateStatus("下载完成");
}
private void OnDownloadError(DownloadErrorData downloadErrorData)
{
PatchEvent.UpdateStatus($"下载失败:{downloadErrorData.FileName}\n{downloadErrorData.ErrorInfo}");
}
}

View File

@@ -1,28 +0,0 @@
using System;
public static class PatchEvent
{
public static event Action<string> OnStatusUpdate;
public static event Action<float> OnProgressUpdate;
public static event Action<string> OnDownloadSizeUpdate;
public static event Action OnClosePatchWindow;
public static void UpdateStatus(string status)
{
OnStatusUpdate?.Invoke(status);
}
public static void UpdateProgress(float progress)
{
OnProgressUpdate?.Invoke(progress);
}
public static void UpdateDownloadSize(string sizeText)
{
OnDownloadSizeUpdate?.Invoke(sizeText);
}
public static void ClosePatchWindow()
{
OnClosePatchWindow?.Invoke();
}
}

View File

@@ -1,19 +0,0 @@
using Cysharp.Threading.Tasks;
using YooAsset;
public class PatchManager : Singleton<PatchManager>
{
public async UniTask<bool> StartOperation(EPlayMode playMode)
{
YooAssets.Initialize();
PreloadOperation _preloadOperation = new PreloadOperation(playMode);
await _preloadOperation.Execute();
MainOperation _mainOperation = new MainOperation(playMode);
await _mainOperation.Execute();
await HotDllLoader.Inst.LoadDepDll(YooAssets.GetPackage("Main"));
await HotDllLoader.Inst.LoadDll(YooAssets.GetPackage("Main"), "GameScripts.Main");
return true;
}
}

View File

@@ -1,358 +0,0 @@
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;
using static YooAsset.DownloaderOperation;
public class PatchOperationData
{
public string packageName;
public EPlayMode playMode;
public bool useBuildinFileSystem;
public DownloadError downloadError;
public DownloaderFinish downloadFinish;
public DownloadUpdate downloadUpdate;
public int downloadingMaxNum = 10;
public int failedTryAgain = 3;
}
public class PatchOperation
{
public PatchOperationData data;
public ResourcePackage package;
public ResourceDownloaderOperation downloader;
public string packageVersion;
public string PkgVersionKey;
public PatchOperation(PatchOperationData data)
{
this.data = data;
PkgVersionKey = $"{Application.productName}_{data.packageName}";
#if !UNITY_EDITOR
PkgVersionKey = $"Editor_{Application.productName}_{data.packageName}";
#endif
}
#region
public async UniTask<bool> InitializePackage()
{
package = YooAssets.TryGetPackage(data.packageName);
if (package == null)
package = YooAssets.CreatePackage(data.packageName);
InitializationOperation initializationOperation = null;
switch (data.playMode)
{
case EPlayMode.EditorSimulateMode:
initializationOperation = InitializeEditorMode(package);
break;
case EPlayMode.OfflinePlayMode:
initializationOperation = InitializeOfflineMode(package);
break;
case EPlayMode.HostPlayMode:
initializationOperation = InitializeHostMode(package);
break;
case EPlayMode.WebPlayMode:
initializationOperation = InitializeWebPlayMode(package);
break;
case EPlayMode.CustomPlayMode:
break;
default:
break;
}
await initializationOperation.ToUniTask();
if (initializationOperation.Status != EOperationStatus.Succeed)
{
MessageBox.Show()
.SetTitle($"{data.packageName}初始化")
.SetContent($"{initializationOperation.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
else
{
PatchEvent.UpdateStatus($"初始化成功{data.packageName}");
Debug.Log($"初始化成功{data.packageName}");
}
return initializationOperation.Status == EOperationStatus.Succeed;
}
private InitializationOperation InitializeEditorMode(ResourcePackage package)
{
var buildResult = EditorSimulateModeHelper.SimulateBuild(data.packageName);
var packageRoot = buildResult.PackageRootDirectory;
var createParameters = new EditorSimulateModeParameters();
createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeOfflineMode(ResourcePackage package)
{
var createParameters = new OfflinePlayModeParameters();
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeHostMode(ResourcePackage package)
{
FileSystemParameters buildinFileSystemParams = null;
if (data.useBuildinFileSystem)
{
// 注意设置参数COPY_BUILDIN_PACKAGE_MANIFEST可以初始化的时候拷贝内置清单到沙盒目录
buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
}
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
// 注意设置参数INSTALL_CLEAR_MODE可以解决覆盖安装的时候将拷贝的内置清单文件清理的问题。
var cacheFileSystemParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
cacheFileSystemParams.AddParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, EOverwriteInstallClearMode.ClearAllManifestFiles);
var createParameters = new HostPlayModeParameters();
createParameters.BuildinFileSystemParameters = buildinFileSystemParams;
createParameters.CacheFileSystemParameters = cacheFileSystemParams;
return package.InitializeAsync(createParameters);
}
private InitializationOperation InitializeWebPlayMode(ResourcePackage package)
{
#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR
var createParameters = new WebPlayModeParameters();
string defaultHostServer = GetHostServerURL();
string fallbackHostServer = GetHostServerURL();
string packageRoot = $"{WeChatWASM.WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; //注意:如果有子目录,请修改此处!
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateFileSystemParameters(packageRoot, remoteServices);
return package.InitializeAsync(createParameters);
#else
var createParameters = new WebPlayModeParameters();
createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
return package.InitializeAsync(createParameters);
#endif
}
private string GetHostServerURL()
{
string hostServerIP = $"https://home.gtuantuan.online:9444/{Application.productName}";
string appVersion = "v1";
#if UNITY_EDITOR
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
// return $"{hostServerIP}/CDN/Android/{packageName}/{appVersion}";
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
return $"{hostServerIP}/CDN/IPhone/{data.packageName}/{appVersion}";
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
return $"{hostServerIP}/CDN/WebGL/{data.packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
#else
if (Application.platform == RuntimePlatform.Android)
return $"{hostServerIP}/CDN/Android/{data.packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.IPhonePlayer)
return $"{hostServerIP}/CDN/IPhone/{data.packageName}/{appVersion}";
else if (Application.platform == RuntimePlatform.WebGLPlayer)
return $"{hostServerIP}/CDN/WebGL/{data.packageName}/{appVersion}";
else
return $"{hostServerIP}/CDN/PC/{data.packageName}/{appVersion}";
#endif
}
private class RemoteServices : IRemoteServices
{
private readonly string _defaultHostServer;
private readonly string _fallbackHostServer;
public RemoteServices(string defaultHostServer, string fallbackHostServer)
{
_defaultHostServer = defaultHostServer;
_fallbackHostServer = fallbackHostServer;
}
string IRemoteServices.GetRemoteMainURL(string fileName)
{
return $"{_defaultHostServer}/{fileName}";
}
string IRemoteServices.GetRemoteFallbackURL(string fileName)
{
return $"{_fallbackHostServer}/{fileName}";
}
}
#endregion
#region
public async UniTask<bool> RequestPackageVersion(bool showBox = true)
{
var operation = package.RequestPackageVersionAsync(true, 5);
await operation.ToUniTask();
if (operation.Status != EOperationStatus.Succeed)
{
if (showBox)
{
if(await CheckUseLocalVersion(operation))
{
string cachedVersion = GetCachedPackageVersion();
if (!string.IsNullOrEmpty(cachedVersion))
{
packageVersion = cachedVersion;
return true;
}
}
}
}
else
{
packageVersion = operation.PackageVersion;
PatchEvent.UpdateStatus($"获取版本成功{data.packageName}");
Debug.Log($"获取版本成功{data.packageName}{packageVersion}");
}
return operation.Status == EOperationStatus.Succeed;
}
public async UniTask<bool> CheckUseLocalVersion(RequestPackageVersionOperation operation)
{
var completionSource = new UniTaskCompletionSource<bool>();
MessageBox.Show()
.SetTitle($"{data.packageName}请求版本")
.SetContent($"{operation.Error}")
.AddButton("继续", (box) =>
{
completionSource.TrySetResult(true);
})
.AddButton("退出", (box) =>
{
completionSource.TrySetResult(false);
Application.Quit();
});
bool shouldContinue = await completionSource.Task;
return shouldContinue;
}
public async Task<string> GetBuildinPackageVersion()
{
var operation = new GetBuildinPackageVersionOperation(data.packageName);
YooAssets.StartOperation(operation);
await operation;
if (operation.Status == EOperationStatus.Succeed)
{
return operation.PackageVersion;
}
else
{
return null;
}
}
public string GetCachedPackageVersion()
{
if (PlayerPrefs.HasKey(PkgVersionKey))
{
return PlayerPrefs.GetString(PkgVersionKey);
}
return null;
}
public void SaveVersionToCache()
{
PlayerPrefs.SetString(PkgVersionKey, packageVersion);
PlayerPrefs.Save();
PatchEvent.UpdateStatus($"更新完成{data.packageName}");
Debug.Log($"更新{data.packageName}完成,版本号{packageVersion}");
}
public async UniTask<bool> UpdatePackageManifest(bool showBox = true)
{
var operation = package.UpdatePackageManifestAsync(packageVersion,10);
await operation.ToUniTask();
if (operation.Status != EOperationStatus.Succeed)
{
if (showBox)
{
MessageBox.Show()
.SetTitle($"{data.packageName}更新清单")
.SetContent($"{operation.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
}
else
{
PatchEvent.UpdateStatus($"获取资源清单成功{data.packageName}");
Debug.Log($"获取资源清单成功{data.packageName}");
}
return operation.Status == EOperationStatus.Succeed;
}
#endregion
#region
public bool CreateDownloader()
{
downloader = package.CreateResourceDownloader(data.downloadingMaxNum, data.failedTryAgain);
downloader.DownloadErrorCallback = data.downloadError;
downloader.DownloadFinishCallback = data.downloadFinish;
downloader.DownloadUpdateCallback = data.downloadUpdate;
if (downloader.TotalDownloadCount == 0) return false;
return true;
}
public async UniTask<bool> CheckDownloadOrSkip()
{
var completionSource = new UniTaskCompletionSource<bool>();
MessageBox.Show()
.SetTitle($"{data.packageName}发现更新")
.SetContent($"发现资源更新\n{GetCachedPackageVersion()}=>{packageVersion}: {FormatFileSize(downloader.TotalDownloadBytes)}")
.AddButton("下载", async (box) =>
{
bool success = await DownloadPackageFiles();
completionSource.TrySetResult(success);
})
.AddButton("跳过", async (box) =>
{
downloader.CancelDownload();
packageVersion = GetCachedPackageVersion();
await UpdatePackageManifest();
completionSource.TrySetResult(true);
})
.AddButton("退出", (box) =>
{
downloader.CancelDownload();
completionSource.TrySetResult(false);
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
});
bool shouldContinue = await completionSource.Task;
return shouldContinue;
}
public async UniTask<bool> DownloadPackageFiles(bool showBox = true)
{
if (downloader.TotalDownloadCount == 0)
return true;
Debug.Log($"{data.packageName} DownloadPackageFiles {downloader.TotalDownloadCount}");
downloader.BeginDownload();
await downloader.ToUniTask();
if (downloader.Status != EOperationStatus.Succeed)
{
if (showBox)
{
MessageBox.Show()
.SetTitle($"{data.packageName}下载文件")
.SetContent($"{downloader.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
return false;
}
return downloader.Status == EOperationStatus.Succeed;
}
public async UniTask<bool> ClearCacheBundle(bool showBox = true)
{
var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearUnusedBundleFiles);
await operation.ToUniTask();
if (operation.Status != EOperationStatus.Succeed)
{
if (showBox)
{
MessageBox.Show()
.SetTitle($"{data.packageName}清除缓存")
.SetContent($"{operation.Error}")
.AddButton("退出", (box) => { Application.Quit(); });
}
}
return operation.Status == EOperationStatus.Succeed;
}
public string FormatFileSize(long size)
{
if (size < 1024 * 1024)
return $"{(size / 1024f):F1}KB";
else
return $"{size / 1024f / 1024f:F1}MB";
}
#endregion
}

View File

@@ -1,88 +0,0 @@
using Cysharp.Threading.Tasks;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;
public class PreloadOperation
{
PatchOperationData data;
PatchOperation operation;
public PreloadOperation(EPlayMode playMode)
{
data = new PatchOperationData();
data.packageName = "Preload";
data.playMode = playMode;
data.useBuildinFileSystem = true;
data.downloadingMaxNum = 10;
data.failedTryAgain = 3;
operation = new PatchOperation(data);
}
public async UniTask Execute()
{
#if !UNITY_EDITOR
CheckIsOffline();
#endif
if (!await operation.InitializePackage()) return;
var version = await GetBestPackageVersion();
#if UNITY_EDITOR
if (!await operation.RequestPackageVersion()) return;
version = operation.packageVersion;
#endif
//获取版本失败
if (version == null)
{
MessageBox.Show()
.SetTitle($"{operation.data.packageName}获取版本")
.SetContent("获取版本失败")
.AddButton("退出", (box) => { Application.Quit(); });
return;
}
operation.packageVersion = version;
if (!await operation.UpdatePackageManifest()) return;
await HotDllLoader.Inst.LoadDll(YooAssets.GetPackage("Preload"), "GameScripts.Preload");
await LoadAndShowPatchWindow();
MainUICanvas.Inst.InitBg.SetActive(false);
_ = UpdatePreloadPackage();
}
void CheckIsOffline()
{
if (string.IsNullOrEmpty(operation.GetCachedPackageVersion()))
{
operation.data.playMode = EPlayMode.OfflinePlayMode;
}
}
async Task<string> GetBestPackageVersion()
{
string cachedVersion = operation.GetCachedPackageVersion();
if (!string.IsNullOrEmpty(cachedVersion))
{
return cachedVersion;
}
string buildinVersion = await operation.GetBuildinPackageVersion();
if (!string.IsNullOrEmpty(buildinVersion))
{
return buildinVersion;
}
return null;
}
private async UniTask LoadAndShowPatchWindow()
{
var assetHandle = operation.package.LoadAssetAsync<GameObject>("PatchWindow");
await assetHandle.ToUniTask();
if (assetHandle.Status == EOperationStatus.Succeed)
GameObject.Instantiate(assetHandle.AssetObject, MainUICanvas.Inst.Medium);
Debug.Log("创建热更信息界面");
}
private async UniTask UpdatePreloadPackage()
{
if (!await operation.RequestPackageVersion(false)) return;
if (!await operation.UpdatePackageManifest(false)) return;
if (operation.CreateDownloader())
{
if (!await operation.DownloadPackageFiles()) return;
}
if (!await operation.ClearCacheBundle()) return;
operation.SaveVersionToCache();
}
}

View File

@@ -1,22 +1,25 @@
using UnityEngine;
public class Singleton<T> where T:class,new()
namespace Tuan.GameFramework
{
static T _inst;
static readonly object _lock = new object();
public static T Inst
public class Singleton<T> where T : class, new()
{
get
static T _inst;
static readonly object _lock = new object();
public static T Inst
{
lock (_lock)
get
{
if(_inst == null)
lock (_lock)
{
_inst = new T();
Debug.Log($"[Singleton] 创建 {typeof(T).Name} 实例");
if (_inst == null)
{
_inst = new T();
Debug.Log($"[Singleton] 创建 {typeof(T).Name} 实例");
}
return _inst;
}
return _inst;
}
}
}
}
}

View File

@@ -1,28 +1,31 @@
using UnityEngine;
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
namespace Tuan.GameFramework
{
private static T _instance;
private static readonly object _lock = new object();
public static T Inst
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{
get
private static T _instance;
private static readonly object _lock = new object();
public static T Inst
{
lock (_lock)
get
{
if (_instance == null)
lock (_lock)
{
_instance = FindAnyObjectByType<T>();
if (_instance == null)
{
GameObject singletonObject = new GameObject(typeof(T).Name);
DontDestroyOnLoad(singletonObject);
_instance = singletonObject.AddComponent<T>();
Debug.Log($"实例化{singletonObject.name}");
_instance = FindAnyObjectByType<T>();
if (_instance == null)
{
GameObject singletonObject = new GameObject(typeof(T).Name);
DontDestroyOnLoad(singletonObject);
_instance = singletonObject.AddComponent<T>();
Debug.Log($"实例化{singletonObject.name}");
}
}
return _instance;
}
return _instance;
}
}
}