【m】框架优化
This commit is contained in:
@@ -1,81 +1,33 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Main;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEditor;
|
||||
|
||||
#if Immersal
|
||||
using Immersal.AR;
|
||||
#endif
|
||||
using Stary.Evo;
|
||||
using Stary.Evo.InformationSave;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
using UnityEngine.SceneManagement;
|
||||
using YooAsset;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Stary.Evo
|
||||
{
|
||||
public class ResStartState : AbstractFSMIStateAsync
|
||||
{
|
||||
private DomainConfig.LoadResType loadResType;
|
||||
public GameObject mainPrefab;
|
||||
private DomainConfig domainConfig;
|
||||
|
||||
private string _sceneName;
|
||||
|
||||
public ResStartState(IFsmSystemAsync system) : base(system)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public override async UniTask OnEnterAsync()
|
||||
public override UniTask OnEnterAsync()
|
||||
{
|
||||
Debug.Log("UnityEvo:启动开始资源初始化...");
|
||||
|
||||
|
||||
// 初始化资源系统
|
||||
YooAssets.Initialize();
|
||||
//初始化资源加载模块
|
||||
// 增加包存在性检查
|
||||
var package = YooAssets.TryGetPackage(AppConfig.PackageDomainName);
|
||||
if (package == null)
|
||||
{
|
||||
Debug.LogWarning($"UnityEvo:资源包 {AppConfig.PackageDomainName} 不存在,正在尝试创建...");
|
||||
package = YooAssets.CreatePackage(AppConfig.PackageDomainName);
|
||||
}
|
||||
|
||||
YooAssets.SetDefaultPackage(package);
|
||||
|
||||
// 初始化资源包
|
||||
#if EDITOR_SIMULATEMODE
|
||||
await EDITOR_SIMULATEMODE(package);
|
||||
await FsmSystem.SetCurState(nameof(ResEditorSimulateState));
|
||||
#elif OFFLINE_PLAYMODE
|
||||
await OFFLINE_PLAYMODE(package);
|
||||
await FsmSystem.SetCurState(nameof(ResUpdateLocalState));
|
||||
#elif HOST_PLAYMODE
|
||||
//登录
|
||||
if (((FsmLoadSystem)FsmSystem).IsLogin)
|
||||
await HOST_PLAYMODE(package);
|
||||
else
|
||||
{
|
||||
await OFFLINE_PLAYMODE(package);
|
||||
await ChChe_PLAYMODE(package);
|
||||
}
|
||||
|
||||
|
||||
await FsmSystem.SetCurState(nameof(ResUpdateLocalState));
|
||||
|
||||
#elif WEB_PLAYMODE
|
||||
// IRemoteServices remoteServices = new RemoteServices(defaultHostServer, fallbackHostServer);
|
||||
// var webServerFileSystemParams = FileSystemParameters.CreateDefaultWebServerFileSystemParameters();
|
||||
// var webRemoteFileSystemParams =
|
||||
// FileSystemParameters.CreateDefaultWebRemoteFileSystemParameters(remoteServices); //支持跨域下载
|
||||
//
|
||||
// var initParameters = new WebPlayModeParameters();
|
||||
// initParameters.WebServerFileSystemParameters = webServerFileSystemParams;
|
||||
// initParameters.WebRemoteFileSystemParameters = webRemoteFileSystemParams;
|
||||
//
|
||||
// var initOperation = package.InitializeAsync(initParameters);
|
||||
// await initOperation;
|
||||
//
|
||||
// if (initOperation.Status == EOperationStatus.Succeed)
|
||||
// Debug.Log("UnityEvo:资源包初始化成功!");
|
||||
// else
|
||||
// Debug.LogError($"UnityEvo:资源包初始化失败:{initOperation.Error}");
|
||||
|
||||
Debug.LogError($"UnityEvo:暂不支持");
|
||||
#endif
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override UniTask OnEnterAsync<T>(T param)
|
||||
@@ -83,233 +35,162 @@ namespace Stary.Evo
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override UniTask OnEnterAsync<T1, T2>(T1 param1, T2 param2)
|
||||
public override async UniTask OnEnterAsync<T1, T2>(T1 param1, T2 param2)
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override void OnUpdate()
|
||||
{
|
||||
base.OnUpdate();
|
||||
}
|
||||
|
||||
|
||||
public override UniTask OnExitAsync()
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
private async UniTask EDITOR_SIMULATEMODE(ResourcePackage package)
|
||||
{
|
||||
var initParams = YooAssetFileSystem.EditorSimulateInitializeParameter(package.PackageName);
|
||||
var initialization = package.InitializeAsync(initParams);
|
||||
await initialization;
|
||||
if (initialization.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
Assert.AreEqual(EOperationStatus.Succeed, initialization.Status);
|
||||
Debug.Log("UnityEvo:资源包初始化成功!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"UnityEvo:资源包初始化失败:{initialization.Error}");
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask ChChe_PLAYMODE(ResourcePackage package)
|
||||
{
|
||||
//Debug.Log("UnityEvo:网络连接不通畅,切换缓存加载!");
|
||||
var initParams = YooAssetFileSystem.HostInitializeParameter();
|
||||
var initOperation = package.InitializeAsync(initParams);
|
||||
await initOperation;
|
||||
if (initOperation.Status == EOperationStatus.Succeed)
|
||||
Debug.Log("UnityEvo:从本地缓存中资源包,初始化成功!");
|
||||
else
|
||||
{
|
||||
Debug.LogError($"UnityEvo:从本地缓存中资源包,初始化失败:{initOperation.Error}");
|
||||
}
|
||||
|
||||
|
||||
var operation = package.RequestPackageVersionAsync();
|
||||
await operation;
|
||||
if (operation.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION", operation.PackageVersion);
|
||||
Debug.Log("UnityEvo:从本地缓存中加载资源包,初始化获取版本号成功!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"UnityEvo:从本地缓存中加载资源包,初始化获取版本号失败!");
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask OFFLINE_PLAYMODE(ResourcePackage package)
|
||||
{
|
||||
Debug.Log("UnityEvo:网络连接不通畅,切换缓存加载!");
|
||||
CopyLocalFile();
|
||||
// var initParams = YooAssetFileSystem.OfflineInitializeParameter();
|
||||
// var initOperation = package.InitializeAsync(initParams);
|
||||
// await initOperation;
|
||||
//
|
||||
// var operation = package.RequestPackageVersionAsync();
|
||||
// await operation;
|
||||
// if (operation.Status == EOperationStatus.Succeed)
|
||||
// {
|
||||
// PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION", operation.PackageVersion);
|
||||
// Debug.Log("UnityEvo:从本地StreamingAssets加载资源包,初始化获取版本号成功!");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Debug.LogError($"UnityEvo:从本地StreamingAssets加载资源包,初始化获取版本号失败!");
|
||||
// }
|
||||
//
|
||||
// if (initOperation.Status == EOperationStatus.Succeed)
|
||||
// Debug.Log("UnityEvo:从本地StreamingAssets加载资源包,初始化成功!");
|
||||
// else
|
||||
// Debug.LogError($"UnityEvo:从本地StreamingAssets加载资源包,初始化失败:{initOperation.Error}");
|
||||
}
|
||||
|
||||
private void CopyLocalFile()
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
// 将StreamingAssets下指定的package拷贝到目标路径
|
||||
string sourcePath = Path.Combine(Application.streamingAssetsPath,
|
||||
YooAssetSettingsData.GetDefaultYooFolderName(), AppConfig.PackageDomainName);
|
||||
string loadPath = Path.Combine(Application.temporaryCachePath, "DownloadedContent",
|
||||
AppConfig.PackageDomainName);
|
||||
|
||||
// 创建目标目录(如果不存在)
|
||||
if (!Directory.Exists(loadPath))
|
||||
{
|
||||
Directory.CreateDirectory(loadPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (HybridClREntrance.Global.stage == StageType.Originality)
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查源路径是否存在
|
||||
if (Directory.Exists(sourcePath))
|
||||
domainConfig = param1 as DomainConfig;
|
||||
loadResType = domainConfig.loadResType;
|
||||
Type type = param2 as Type;
|
||||
var package = YooAssets.GetPackage(domainConfig.domain);
|
||||
switch (loadResType)
|
||||
{
|
||||
// 拷贝所有文件
|
||||
string[] files = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories);
|
||||
foreach (string file in files)
|
||||
{
|
||||
// 计算目标文件路径
|
||||
string relativePath = Path.GetRelativePath(sourcePath, file);
|
||||
string destFile = Path.Combine(loadPath, relativePath);
|
||||
case DomainConfig.LoadResType.Prefab:
|
||||
|
||||
// 创建目标子目录(如果不存在)
|
||||
string destDir = Path.GetDirectoryName(destFile);
|
||||
if (!Directory.Exists(destDir))
|
||||
await LoadDomainPrefab(package);
|
||||
|
||||
break;
|
||||
case DomainConfig.LoadResType.Scene:
|
||||
|
||||
var sceneMode = domainConfig.loadSceneMode;
|
||||
var physicsMode = LocalPhysicsMode.None;
|
||||
SceneHandle handle = package.LoadSceneAsync(domainConfig.sceneIdentifier, sceneMode, physicsMode);
|
||||
await handle;
|
||||
|
||||
Scene targetScene = SceneManager.GetSceneByName(handle.SceneName);
|
||||
// targetScene.name = domainConfig.mainScene;
|
||||
// 设置为 active scene 或者后续 Move 到该 scene
|
||||
SceneManager.SetActiveScene(targetScene);
|
||||
_sceneName = targetScene.name;
|
||||
mainPrefab = GameObject.Find(domainConfig.mainPrefab);
|
||||
if (mainPrefab == null)
|
||||
{
|
||||
Directory.CreateDirectory(destDir);
|
||||
await LoadDomainPrefab(package);
|
||||
}
|
||||
|
||||
// 拷贝文件
|
||||
File.Copy(file, destFile, true);
|
||||
}
|
||||
|
||||
Debug.Log($"UnityEvo:成功将StreamingAssets下的{AppConfig.PackageDomainName}包拷贝到{loadPath}");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"UnityEvo:StreamingAssets下的{AppConfig.PackageDomainName}包不存在");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public async UniTask HOST_PLAYMODE(ResourcePackage package)
|
||||
{
|
||||
// 新增平台判断代码
|
||||
#if UNITY_EDITOR
|
||||
BuildTarget buildTarget = UnityEditor.EditorUserBuildSettings.activeBuildTarget;
|
||||
AppConfig.Platform = buildTarget.ToString();
|
||||
#else
|
||||
AppConfig.Platform = Application.platform.ToString();
|
||||
#endif
|
||||
Debug.Log($"目标平台标识: {AppConfig.Platform}");
|
||||
if (domainConfig.domain != "Main")
|
||||
{
|
||||
LocalTransformInfo info = mainPrefab.GetOrAddComponent<LocalTransformInfo>();
|
||||
FsmLoadSystem fsmLoadSystem = FsmSystem as FsmLoadSystem;
|
||||
|
||||
// 请求资源版本
|
||||
string url = $"{AppConfig.IpConfig}/ResDomain/GetResDomainByDomain";
|
||||
var resDmainRequst = new ResDmainRequst()
|
||||
{
|
||||
ProductName = AppConfig.ProductName,
|
||||
DomainName = AppConfig.PackageDomainName,
|
||||
Platform = AppConfig.Platform,
|
||||
};
|
||||
//获取服务器版本
|
||||
var resDmainMessageEntity = await WebRequestSystem.Post(url, JsonConvert.SerializeObject(resDmainRequst));
|
||||
if (resDmainMessageEntity.code == 200)
|
||||
{
|
||||
ResDmainResponse resDmainResponse =
|
||||
JsonConvert.DeserializeObject<ResDmainResponse>(resDmainMessageEntity.data.ToString());
|
||||
//获取当前版本
|
||||
var oldVersion = PlayerPrefs.GetString($"{AppConfig.PackageDomainName}_GAME_VERSION");
|
||||
//版本不一致,开始下载
|
||||
if (resDmainResponse.PackageVersion != oldVersion)
|
||||
if (info._list.Count >= 2)
|
||||
{
|
||||
await Download(resDmainResponse.DocumentFileId);
|
||||
PlayerPrefs.SetString($"{AppConfig.PackageDomainName}_GAME_VERSION",
|
||||
resDmainResponse.PackageVersion);
|
||||
if (fsmLoadSystem.GetOpenDomainType() == OpenDomainType.PointCloud)
|
||||
{
|
||||
info.Switch(1);
|
||||
}
|
||||
else if (fsmLoadSystem.GetOpenDomainType() == OpenDomainType.VIOICE)
|
||||
{
|
||||
info.Switch(0);
|
||||
}
|
||||
else if (fsmLoadSystem.GetOpenDomainType() == OpenDomainType.ImageTracked)
|
||||
{
|
||||
info.transform.position = fsmLoadSystem.GetTransformCtor().position;
|
||||
info.transform.rotation = Quaternion.Euler(fsmLoadSystem.GetTransformCtor().rotation);
|
||||
info.transform.localScale = fsmLoadSystem.GetTransformCtor().scale;
|
||||
}
|
||||
else
|
||||
{
|
||||
info.Switch(0);
|
||||
}
|
||||
}
|
||||
else //版本一致,加载缓存资源
|
||||
else
|
||||
{
|
||||
Debug.Log($"UnityEvo:资源版本一致,自动跳过更新...");
|
||||
Debug.LogError($"UnityEvo:{mainPrefab.name}的TransformInfo长度小于2,无法继续运行,请排查");
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
if (mainPrefab != null)
|
||||
{
|
||||
Debug.LogError($"UnityEvo:获取资源版本失败: 【{resDmainMessageEntity.message}】");
|
||||
DomainBase hotfixInstance = mainPrefab.GetComponent(type) as DomainBase;
|
||||
if (hotfixInstance == null)
|
||||
{
|
||||
hotfixInstance = mainPrefab.AddComponent(type) as DomainBase;
|
||||
}
|
||||
|
||||
if (hotfixInstance == null)
|
||||
{
|
||||
Debug.LogError($"热更类{type.Name}实例创建失败!必须继承MonoBehaviour");
|
||||
return;
|
||||
}
|
||||
|
||||
hotfixInstance.DomainName = domainConfig.domain;
|
||||
hotfixInstance.DomainNameRaw = $"{domainConfig.domain}_RawFile";
|
||||
|
||||
|
||||
// 原有调用逻辑修改为使用实例
|
||||
hotfixInstance.OnEnter("");
|
||||
hotfixInstance.OnEnterAsync("");
|
||||
}
|
||||
|
||||
var initParams = YooAssetFileSystem.HostInitializeParameter();
|
||||
// initParameters.CacheFileSystemParameters = cacheFileSystemParams;
|
||||
|
||||
var initOperation = package.InitializeAsync(initParams);
|
||||
|
||||
await initOperation;
|
||||
|
||||
if (initOperation.Status == EOperationStatus.Succeed)
|
||||
Debug.Log("UnityEvo:从远程加载资源包,初始化成功!");
|
||||
else
|
||||
Debug.LogError($"UnityEvo:从远程加载资源包,初始化失败:{initOperation.Error}");
|
||||
}
|
||||
|
||||
public async UniTask Download(string fileId)
|
||||
private async UniTask LoadDomainPrefab(ResourcePackage package)
|
||||
{
|
||||
// 在任意MonoBehaviour或DomainBase派生类中
|
||||
string loadPath = Path.Combine(Application.temporaryCachePath, "DownloadedContent",
|
||||
AppConfig.PackageDomainName);
|
||||
//string loadPath = $"{Application.persistentDataPath}/DownloadedContent/{AppConfig.PackageDomainName}";
|
||||
if (Directory.Exists(loadPath))
|
||||
{
|
||||
Directory.Delete(loadPath, true);
|
||||
// 等一帧,让系统真正释放句柄
|
||||
await UniTask.DelayFrame(1);
|
||||
}
|
||||
// 加载热更资源
|
||||
var loadOperation = package.LoadAssetAsync<GameObject>(domainConfig.mainPrefab);
|
||||
|
||||
FsmLoadSystem loadSystem = FsmSystem as FsmLoadSystem;
|
||||
if (loadSystem.ProgressBarPanel == null)
|
||||
await loadOperation;
|
||||
if (loadOperation.Status == EOperationStatus.Succeed)
|
||||
{
|
||||
loadSystem.ProgressBarPanel = Object.Instantiate(Resources.Load<GameObject>("ProgressBarPanel"),
|
||||
Camera.main.transform).GetOrAddComponent<ProgressBarPanel>();
|
||||
#if Immersal
|
||||
ARSpace arSpace = GameObject.FindObjectOfType<ARSpace>();
|
||||
if (arSpace != null)
|
||||
{
|
||||
Debug.Log("UnityEvo:找到ARSpace,开始加载点云运行环境...");
|
||||
mainPrefab = loadOperation.InstantiateSync(arSpace.transform);
|
||||
} else
|
||||
{
|
||||
Debug.Log("UnityEvo:未找到ARSpace,开始加载普通运行环境,通过语音唤醒...");
|
||||
mainPrefab = loadOperation.InstantiateSync();
|
||||
}
|
||||
#elif NotPointClond
|
||||
mainPrefab = loadOperation.InstantiateSync();
|
||||
#endif
|
||||
if (domainConfig.domain == "Main")
|
||||
AppConfig.SetDefaultMainInstance(mainPrefab);
|
||||
}
|
||||
|
||||
await ZipTool.DownloadAndUnzipAsync(fileId, loadPath, DownLoadProgress, UnzipProgress);
|
||||
}
|
||||
|
||||
private void DownLoadProgress(float progress)
|
||||
public override async UniTask OnExitAsync()
|
||||
{
|
||||
Debug.Log($"下载进度:{progress:P0}");
|
||||
FsmLoadSystem loadSystem = FsmSystem as FsmLoadSystem;
|
||||
loadSystem.ProgressBarPanel.SetProgressBarValue("下载中", progress);
|
||||
}
|
||||
Debug.Log("UnityEvo:Domain退出...");
|
||||
if (HybridClREntrance.Global.stage == StageType.Developer)
|
||||
{
|
||||
if (domainConfig.domain != "Main")
|
||||
{
|
||||
DomainBase domainBase = mainPrefab.GetOrAddComponent<DomainBase>();
|
||||
if (domainBase == null)
|
||||
{
|
||||
Debug.LogError($"UnityEvo:{mainPrefab.name}的DomainBase为空,无法退出,请排查");
|
||||
}
|
||||
else
|
||||
{
|
||||
domainBase.OnExit();
|
||||
await domainBase.OnExitAsync();
|
||||
}
|
||||
|
||||
private void UnzipProgress(float progress)
|
||||
{
|
||||
Debug.Log($"解压进度:{progress:P0}");
|
||||
FsmLoadSystem loadSystem = FsmSystem as FsmLoadSystem;
|
||||
loadSystem.ProgressBarPanel.SetProgressBarValue("解压中", progress);
|
||||
if (domainBase != null)
|
||||
{
|
||||
GameObject.Destroy(domainBase.gameObject);
|
||||
|
||||
await ((FsmLoadSystem)FsmSystem).ForceUnloadAllAssets(domainBase.DomainName);
|
||||
await ((FsmLoadSystem)FsmSystem).ForceUnloadAllAssets(domainBase.DomainNameRaw);
|
||||
}
|
||||
|
||||
if (loadResType == DomainConfig.LoadResType.Scene)
|
||||
{
|
||||
await SceneManager.UnloadSceneAsync(_sceneName);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (HybridClREntrance.Global.stage == StageType.Originality)
|
||||
{
|
||||
await ((FsmLoadSystem)FsmSystem).ForceUnloadAllAssets(AppConfig.PackageDomainName);
|
||||
AppConfig.PackageDomainName = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user