init
This commit is contained in:
30
Assets/GameFramework/Runtime/Boot.cs
Normal file
30
Assets/GameFramework/Runtime/Boot.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using YooAsset;
|
||||
|
||||
public class Boot : MonoBehaviour
|
||||
{
|
||||
public GameObject MainUICanvas;
|
||||
public Camera UICamera;
|
||||
public EPlayMode PlayMode = EPlayMode.EditorSimulateMode;
|
||||
void Awake()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
PlayerPrefs.DeleteAll();
|
||||
#endif
|
||||
Application.targetFrameRate = 60;
|
||||
Application.runInBackground = true;
|
||||
}
|
||||
async void Start()
|
||||
{
|
||||
GameManager.Inst.UICamera = UICamera;
|
||||
GameManager.Inst.MainUICanvas = MainUICanvas;
|
||||
bool updateSuccess = await PatchManager.Inst.StartOperation(PlayMode);
|
||||
if (updateSuccess)
|
||||
EnterGame();
|
||||
}
|
||||
private void EnterGame()
|
||||
{
|
||||
Debug.Log("EnterGame");
|
||||
}
|
||||
}
|
||||
2
Assets/GameFramework/Runtime/Boot.cs.meta
Normal file
2
Assets/GameFramework/Runtime/Boot.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: beb122424f267994cb04b5ba15d3f694
|
||||
7
Assets/GameFramework/Runtime/GameManager.cs
Normal file
7
Assets/GameFramework/Runtime/GameManager.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class GameManager : Singleton<GameManager>
|
||||
{
|
||||
public Camera UICamera;
|
||||
public GameObject MainUICanvas;
|
||||
}
|
||||
2
Assets/GameFramework/Runtime/GameManager.cs.meta
Normal file
2
Assets/GameFramework/Runtime/GameManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e75707ce2167c0842a4225d65f458010
|
||||
86
Assets/GameFramework/Runtime/MessageBox.cs
Normal file
86
Assets/GameFramework/Runtime/MessageBox.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class MessageBox : MonoBehaviour
|
||||
{
|
||||
[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()
|
||||
{
|
||||
|
||||
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, GameManager.Inst.MainUICanvas.transform);
|
||||
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)
|
||||
{
|
||||
if (button != null) Destroy(button.gameObject);
|
||||
}
|
||||
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(() =>
|
||||
{
|
||||
onClick?.Invoke(this);
|
||||
Hide();
|
||||
});
|
||||
|
||||
createdButtons.Add(button);
|
||||
button.gameObject.SetActive(true);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
2
Assets/GameFramework/Runtime/MessageBox.cs.meta
Normal file
2
Assets/GameFramework/Runtime/MessageBox.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a72b28cd18f8324b8d9bac07e02771f
|
||||
8
Assets/GameFramework/Runtime/PatchLogic.meta
Normal file
8
Assets/GameFramework/Runtime/PatchLogic.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39bbdf272d77f6640ab4a9149b6c1377
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
77
Assets/GameFramework/Runtime/PatchLogic/MainOperation.cs
Normal file
77
Assets/GameFramework/Runtime/PatchLogic/MainOperation.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
|
||||
public class MainOperation
|
||||
{
|
||||
PatchOperationData data;
|
||||
PatchOperation operation;
|
||||
public MainOperation(EPlayMode playMode)
|
||||
{
|
||||
data = new PatchOperationData();
|
||||
data.packageName = "Main";
|
||||
data.playMode = playMode;
|
||||
data.useBuildinFileSystem = false;
|
||||
data.downloadingMaxNum = 10;
|
||||
data.failedTryAgain = 3;
|
||||
|
||||
operation = new PatchOperation(data);
|
||||
}
|
||||
|
||||
public async UniTask Execute()
|
||||
{
|
||||
InitializationOperation initializationOperation = await operation.InitializePackage();
|
||||
//<2F><>ʼ<EFBFBD><CABC>ʧ<EFBFBD><CAA7>
|
||||
if (initializationOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent($"{initializationOperation.Error}")
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
var PackageVersionOperation = await operation.RequestPackageVersion();
|
||||
if (PackageVersionOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent($"{PackageVersionOperation.Error}")
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
operation.packageVersion = PackageVersionOperation.PackageVersion;
|
||||
var PackageManifestOperation = await operation.UpdatePackageManifest();
|
||||
if (PackageManifestOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent($"{PackageManifestOperation.Error}")
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
var DownloaderOperation = operation.CreateDownloader();
|
||||
if (DownloaderOperation.TotalDownloadCount == 0)
|
||||
{
|
||||
operation.SaveVersionToCache();
|
||||
return;
|
||||
}
|
||||
if (!await operation.DownloadPackageFiles())
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent($"{DownloaderOperation.Error}")
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
var ClearCacheFilesOperation = await operation.ClearCacheBundle();
|
||||
if (ClearCacheFilesOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent($"{ClearCacheFilesOperation.Error}")
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
operation.SaveVersionToCache();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 573021b0c5d8ec74f96d2a20ee3a7165
|
||||
18
Assets/GameFramework/Runtime/PatchLogic/PatchManager.cs
Normal file
18
Assets/GameFramework/Runtime/PatchLogic/PatchManager.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using UnityEngine;
|
||||
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();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 625284a2d721ab646904f46c84f37a8a
|
||||
230
Assets/GameFramework/Runtime/PatchLogic/PatchOperation.cs
Normal file
230
Assets/GameFramework/Runtime/PatchLogic/PatchOperation.cs
Normal file
@@ -0,0 +1,230 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System;
|
||||
using System.Threading;
|
||||
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}";
|
||||
}
|
||||
#region <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
public async UniTask<InitializationOperation> 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();
|
||||
return initializationOperation;
|
||||
}
|
||||
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)
|
||||
{
|
||||
// ע<>⣺<EFBFBD><E2A3BA><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>COPY_BUILDIN_PACKAGE_MANIFEST<53><54><EFBFBD><EFBFBD><EFBFBD>Գ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><F2BFBDB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>嵥<EFBFBD><E5B5A5>ɳ<EFBFBD><C9B3>Ŀ¼
|
||||
buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
|
||||
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
|
||||
}
|
||||
string defaultHostServer = GetHostServerURL();
|
||||
string fallbackHostServer = GetHostServerURL();
|
||||
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
||||
// ע<>⣺<EFBFBD><E2A3BA><EFBFBD>ò<EFBFBD><C3B2><EFBFBD>INSTALL_CLEAR_MODE<44><45><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>װ<EFBFBD><D7B0>ʱ<EFBFBD><EFBFBD><F2BDABBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>嵥<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⡣
|
||||
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"; //ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD>Ĵ˴<C4B4><CBB4><EFBFBD>
|
||||
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 <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#region <EFBFBD>汾<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>嵥<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
public async UniTask<RequestPackageVersionOperation> RequestPackageVersion()
|
||||
{
|
||||
var operation = package.RequestPackageVersionAsync(true, 1);
|
||||
await operation.ToUniTask();
|
||||
return operation;
|
||||
}
|
||||
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()
|
||||
{
|
||||
#if !UNITY_EDITOR
|
||||
PlayerPrefs.SetString(PkgVersionKey, packageVersion);
|
||||
PlayerPrefs.Save();
|
||||
#endif
|
||||
}
|
||||
public async UniTask<UpdatePackageManifestOperation> UpdatePackageManifest()
|
||||
{
|
||||
var operation = package.UpdatePackageManifestAsync(packageVersion,5);
|
||||
await operation.ToUniTask();
|
||||
return operation;
|
||||
}
|
||||
#endregion <EFBFBD>汾<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>嵥<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
public ResourceDownloaderOperation CreateDownloader()
|
||||
{
|
||||
downloader = package.CreateResourceDownloader(data.downloadingMaxNum, data.failedTryAgain);
|
||||
downloader.DownloadErrorCallback = data.downloadError;
|
||||
downloader.DownloadFinishCallback = data.downloadFinish;
|
||||
downloader.DownloadUpdateCallback = data.downloadUpdate;
|
||||
return downloader;
|
||||
}
|
||||
public async UniTask<bool> DownloadPackageFiles()
|
||||
{
|
||||
if (downloader.TotalDownloadCount == 0)
|
||||
return true;
|
||||
Debug.Log($"{data.packageName} DownloadPackageFiles {downloader.TotalDownloadCount}");
|
||||
downloader.BeginDownload();
|
||||
await downloader.ToUniTask();
|
||||
return downloader.Status == EOperationStatus.Succeed;
|
||||
}
|
||||
public async UniTask<ClearCacheFilesOperation> ClearCacheBundle()
|
||||
{
|
||||
var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearUnusedBundleFiles);
|
||||
await operation.ToUniTask();
|
||||
return operation;
|
||||
}
|
||||
#endregion <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2a463a1d7227c3478645bd706515e73
|
||||
131
Assets/GameFramework/Runtime/PatchLogic/PreloadOperation.cs
Normal file
131
Assets/GameFramework/Runtime/PatchLogic/PreloadOperation.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
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
|
||||
InitializationOperation initializationOperation = await operation.InitializePackage();
|
||||
//<2F><>ʼ<EFBFBD><CABC>ʧ<EFBFBD><CAA7>
|
||||
if (initializationOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent($"{initializationOperation.Error}")
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
Debug.Log($"<22><>ʼ<EFBFBD><CABC><EFBFBD>ɹ<EFBFBD>{operation.data.packageName}");
|
||||
var version = await GetBestPackageVersion();
|
||||
#if UNITY_EDITOR
|
||||
var PackageVersionOperation = await operation.RequestPackageVersion();
|
||||
version = PackageVersionOperation.PackageVersion;
|
||||
#endif
|
||||
//<2F><>ȡ<EFBFBD>汾ʧ<E6B1BE><CAA7>
|
||||
if (version == null)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent("<22><>ȡ<EFBFBD>汾ʧ<E6B1BE><CAA7>")
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
operation.packageVersion = version;
|
||||
Debug.Log($"<22><>ȡ<EFBFBD>汾<EFBFBD>ɹ<EFBFBD>{operation.data.packageName}<7D><>{version}");
|
||||
UpdatePackageManifestOperation updatePackageManifest = await operation.UpdatePackageManifest();
|
||||
//<2F><>ȡ<EFBFBD><C8A1>Դ<EFBFBD>嵥ʧ<E5B5A5><CAA7>
|
||||
if (updatePackageManifest.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(operation.data.packageName)
|
||||
.SetContent(updatePackageManifest.Error)
|
||||
.AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
return;
|
||||
}
|
||||
await LoadAndShowPatchWindow();
|
||||
_ = 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, GameManager.Inst.MainUICanvas.transform);
|
||||
}
|
||||
private async UniTask UpdatePreloadPackage()
|
||||
{
|
||||
var PackageVersionOperation = await operation.RequestPackageVersion();
|
||||
if (PackageVersionOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD><CCA8><EFBFBD>°汾ʧ<E6B1BE>ܣ<EFBFBD>{PackageVersionOperation.Error}");
|
||||
return;
|
||||
}
|
||||
operation.packageVersion = PackageVersionOperation.PackageVersion;
|
||||
var PackageManifestOperation = await operation.UpdatePackageManifest();
|
||||
if (PackageManifestOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>嵥ʧ<E5B5A5>ܣ<EFBFBD>{PackageManifestOperation.Error}");
|
||||
return;
|
||||
}
|
||||
var DownloaderOperation = operation.CreateDownloader();
|
||||
if(DownloaderOperation.TotalDownloadCount == 0)
|
||||
{
|
||||
operation.SaveVersionToCache();
|
||||
Debug.Log($"<22><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>{operation.data.packageName}<7D><><EFBFBD>ɣ<EFBFBD><C9A3>汾<EFBFBD><E6B1BE>{operation.packageVersion}");
|
||||
return;
|
||||
}
|
||||
if (!await operation.DownloadPackageFiles())
|
||||
{
|
||||
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
|
||||
return;
|
||||
}
|
||||
var ClearCacheFilesOperation = await operation.ClearCacheBundle();
|
||||
if (ClearCacheFilesOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
Debug.Log($"{operation.data.packageName}<7D><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>δʹ<CEB4>û<EFBFBD><C3BB><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>{ClearCacheFilesOperation.Error}");
|
||||
return;
|
||||
}
|
||||
operation.SaveVersionToCache();
|
||||
Debug.Log($"<22><>̨<EFBFBD><CCA8><EFBFBD><EFBFBD>{operation.data.packageName}<7D><><EFBFBD>ɣ<EFBFBD><C9A3>汾<EFBFBD><E6B1BE>{operation.packageVersion}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4532eb0a1a183db43aa88ef78d4d9ee8
|
||||
23
Assets/GameFramework/Runtime/Singleton.cs
Normal file
23
Assets/GameFramework/Runtime/Singleton.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
using UnityEngine;
|
||||
|
||||
public class Singleton<T> where T:class,new()
|
||||
{
|
||||
static T _inst;
|
||||
static readonly object _lock = new object();
|
||||
public static T Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if(_inst == null)
|
||||
{
|
||||
_inst = new T();
|
||||
Debug.Log($"[Singleton] 创建 {typeof(T).Name} 实例");
|
||||
}
|
||||
return _inst;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/GameFramework/Runtime/Singleton.cs.meta
Normal file
2
Assets/GameFramework/Runtime/Singleton.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4b9882c53361b3145b8c1dc7a01556dd
|
||||
29
Assets/GameFramework/Runtime/SingletonMono.cs
Normal file
29
Assets/GameFramework/Runtime/SingletonMono.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
private static T _instance;
|
||||
private static readonly object _lock = new object();
|
||||
public static T Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/GameFramework/Runtime/SingletonMono.cs.meta
Normal file
2
Assets/GameFramework/Runtime/SingletonMono.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4cdfe572084940a4e991ea759ce2945a
|
||||
Reference in New Issue
Block a user