Init
This commit is contained in:
55
Assets/Scripts/Runtime/Boot.cs
Normal file
55
Assets/Scripts/Runtime/Boot.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Event;
|
||||
using YooAsset;
|
||||
|
||||
public class Boot : Singleton<Boot>
|
||||
{
|
||||
public GameObject MainUICanvas;
|
||||
public Camera UICamera;
|
||||
public List<string> DepDlls = new List<string>()
|
||||
{
|
||||
"mscorlib.dll",
|
||||
"System.dll",
|
||||
"System.Core.dll",
|
||||
"Mirror.dll"
|
||||
};
|
||||
public List<GameObject> DontDestroyObjs = new List<GameObject>();
|
||||
/// <summary>
|
||||
/// 资源系统运行模式
|
||||
/// </summary>
|
||||
public EPlayMode PlayMode = EPlayMode.EditorSimulateMode;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Debug.Log($"资源系统运行模式:{PlayMode}");
|
||||
Debug.Log($"Application.persistentDataPath:{Application.persistentDataPath}");
|
||||
Application.targetFrameRate = 60;
|
||||
Application.runInBackground = true;
|
||||
}
|
||||
IEnumerator Start()
|
||||
{
|
||||
foreach (GameObject obj in DontDestroyObjs)
|
||||
{
|
||||
DontDestroyOnLoad(obj);
|
||||
}
|
||||
GameManager.Inst.Behaviour = this;
|
||||
GameManager.Inst.MainUICanvas = MainUICanvas;
|
||||
GameManager.Inst.UICamera = UICamera;
|
||||
GameManager.Inst.LoadServerSettings();
|
||||
|
||||
YooAssets.Initialize();
|
||||
|
||||
//更新Preload
|
||||
var operation = new PatchOperation(PatchManager.Inst.PreloadData(PlayMode));
|
||||
YooAssets.StartOperation(operation);
|
||||
yield return operation;
|
||||
|
||||
//加载更新界面,Main更新流程以及GameStart调用在PatchWindow中
|
||||
GameManager.Inst.LoadDll(YooAssets.GetPackage("Preload"), "Preload");
|
||||
AssetHandle patchWindowHandle = YooAssets.GetPackage("Preload").LoadAssetAsync<GameObject>("PatchWindow");
|
||||
patchWindowHandle.InstantiateAsync(GameManager.Inst.MainUICanvas.transform);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Runtime/Boot.cs.meta
Normal file
11
Assets/Scripts/Runtime/Boot.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41fef9a778e4e254b939e9dc3e553bf0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
23
Assets/Scripts/Runtime/MyScripts.Runtime.asmdef
Normal file
23
Assets/Scripts/Runtime/MyScripts.Runtime.asmdef
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "MyScripts.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:e34a5702dd353724aa315fb8011f08c3",
|
||||
"GUID:13ba8ce62aa80c74598530029cb2d649",
|
||||
"GUID:f22fac247a56d2d41b687bb0d900e54e",
|
||||
"GUID:c55575a9f1747c240822f4b7e0400716",
|
||||
"GUID:1ea6b676786131e4095182b742bb64ec",
|
||||
"GUID:fecf25954bb196642ab50657689761d6",
|
||||
"GUID:30817c1a0e6d646d99c048fc403f5979",
|
||||
"GUID:72872094b21c16e48b631b2224833d49"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
7
Assets/Scripts/Runtime/MyScripts.Runtime.asmdef.meta
Normal file
7
Assets/Scripts/Runtime/MyScripts.Runtime.asmdef.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8804cd4b3fef1e041ad4f6c94ec49e0c
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
24
Assets/Scripts/Runtime/Singleton.cs
Normal file
24
Assets/Scripts/Runtime/Singleton.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Singleton<T> : MonoBehaviour where T : MonoBehaviour
|
||||
{
|
||||
private static T _instance;
|
||||
|
||||
public static T Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = FindAnyObjectByType<T>();
|
||||
|
||||
if (_instance == null)
|
||||
{
|
||||
GameObject singletonObject = new GameObject(typeof(T).Name);
|
||||
_instance = singletonObject.AddComponent<T>();
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Runtime/Singleton.cs.meta
Normal file
11
Assets/Scripts/Runtime/Singleton.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1e38d108023ce9d4ca9206ac329be382
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Runtime/YooAsset.meta
Normal file
8
Assets/Scripts/Runtime/YooAsset.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8a154c95ffcec148b4d3170628a4dca
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Runtime/YooAsset/GameLogic.meta
Normal file
8
Assets/Scripts/Runtime/YooAsset/GameLogic.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81ae789e37712a9409c6f805d7236f42
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
81
Assets/Scripts/Runtime/YooAsset/GameLogic/GameManager.cs
Normal file
81
Assets/Scripts/Runtime/YooAsset/GameLogic/GameManager.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using HybridCLR;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
using static UnityEngine.Rendering.VirtualTexturing.Debugging;
|
||||
|
||||
public class GameManager
|
||||
{
|
||||
public MonoBehaviour Behaviour;
|
||||
public string ServerAddress;
|
||||
public bool isServer;
|
||||
public GameObject MainUICanvas;
|
||||
public Camera UICamera;
|
||||
|
||||
private static GameManager _instance;
|
||||
public static GameManager Inst
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
_instance = new GameManager();
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
public void StartCoroutine(IEnumerator enumerator)
|
||||
{
|
||||
Behaviour.StartCoroutine(enumerator);
|
||||
}
|
||||
|
||||
public void LoadDll(ResourcePackage package, string dll)
|
||||
{
|
||||
if (package.GetAssetInfo(dll).Error == string.Empty)
|
||||
{
|
||||
AssetHandle handle = package.LoadAssetSync<TextAsset>(dll);
|
||||
#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 void LoadDepDll(ResourcePackage package,List<string> dlls)
|
||||
{
|
||||
foreach (string dll in dlls)
|
||||
{
|
||||
if (package.GetAssetInfo(dll).Error == string.Empty)
|
||||
{
|
||||
AssetHandle handle = package.LoadAssetSync<TextAsset>(dll);
|
||||
RuntimeApi.LoadMetadataForAOTAssembly((handle.AssetObject as TextAsset).bytes, HomologousImageMode.SuperSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void LoadServerSettings()
|
||||
{
|
||||
string settingsPath = Path.Combine(Application.persistentDataPath, "settings.json");
|
||||
if (File.Exists(settingsPath))
|
||||
{
|
||||
string json = File.ReadAllText(settingsPath);
|
||||
ServerAddress = JsonUtility.FromJson<ServerSettings>(json).serverAddress;
|
||||
isServer = JsonUtility.FromJson<ServerSettings>(json).isServer;
|
||||
Debug.Log(ServerAddress);
|
||||
}
|
||||
else
|
||||
{
|
||||
string json = JsonUtility.ToJson(new ServerSettings(), true);
|
||||
File.WriteAllText(settingsPath, json);
|
||||
}
|
||||
}
|
||||
[System.Serializable]
|
||||
public class ServerSettings
|
||||
{
|
||||
public string serverAddress;
|
||||
public bool isServer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f6630e7de79efb24b9263519ba4282e1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Runtime/YooAsset/PatchLogic.meta
Normal file
8
Assets/Scripts/Runtime/YooAsset/PatchLogic.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 81fbe3bfa76809b4d82e46e8889d805e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Runtime/YooAsset/PatchLogic/FsmNode.meta
Normal file
8
Assets/Scripts/Runtime/YooAsset/PatchLogic/FsmNode.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 22a43ec58b9da1743bfa629be08ef1ac
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
internal class FsmClearCacheBundle : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
var packageName = data.packageName;
|
||||
var package = YooAssets.GetPackage(packageName);
|
||||
var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearUnusedBundleFiles);
|
||||
operation.Completed += Operation_Completed;
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private void Operation_Completed(YooAsset.AsyncOperationBase obj)
|
||||
{
|
||||
_machine.ChangeState<FsmStartGame>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d83ce785f1442a40a1ad0f837073b69
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,68 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
public class FsmCreateDownloader : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
Debug.Log($"创建资源下载器{data.packageName}");
|
||||
CreateDownloader();
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
void CreateDownloader()
|
||||
{
|
||||
var packageName = data.packageName;
|
||||
var package = YooAssets.GetPackage(packageName);
|
||||
var downloader = package.CreateResourceDownloader(10, 3);
|
||||
downloader.DownloadErrorCallback = data.downloadError;
|
||||
downloader.DownloadFinishCallback = data.downloadFinish;
|
||||
downloader.DownloadUpdateCallback = data.downloadUpdate;
|
||||
var curVersion = PlayerPrefs.GetString($"{packageName}_Version", string.Empty);
|
||||
var packageVersion = (string)_machine.GetBlackboardValue($"{packageName}_Version");
|
||||
_machine.SetBlackboardValue($"{packageName}_Downloader", downloader);
|
||||
|
||||
if (downloader.TotalDownloadCount == 0)
|
||||
{
|
||||
Debug.Log("Not found any download files !");
|
||||
_machine.ChangeState<FsmStartGame>();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.autoDownload)
|
||||
{
|
||||
_machine.ChangeState<FsmDownloadPackageFiles>();
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(packageName)
|
||||
.SetContent($"发现资源更新\n{curVersion}=>{packageVersion}: {downloader.TotalDownloadBytes / 1024f / 1024f:F1}MB")
|
||||
.AddButton("下载", (box) => { _machine.ChangeState<FsmDownloadPackageFiles>(); })
|
||||
.AddButton("取消", (box) =>
|
||||
{
|
||||
downloader.CancelDownload();
|
||||
data.useLocal = true;
|
||||
_machine.SetBlackboardValue("PatchOperationData", data);
|
||||
_machine.ChangeState<FsmRequestPackageVersion>();
|
||||
})
|
||||
.AddButton("退出", (box) => { Application.Quit(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8b104741ebcb72946abe282c1da73360
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
public class FsmDownloadPackageFiles : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
Debug.Log($"{data.packageName}开始下载资源文件");
|
||||
GameManager.Inst.StartCoroutine(BeginDownload());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator BeginDownload()
|
||||
{
|
||||
var downloader = (ResourceDownloaderOperation)_machine.GetBlackboardValue($"{data.packageName}_Downloader");
|
||||
downloader.BeginDownload();
|
||||
yield return downloader;
|
||||
|
||||
// 检测下载结果
|
||||
if (downloader.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(data.packageName)
|
||||
.SetContent($"下载失败{downloader.Error}")
|
||||
.AddButton("退出", (box) => { Application.Quit(); });
|
||||
yield break;
|
||||
}
|
||||
_machine.ChangeState<FsmDownloadPackageOver>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a3fae22040fe2d541ab4582c7c1c71fb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
internal class FsmDownloadPackageOver : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
Debug.Log($"{data.packageName}下载完成");
|
||||
//// 注意:下载完成之后再保存本地版本
|
||||
//if (data.playMode != EPlayMode.EditorSimulateMode)
|
||||
//{
|
||||
// var packageVersion = (string)_machine.GetBlackboardValue($"{data.packageName}_Version");
|
||||
// PlayerPrefs.SetString($"{data.packageName}_Version", packageVersion);
|
||||
// Debug.Log($"{data.packageName} 更新流程完毕 保存版本{packageVersion}");
|
||||
//}
|
||||
_machine.ChangeState<FsmClearCacheBundle>();
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c96c57c65f74bb3469dc71b13ef78c84
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,163 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
internal class FsmInitializePackage : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
Debug.Log($"初始化{data.packageName},下载路径:{GetHostServerURL(data.packageName)}");
|
||||
GameManager.Inst.StartCoroutine(InitPackage());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator InitPackage()
|
||||
{
|
||||
var playMode = data.playMode;
|
||||
var packageName = data.packageName;
|
||||
|
||||
// 创建资源包裹类
|
||||
var package = YooAssets.TryGetPackage(packageName);
|
||||
if (package == null)
|
||||
package = YooAssets.CreatePackage(packageName);
|
||||
// 编辑器下的模拟模式
|
||||
InitializationOperation initializationOperation = null;
|
||||
if (playMode == EPlayMode.EditorSimulateMode)
|
||||
{
|
||||
var buildResult = EditorSimulateModeHelper.SimulateBuild(packageName);
|
||||
var packageRoot = buildResult.PackageRootDirectory;
|
||||
var createParameters = new EditorSimulateModeParameters();
|
||||
createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(packageRoot);
|
||||
initializationOperation = package.InitializeAsync(createParameters);
|
||||
}
|
||||
|
||||
// 单机运行模式
|
||||
if (playMode == EPlayMode.OfflinePlayMode)
|
||||
{
|
||||
var createParameters = new OfflinePlayModeParameters();
|
||||
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
|
||||
initializationOperation = package.InitializeAsync(createParameters);
|
||||
}
|
||||
|
||||
// 联机运行模式
|
||||
if (playMode == EPlayMode.HostPlayMode)
|
||||
{
|
||||
// 注意:设置参数COPY_BUILDIN_PACKAGE_MANIFEST,可以初始化的时候拷贝内置清单到沙盒目录
|
||||
var buildinFileSystemParams = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
|
||||
buildinFileSystemParams.AddParameter(FileSystemParametersDefine.COPY_BUILDIN_PACKAGE_MANIFEST, true);
|
||||
string defaultHostServer = GetHostServerURL(packageName);
|
||||
string fallbackHostServer = GetHostServerURL(packageName);
|
||||
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
||||
// 注意:设置参数INSTALL_CLEAR_MODE,可以解决覆盖安装的时候将拷贝的内置清单文件清理的问题。
|
||||
var cacheFileSystemParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
|
||||
cacheFileSystemParams.AddParameter(FileSystemParametersDefine.INSTALL_CLEAR_MODE, EOverwriteInstallClearMode.None);
|
||||
|
||||
var createParameters = new HostPlayModeParameters();
|
||||
createParameters.BuildinFileSystemParameters = packageName == "Preload" ? buildinFileSystemParams : null;
|
||||
createParameters.CacheFileSystemParameters = cacheFileSystemParams;
|
||||
initializationOperation = package.InitializeAsync(createParameters);
|
||||
}
|
||||
|
||||
// WebGL运行模式
|
||||
if (playMode == EPlayMode.WebPlayMode)
|
||||
{
|
||||
#if UNITY_WEBGL && WEIXINMINIGAME && !UNITY_EDITOR
|
||||
var createParameters = new WebPlayModeParameters();
|
||||
string defaultHostServer = GetHostServerURL(packageName);
|
||||
string fallbackHostServer = GetHostServerURL(packageName);
|
||||
string packageRoot = $"{WeChatWASM.WX.env.USER_DATA_PATH}/__GAME_FILE_CACHE"; //注意:如果有子目录,请修改此处!
|
||||
IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
||||
createParameters.WebServerFileSystemParameters = WechatFileSystemCreater.CreateFileSystemParameters(packageRoot, remoteServices);
|
||||
initializationOperation = package.InitializeAsync(createParameters);
|
||||
#else
|
||||
var createParameters = new WebPlayModeParameters();
|
||||
createParameters.WebServerFileSystemParameters = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
|
||||
initializationOperation = package.InitializeAsync(createParameters);
|
||||
#endif
|
||||
}
|
||||
|
||||
yield return initializationOperation;
|
||||
|
||||
// 如果初始化失败弹出提示界面
|
||||
if (initializationOperation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(packageName)
|
||||
.SetContent($"{initializationOperation.Error}")
|
||||
.AddButton("退出", (box) => { Application.Quit(); });
|
||||
}
|
||||
else
|
||||
{
|
||||
_machine.ChangeState<FsmRequestPackageVersion>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取资源服务器地址
|
||||
/// </summary>
|
||||
public string GetHostServerURL(string packageName)
|
||||
{
|
||||
string hostServerIP = $"http://localhost:8080/{Application.productName}";
|
||||
if (GameManager.Inst.ServerAddress != "" && GameManager.Inst.ServerAddress != null)
|
||||
hostServerIP = $"{GameManager.Inst.ServerAddress}/{Application.productName}";
|
||||
string appVersion = "v1";
|
||||
#if UNITY_EDITOR
|
||||
if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android)
|
||||
return $"{hostServerIP}/CDN/Android/{packageName}/{appVersion}";
|
||||
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS)
|
||||
return $"{hostServerIP}/CDN/IPhone/{packageName}/{appVersion}";
|
||||
else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL)
|
||||
return $"{hostServerIP}/CDN/WebGL/{packageName}/{appVersion}";
|
||||
else
|
||||
return $"{hostServerIP}/CDN/PC/{packageName}/{appVersion}";
|
||||
#else
|
||||
if (Application.platform == RuntimePlatform.Android)
|
||||
return $"{hostServerIP}/CDN/Android/{packageName}/{appVersion}";
|
||||
else if (Application.platform == RuntimePlatform.IPhonePlayer)
|
||||
return $"{hostServerIP}/CDN/IPhone/{packageName}/{appVersion}";
|
||||
else if (Application.platform == RuntimePlatform.WebGLPlayer)
|
||||
return $"{hostServerIP}/CDN/WebGL/{packageName}/{appVersion}";
|
||||
else
|
||||
return $"{hostServerIP}/CDN/PC/{packageName}/{appVersion}";
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 远端资源地址查询服务类
|
||||
/// </summary>
|
||||
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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c67e6e479b68e345afcdf325775c079
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,60 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
internal class FsmRequestPackageVersion : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
Debug.Log($"更新版本信息{data.packageName}");
|
||||
GameManager.Inst.StartCoroutine(UpdatePackageVersion());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator UpdatePackageVersion()
|
||||
{
|
||||
var packageName = data.packageName;
|
||||
var package = YooAssets.GetPackage(packageName);
|
||||
var operation = package.RequestPackageVersionAsync(true,10);
|
||||
var curVersion = PlayerPrefs.GetString($"{packageName}_Version", string.Empty);
|
||||
yield return operation;
|
||||
|
||||
if (operation.Status != EOperationStatus.Succeed || data.useLocal)
|
||||
{
|
||||
Debug.LogWarning(operation.Error);
|
||||
if (string.IsNullOrEmpty(curVersion))
|
||||
{
|
||||
MessageBox.Show()
|
||||
.SetTitle(packageName)
|
||||
.SetContent(operation.Error)
|
||||
.AddButton("退出", (box) => { Application.Quit(); });
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"{packageName}获取上次成功记录的版本{curVersion}");
|
||||
_machine.SetBlackboardValue($"{packageName}_Version", curVersion);
|
||||
_machine.ChangeState<FsmUpdatePackageManifest>();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"{packageName}获取远端资源版本成功{operation.PackageVersion}");
|
||||
_machine.SetBlackboardValue($"{packageName}_Version", operation.PackageVersion);
|
||||
_machine.ChangeState<FsmUpdatePackageManifest>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f5ea76dfe0fc52b47a00a3bf68a54d09
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
internal class FsmStartGame : IStateNode
|
||||
{
|
||||
private PatchOperation _owner;
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_owner = machine.Owner as PatchOperation;
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
if (data.playMode != EPlayMode.EditorSimulateMode)
|
||||
{
|
||||
var packageVersion = (string)_machine.GetBlackboardValue($"{data.packageName}_Version");
|
||||
PlayerPrefs.SetString($"{data.packageName}_Version", packageVersion);
|
||||
Debug.Log($"{data.packageName} 更新流程完毕 保存版本{packageVersion}");
|
||||
}
|
||||
_owner.SetFinish();
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6cf80cfbed474c34b892eaeda7fcb054
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using YooAsset;
|
||||
|
||||
public class FsmUpdatePackageManifest : IStateNode
|
||||
{
|
||||
private StateMachine _machine;
|
||||
PatchOperationData data;
|
||||
void IStateNode.OnCreate(StateMachine machine)
|
||||
{
|
||||
_machine = machine;
|
||||
}
|
||||
void IStateNode.OnEnter()
|
||||
{
|
||||
data = (PatchOperationData)_machine.GetBlackboardValue("PatchOperationData");
|
||||
Debug.Log($"更新资源清单{data.packageName}");
|
||||
GameManager.Inst.StartCoroutine(UpdateManifest());
|
||||
}
|
||||
void IStateNode.OnUpdate()
|
||||
{
|
||||
}
|
||||
void IStateNode.OnExit()
|
||||
{
|
||||
}
|
||||
|
||||
private IEnumerator UpdateManifest()
|
||||
{
|
||||
var packageName = data.packageName;
|
||||
var packageVersion = (string)_machine.GetBlackboardValue($"{packageName}_Version");
|
||||
var package = YooAssets.GetPackage(packageName);
|
||||
var operation = package.UpdatePackageManifestAsync(packageVersion);
|
||||
yield return operation;
|
||||
|
||||
if (operation.Status != EOperationStatus.Succeed)
|
||||
{
|
||||
Debug.LogWarning(operation.Error);
|
||||
MessageBox.Show()
|
||||
.SetTitle(packageName)
|
||||
.SetContent(operation.Error)
|
||||
.AddButton("退出", (box) => { Application.Quit(); });
|
||||
yield break;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"{packageName}资源清单更新成功");
|
||||
_machine.ChangeState<FsmCreateDownloader>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13faaa12de67f5e4db31cd8a44563089
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
39
Assets/Scripts/Runtime/YooAsset/PatchLogic/PatchManager.cs
Normal file
39
Assets/Scripts/Runtime/YooAsset/PatchLogic/PatchManager.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using YooAsset;
|
||||
|
||||
public class PatchManager : Singleton<PatchManager>
|
||||
{
|
||||
public PatchOperationData PreloadData(EPlayMode PlayMode)
|
||||
{
|
||||
PatchOperationData preload = new PatchOperationData();
|
||||
preload.packageName = "Preload";
|
||||
preload.playMode = PlayMode;
|
||||
preload.autoDownload = true;
|
||||
//preload.downloadError = DefDownloadError;
|
||||
//preload.downloadFinish = DefDownloadFinsh;
|
||||
return preload;
|
||||
}
|
||||
|
||||
public PatchOperationData MainData(EPlayMode PlayMode, DownloaderOperation.DownloadUpdate DownloadUpdate)
|
||||
{
|
||||
PatchOperationData main = new PatchOperationData();
|
||||
main.packageName = "Main";
|
||||
main.playMode = PlayMode;
|
||||
main.autoDownload = GameManager.Inst.isServer?true:false;
|
||||
//main.downloadError = DefDownloadError;
|
||||
main.downloadUpdate = DownloadUpdate;
|
||||
//main.downloadFinish = DefDownloadFinsh;
|
||||
return main;
|
||||
}
|
||||
|
||||
//Ĭ<><C4AC><EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD><D8B4><EFBFBD>ص<EFBFBD>
|
||||
//public void DefDownloadError(DownloadErrorData downloadErrorData)
|
||||
//{
|
||||
// MessageBox.Show()
|
||||
// .SetTitle(packageName)
|
||||
// .SetContent(downloadErrorData.ErrorInfo)
|
||||
// .AddButton("<22>˳<EFBFBD>", (box) => { Application.Quit(); });
|
||||
//}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45a555daf207acb4cb579cd5bdf270a5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
72
Assets/Scripts/Runtime/YooAsset/PatchLogic/PatchOperation.cs
Normal file
72
Assets/Scripts/Runtime/YooAsset/PatchLogic/PatchOperation.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using UnityEngine;
|
||||
using UniFramework.Machine;
|
||||
using UniFramework.Event;
|
||||
using YooAsset;
|
||||
|
||||
public class PatchOperation : GameAsyncOperation
|
||||
{
|
||||
private enum ESteps
|
||||
{
|
||||
None,
|
||||
Update,
|
||||
Done,
|
||||
}
|
||||
|
||||
private readonly EventGroup _eventGroup = new EventGroup();
|
||||
private readonly StateMachine _machine;
|
||||
private readonly string _packageName;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
public PatchOperation(PatchOperationData data)
|
||||
{
|
||||
_packageName = data.packageName;
|
||||
// 创建状态机
|
||||
_machine = new StateMachine(this);
|
||||
_machine.AddNode<FsmInitializePackage>();
|
||||
_machine.AddNode<FsmRequestPackageVersion>();
|
||||
_machine.AddNode<FsmUpdatePackageManifest>();
|
||||
_machine.AddNode<FsmCreateDownloader>();
|
||||
_machine.AddNode<FsmDownloadPackageFiles>();
|
||||
_machine.AddNode<FsmDownloadPackageOver>();
|
||||
_machine.AddNode<FsmClearCacheBundle>();
|
||||
_machine.AddNode<FsmStartGame>();
|
||||
|
||||
_machine.SetBlackboardValue("PatchOperationData", data);
|
||||
}
|
||||
protected override void OnStart()
|
||||
{
|
||||
_steps = ESteps.Update;
|
||||
_machine.Run<FsmInitializePackage>();
|
||||
}
|
||||
protected override void OnUpdate()
|
||||
{
|
||||
if (_steps == ESteps.None || _steps == ESteps.Done)
|
||||
return;
|
||||
|
||||
if (_steps == ESteps.Update)
|
||||
{
|
||||
_machine.Update();
|
||||
}
|
||||
}
|
||||
protected override void OnAbort()
|
||||
{
|
||||
}
|
||||
|
||||
public void SetFinish()
|
||||
{
|
||||
_steps = ESteps.Done;
|
||||
_eventGroup.RemoveAllListener();
|
||||
Status = EOperationStatus.Succeed;
|
||||
Debug.Log($"Package {_packageName} patch done !");
|
||||
}
|
||||
}
|
||||
public class PatchOperationData
|
||||
{
|
||||
public string packageName;
|
||||
public EPlayMode playMode;
|
||||
public bool autoDownload;
|
||||
public bool useLocal;
|
||||
public DownloaderOperation.DownloadError downloadError;
|
||||
public DownloaderOperation.DownloaderFinish downloadFinish;
|
||||
public DownloaderOperation.DownloadUpdate downloadUpdate;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4c1b4cadd806a1a409db720564a44a83
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
8
Assets/Scripts/Runtime/YooAsset/WindowLogic.meta
Normal file
8
Assets/Scripts/Runtime/YooAsset/WindowLogic.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b859cdb20aace34b9b9080421cfc9e9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
86
Assets/Scripts/Runtime/YooAsset/WindowLogic/MessageBox.cs
Normal file
86
Assets/Scripts/Runtime/YooAsset/WindowLogic/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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 639f3e5fe9a96404293c528ac57562dd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user