update sample
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
public class BundleStream : FileStream
|
||||
{
|
||||
public const byte KEY = 64;
|
||||
|
||||
public BundleStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) : base(path, mode, access, share, bufferSize, useAsync)
|
||||
{
|
||||
}
|
||||
public BundleStream(string path, FileMode mode) : base(path, mode)
|
||||
{
|
||||
}
|
||||
|
||||
public override int Read(byte[] array, int offset, int count)
|
||||
{
|
||||
var index = base.Read(array, offset, count);
|
||||
for (int i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] ^= KEY;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7da6bc93686626a4cb248691891073ac
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 22a43ec58b9da1743bfa629be08ef1ac
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
|
||||
/// <summary>
|
||||
/// 清理未使用的缓存文件
|
||||
/// </summary>
|
||||
internal class FsmClearCache : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
PatchEventDefine.PatchStatesChange.SendEventMessage("清理未使用的缓存文件!");
|
||||
var package = YooAsset.YooAssets.GetAssetsPackage("DefaultPackage");
|
||||
var operation = package.ClearPackageUnusedCacheFilesAsync();
|
||||
operation.Completed += Operation_Completed;
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private void Operation_Completed(YooAsset.AsyncOperationBase obj)
|
||||
{
|
||||
_machine.ChangeState<FsmPatchDone>();
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d83ce785f1442a40a1ad0f837073b69
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Module;
|
||||
using YooAsset;
|
||||
|
||||
/// <summary>
|
||||
/// 创建文件下载器
|
||||
/// </summary>
|
||||
public class FsmCreateDownloader : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
PatchEventDefine.PatchStatesChange.SendEventMessage("创建补丁下载器!");
|
||||
UniModule.StartCoroutine(CreateDownloader());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
IEnumerator CreateDownloader()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.5f);
|
||||
|
||||
int downloadingMaxNum = 10;
|
||||
int failedTryAgain = 3;
|
||||
var downloader = YooAssets.CreatePatchDownloader(downloadingMaxNum, failedTryAgain);
|
||||
PatchManager.Instance.Downloader = downloader;
|
||||
|
||||
if (downloader.TotalDownloadCount == 0)
|
||||
{
|
||||
Debug.Log("没有发现需要下载的资源!");
|
||||
_machine.ChangeState<FsmDownloadOver>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"一共发现了{downloader.TotalDownloadCount}个资源需要更新下载!");
|
||||
|
||||
// 发现新更新文件后,挂起流程系统
|
||||
// 注意:开发者需要在下载前检测磁盘空间不足
|
||||
int totalDownloadCount = downloader.TotalDownloadCount;
|
||||
long totalDownloadBytes = downloader.TotalDownloadBytes;
|
||||
PatchEventDefine.FoundUpdateFiles.SendEventMessage(totalDownloadCount, totalDownloadBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b104741ebcb72946abe282c1da73360
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Module;
|
||||
using YooAsset;
|
||||
|
||||
/// <summary>
|
||||
/// 下载更新文件
|
||||
/// </summary>
|
||||
public class FsmDownloadFiles : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
PatchEventDefine.PatchStatesChange.SendEventMessage("开始下载补丁文件!");
|
||||
UniModule.StartCoroutine(BeginDownload());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator BeginDownload()
|
||||
{
|
||||
var downloader = PatchManager.Instance.Downloader;
|
||||
|
||||
// 注册下载回调
|
||||
downloader.OnDownloadErrorCallback = PatchEventDefine.WebFileDownloadFailed.SendEventMessage;
|
||||
downloader.OnDownloadProgressCallback = PatchEventDefine.DownloadProgressUpdate.SendEventMessage;
|
||||
downloader.BeginDownload();
|
||||
yield return downloader;
|
||||
|
||||
// 检测下载结果
|
||||
if (downloader.Status != EOperationStatus.Succeed)
|
||||
yield break;
|
||||
|
||||
_machine.ChangeState<FsmPatchDone>();
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3fae22040fe2d541ab4582c7c1c71fb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
|
||||
/// <summary>
|
||||
/// 下载完毕
|
||||
/// </summary>
|
||||
internal class FsmDownloadOver : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
_machine.ChangeState<FsmClearCache>();
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c96c57c65f74bb3469dc71b13ef78c84
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Module;
|
||||
|
||||
/// <summary>
|
||||
/// 更新完毕
|
||||
/// </summary>
|
||||
internal class FsmPatchDone : IStateNode
|
||||
{
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
PatchEventDefine.PatchStatesChange.SendEventMessage("开始游戏!");
|
||||
|
||||
// 创建游戏管理器
|
||||
UniModule.CreateModule<GameManager>();
|
||||
|
||||
// 开启游戏流程
|
||||
GameManager.Instance.Run();
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6cf80cfbed474c34b892eaeda7fcb054
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Module;
|
||||
using YooAsset;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化工作
|
||||
/// </summary>
|
||||
internal class FsmPatchInit : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
// 加载更新面板
|
||||
var go = Resources.Load<GameObject>("PatchWindow");
|
||||
GameObject.Instantiate(go);
|
||||
|
||||
// 初始化资源系统
|
||||
UniModule.StartCoroutine(InitYooAsset());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator InitYooAsset()
|
||||
{
|
||||
var playMode = PatchManager.Instance.PlayMode;
|
||||
|
||||
// 创建默认的资源包
|
||||
var package = YooAssets.CreateAssetsPackage("DefaultPackage");
|
||||
YooAssets.SetDefaultAssetsPackage(package);
|
||||
|
||||
// 编辑器下的模拟模式
|
||||
if (playMode == EPlayMode.EditorSimulateMode)
|
||||
{
|
||||
var createParameters = new EditorSimulateModeParameters();
|
||||
createParameters.SimulatePatchManifestPath = EditorSimulateModeHelper.SimulateBuild("DefaultPackage");
|
||||
yield return package.InitializeAsync(createParameters);
|
||||
}
|
||||
|
||||
// 单机运行模式
|
||||
if (playMode == EPlayMode.OfflinePlayMode)
|
||||
{
|
||||
var createParameters = new OfflinePlayModeParameters();
|
||||
createParameters.DecryptionServices = new GameDecryptionServices();
|
||||
yield return package.InitializeAsync(createParameters);
|
||||
}
|
||||
|
||||
// 联机运行模式
|
||||
if (playMode == EPlayMode.HostPlayMode)
|
||||
{
|
||||
var createParameters = new HostPlayModeParameters();
|
||||
createParameters.DecryptionServices = new GameDecryptionServices();
|
||||
createParameters.QueryServices = new GameQueryServices();
|
||||
createParameters.DefaultHostServer = GetHostServerURL();
|
||||
createParameters.FallbackHostServer = GetHostServerURL();
|
||||
yield return package.InitializeAsync(createParameters);
|
||||
}
|
||||
|
||||
_machine.ChangeState<FsmUpdateVersion>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源服务器地址
|
||||
/// </summary>
|
||||
private string GetHostServerURL()
|
||||
{
|
||||
//string hostServerIP = "http://10.0.2.2"; //安卓模拟器地址
|
||||
string hostServerIP = "http://127.0.0.1";
|
||||
string gameVersion = "v1.0";
|
||||
|
||||
#if UNITY_EDITOR
|
||||
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
|
||||
return $"{hostServerIP}/CDN/Android/{gameVersion}";
|
||||
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
|
||||
return $"{hostServerIP}/CDN/IPhone/{gameVersion}";
|
||||
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
|
||||
return $"{hostServerIP}/CDN/WebGL/{gameVersion}";
|
||||
else
|
||||
return $"{hostServerIP}/CDN/PC/{gameVersion}";
|
||||
#else
|
||||
if (Application.platform == RuntimePlatform.Android)
|
||||
return $"{hostServerIP}/CDN/Android/{gameVersion}";
|
||||
else if (Application.platform == RuntimePlatform.IPhonePlayer)
|
||||
return $"{hostServerIP}/CDN/IPhone/{gameVersion}";
|
||||
else if (Application.platform == RuntimePlatform.WebGLPlayer)
|
||||
return $"{hostServerIP}/CDN/WebGL/{gameVersion}";
|
||||
else
|
||||
return $"{hostServerIP}/CDN/PC/{gameVersion}";
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内置文件查询服务类
|
||||
/// </summary>
|
||||
private class GameQueryServices : IQueryServices
|
||||
{
|
||||
public bool QueryStreamingAssets(string fileName)
|
||||
{
|
||||
// 注意:使用了BetterStreamingAssets插件,使用前需要初始化该插件!
|
||||
string buildinFolderName = YooAssets.GetStreamingAssetBuildinFolderName();
|
||||
return BetterStreamingAssets.FileExists($"{buildinFolderName}/{fileName}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源文件解密服务类
|
||||
/// </summary>
|
||||
private class GameDecryptionServices : IDecryptionServices
|
||||
{
|
||||
public ulong LoadFromFileOffset(DecryptFileInfo fileInfo)
|
||||
{
|
||||
return 32;
|
||||
}
|
||||
|
||||
public byte[] LoadFromMemory(DecryptFileInfo fileInfo)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public FileStream LoadFromStream(DecryptFileInfo fileInfo)
|
||||
{
|
||||
BundleStream bundleStream = new BundleStream(fileInfo.FilePath, FileMode.Open);
|
||||
return bundleStream;
|
||||
}
|
||||
|
||||
public uint GetManagedReadBufferSize()
|
||||
{
|
||||
return 1024;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8998f67b4187d404eb26190f5cd5ac86
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Module;
|
||||
using YooAsset;
|
||||
|
||||
/// <summary>
|
||||
/// 更新资源清单
|
||||
/// </summary>
|
||||
public class FsmUpdateManifest : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
PatchEventDefine.PatchStatesChange.SendEventMessage("更新资源清单!");
|
||||
UniModule.StartCoroutine(UpdateManifest());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator UpdateManifest()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.5f);
|
||||
|
||||
var package = YooAssets.GetAssetsPackage("DefaultPackage");
|
||||
var operation = package.UpdatePackageManifestAsync(PatchManager.Instance.PackageVersion, 30);
|
||||
yield return operation;
|
||||
|
||||
if(operation.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
_machine.ChangeState<FsmCreateDownloader>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning(operation.Error);
|
||||
PatchEventDefine.PatchManifestUpdateFailed.SendEventMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b4b6ef49759dcf14d80c5aa2d360f597
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Module;
|
||||
using YooAsset;
|
||||
|
||||
/// <summary>
|
||||
/// 更新资源版本号
|
||||
/// </summary>
|
||||
internal class FsmUpdateVersion : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
PatchEventDefine.PatchStatesChange.SendEventMessage("获取最新的资源版本 !");
|
||||
UniModule.StartCoroutine(GetStaticVersion());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator GetStaticVersion()
|
||||
{
|
||||
yield return new WaitForSecondsRealtime(0.5f);
|
||||
|
||||
var package = YooAssets.GetAssetsPackage("DefaultPackage");
|
||||
var operation = package.UpdatePackageVersionAsync(30);
|
||||
yield return operation;
|
||||
|
||||
if (operation.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
PatchManager.Instance.PackageVersion = operation.PackageVersion;
|
||||
_machine.ChangeState<FsmUpdateManifest>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning(operation.Error);
|
||||
PatchEventDefine.PackageVersionUpdateFailed.SendEventMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f5ea76dfe0fc52b47a00a3bf68a54d09
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,105 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Event;
|
||||
using UniFramework.Module;
|
||||
using YooAsset;
|
||||
|
||||
public class PatchManager : ModuleSingleton<PatchManager>, IModule
|
||||
{
|
||||
/// <summary>
|
||||
/// 运行模式
|
||||
/// </summary>
|
||||
public EPlayMode PlayMode { private set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 包裹的版本信息
|
||||
/// </summary>
|
||||
public string PackageVersion { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 下载器
|
||||
/// </summary>
|
||||
public PatchDownloaderOperation Downloader { set; get; }
|
||||
|
||||
|
||||
private bool _isRun = false;
|
||||
private EventGroup _eventGroup = new EventGroup();
|
||||
private StateMachine _machine;
|
||||
|
||||
void IModule.OnCreate(object createParam)
|
||||
{
|
||||
}
|
||||
void IModule.OnDestroy()
|
||||
{
|
||||
_eventGroup.RemoveAllListener();
|
||||
}
|
||||
void IModule.OnUpdate()
|
||||
{
|
||||
if (_machine != null)
|
||||
_machine.Update();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开启流程
|
||||
/// </summary>
|
||||
public void Run(EPlayMode playMode)
|
||||
{
|
||||
if (_isRun == false)
|
||||
{
|
||||
_isRun = true;
|
||||
PlayMode = playMode;
|
||||
|
||||
// 注册监听事件
|
||||
_eventGroup.AddListener<UserEventDefine.UserBeginDownloadWebFiles>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<UserEventDefine.UserTryUpdatePackageVersion>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<UserEventDefine.UserTryUpdatePatchManifest>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<UserEventDefine.UserTryDownloadWebFiles>(OnHandleEventMessage);
|
||||
|
||||
Debug.Log("开启补丁更新流程...");
|
||||
_machine = new StateMachine(this);
|
||||
_machine.AddNode<FsmPatchInit>();
|
||||
_machine.AddNode<FsmUpdateVersion>();
|
||||
_machine.AddNode<FsmUpdateManifest>();
|
||||
_machine.AddNode<FsmCreateDownloader>();
|
||||
_machine.AddNode<FsmDownloadFiles>();
|
||||
_machine.AddNode<FsmDownloadOver>();
|
||||
_machine.AddNode<FsmClearCache>();
|
||||
_machine.AddNode<FsmPatchDone>();
|
||||
_machine.Run<FsmPatchInit>();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("补丁更新已经正在进行中!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收事件
|
||||
/// </summary>
|
||||
private void OnHandleEventMessage(IEventMessage message)
|
||||
{
|
||||
if (message is UserEventDefine.UserBeginDownloadWebFiles)
|
||||
{
|
||||
_machine.ChangeState<FsmDownloadFiles>();
|
||||
}
|
||||
else if (message is UserEventDefine.UserTryUpdatePackageVersion)
|
||||
{
|
||||
_machine.ChangeState<FsmUpdateVersion>();
|
||||
}
|
||||
else if (message is UserEventDefine.UserTryUpdatePatchManifest)
|
||||
{
|
||||
_machine.ChangeState<FsmUpdateManifest>();
|
||||
}
|
||||
else if (message is UserEventDefine.UserTryDownloadWebFiles)
|
||||
{
|
||||
_machine.ChangeState<FsmCreateDownloader>();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException($"{message.GetType()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c1b4cadd806a1a409db720564a44a83
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UniFramework.Event;
|
||||
|
||||
public class PatchWindow : MonoBehaviour
|
||||
{
|
||||
/// <summary>
|
||||
/// 对话框封装类
|
||||
/// </summary>
|
||||
private class MessageBox
|
||||
{
|
||||
private GameObject _cloneObject;
|
||||
private Text _content;
|
||||
private Button _btnOK;
|
||||
private System.Action _clickOK;
|
||||
|
||||
public bool ActiveSelf
|
||||
{
|
||||
get
|
||||
{
|
||||
return _cloneObject.activeSelf;
|
||||
}
|
||||
}
|
||||
|
||||
public void Create(GameObject cloneObject)
|
||||
{
|
||||
_cloneObject = cloneObject;
|
||||
_content = cloneObject.transform.Find("txt_content").GetComponent<Text>();
|
||||
_btnOK = cloneObject.transform.Find("btn_ok").GetComponent<Button>();
|
||||
_btnOK.onClick.AddListener(OnClickYes);
|
||||
}
|
||||
public void Show(string content, System.Action clickOK)
|
||||
{
|
||||
_content.text = content;
|
||||
_clickOK = clickOK;
|
||||
_cloneObject.SetActive(true);
|
||||
_cloneObject.transform.SetAsLastSibling();
|
||||
}
|
||||
public void Hide()
|
||||
{
|
||||
_content.text = string.Empty;
|
||||
_clickOK = null;
|
||||
_cloneObject.SetActive(false);
|
||||
}
|
||||
private void OnClickYes()
|
||||
{
|
||||
_clickOK?.Invoke();
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private readonly EventGroup _eventGroup = new EventGroup();
|
||||
private readonly List<MessageBox> _msgBoxList = new List<MessageBox>();
|
||||
|
||||
// UGUI相关
|
||||
private GameObject _messageBoxObj;
|
||||
private Slider _slider;
|
||||
private Text _tips;
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_slider = transform.Find("UIWindow/Slider").GetComponent<Slider>();
|
||||
_tips = transform.Find("UIWindow/Slider/txt_tips").GetComponent<Text>();
|
||||
_tips.text = "Initializing the game world !";
|
||||
_messageBoxObj = transform.Find("UIWindow/MessgeBox").gameObject;
|
||||
_messageBoxObj.SetActive(false);
|
||||
|
||||
_eventGroup.AddListener<PatchEventDefine.PatchStatesChange>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<PatchEventDefine.FoundUpdateFiles>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<PatchEventDefine.DownloadProgressUpdate>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<PatchEventDefine.PackageVersionUpdateFailed>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<PatchEventDefine.PatchManifestUpdateFailed>(OnHandleEventMessage);
|
||||
_eventGroup.AddListener<PatchEventDefine.WebFileDownloadFailed>(OnHandleEventMessage);
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
_eventGroup.RemoveAllListener();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收事件
|
||||
/// </summary>
|
||||
private void OnHandleEventMessage(IEventMessage message)
|
||||
{
|
||||
if (message is PatchEventDefine.PatchStatesChange)
|
||||
{
|
||||
var msg = message as PatchEventDefine.PatchStatesChange;
|
||||
_tips.text = msg.Tips;
|
||||
}
|
||||
else if (message is PatchEventDefine.FoundUpdateFiles)
|
||||
{
|
||||
var msg = message as PatchEventDefine.FoundUpdateFiles;
|
||||
System.Action callback = () =>
|
||||
{
|
||||
UserEventDefine.UserBeginDownloadWebFiles.SendEventMessage();
|
||||
};
|
||||
float sizeMB = msg.TotalSizeBytes / 1048576f;
|
||||
sizeMB = Mathf.Clamp(sizeMB, 0.1f, float.MaxValue);
|
||||
string totalSizeMB = sizeMB.ToString("f1");
|
||||
ShowMessageBox($"Found update patch files, Total count {msg.TotalCount} Total szie {totalSizeMB}MB", callback);
|
||||
}
|
||||
else if (message is PatchEventDefine.DownloadProgressUpdate)
|
||||
{
|
||||
var msg = message as PatchEventDefine.DownloadProgressUpdate;
|
||||
_slider.value = (float)msg.CurrentDownloadCount / msg.TotalDownloadCount;
|
||||
string currentSizeMB = (msg.CurrentDownloadSizeBytes / 1048576f).ToString("f1");
|
||||
string totalSizeMB = (msg.TotalDownloadSizeBytes / 1048576f).ToString("f1");
|
||||
_tips.text = $"{msg.CurrentDownloadCount}/{msg.TotalDownloadCount} {currentSizeMB}MB/{totalSizeMB}MB";
|
||||
}
|
||||
else if (message is PatchEventDefine.PackageVersionUpdateFailed)
|
||||
{
|
||||
System.Action callback = () =>
|
||||
{
|
||||
UserEventDefine.UserTryUpdatePackageVersion.SendEventMessage();
|
||||
};
|
||||
ShowMessageBox($"Failed to update static version, please check the network status.", callback);
|
||||
}
|
||||
else if (message is PatchEventDefine.PatchManifestUpdateFailed)
|
||||
{
|
||||
System.Action callback = () =>
|
||||
{
|
||||
UserEventDefine.UserTryUpdatePatchManifest.SendEventMessage();
|
||||
};
|
||||
ShowMessageBox($"Failed to update patch manifest, please check the network status.", callback);
|
||||
}
|
||||
else if (message is PatchEventDefine.WebFileDownloadFailed)
|
||||
{
|
||||
var msg = message as PatchEventDefine.WebFileDownloadFailed;
|
||||
System.Action callback = () =>
|
||||
{
|
||||
UserEventDefine.UserTryDownloadWebFiles.SendEventMessage();
|
||||
};
|
||||
ShowMessageBox($"Failed to download file : {msg.FileName}", callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new System.NotImplementedException($"{message.GetType()}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示对话框
|
||||
/// </summary>
|
||||
private void ShowMessageBox(string content, System.Action ok)
|
||||
{
|
||||
// 尝试获取一个可用的对话框
|
||||
MessageBox msgBox = null;
|
||||
for (int i = 0; i < _msgBoxList.Count; i++)
|
||||
{
|
||||
var item = _msgBoxList[i];
|
||||
if (item.ActiveSelf == false)
|
||||
{
|
||||
msgBox = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有可用的对话框,则创建一个新的对话框
|
||||
if (msgBox == null)
|
||||
{
|
||||
msgBox = new MessageBox();
|
||||
var cloneObject = GameObject.Instantiate(_messageBoxObj, _messageBoxObj.transform.parent);
|
||||
msgBox.Create(cloneObject);
|
||||
_msgBoxList.Add(msgBox);
|
||||
}
|
||||
|
||||
// 显示对话框
|
||||
msgBox.Show(content, ok);
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c569e2dd13155914aa41a50e0e588b6d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user