// ============================================================================
|
// YooAssetInitTask.cs — YooAsset 初始化启动任务
|
// 在 LaunchInHot 的启动流水线中初始化 YooAsset,与 AssetBundleInitTask 并行/替代
|
// T013: Register YooAssetService as IYooAssetBridge
|
// ============================================================================
|
|
using Cysharp.Threading.Tasks;
|
using ProjSG.Resource;
|
using UnityEngine;
|
using YooAsset;
|
|
public class YooAssetInitTask : LaunchTask
|
{
|
private bool _initStarted = false;
|
private bool _initCompleted = false;
|
|
public override float expectTime
|
{
|
get { return LocalSave.GetFloat("YooAssetInitTask_ExpectTime", 1f); }
|
protected set { LocalSave.SetFloat("YooAssetInitTask_ExpectTime", value); }
|
}
|
|
public override void Begin()
|
{
|
LaunchInHot.m_CurrentStage = LaunchStage.AssetBundleInit;
|
duration = Mathf.Max(0.5f, expectTime);
|
|
if (!_initStarted)
|
{
|
_initStarted = true;
|
RunInitAsync().Forget();
|
}
|
}
|
|
private async UniTaskVoid RunInitAsync()
|
{
|
try
|
{
|
// Determine play mode based on AssetSource setting
|
EPlayMode playMode;
|
YooAsset.IRemoteServices remoteServices = null;
|
if (!AssetSource.isUseAssetBundle)
|
{
|
// Editor 不使用 AB 模式:EditorSimulateMode 直接读 AssetDatabase
|
playMode = EPlayMode.EditorSimulateMode;
|
}
|
else
|
{
|
#if UNITY_WEBGL
|
// WebGL 平台(含 Editor 下切到 WebGL target):BuildInFileSystem 不支持 WebGL,必须用 WebPlayMode
|
playMode = EPlayMode.WebPlayMode;
|
// 远程模式:资源不随包,从 HTTP 服务器加载
|
remoteServices = WebGLRemoteConfig.CreateRemoteServices();
|
#elif UNITY_EDITOR
|
// Editor 非 WebGL target + AB 模式:从本地 StreamingAssets 加载已构建的 AB
|
playMode = EPlayMode.OfflinePlayMode;
|
#else
|
// 非 WebGL 正式包:与 Launch 阶段保持一致
|
// 有 cdnUrl → HostPlayMode(随包 + 远程下载),无 → OfflinePlayMode
|
string cdnUrl = VersionConfigEx.config?.cdnUrl;
|
if (!string.IsNullOrEmpty(cdnUrl))
|
{
|
playMode = EPlayMode.HostPlayMode;
|
remoteServices = new UrlRemoteServices(cdnUrl);
|
}
|
else
|
{
|
playMode = EPlayMode.OfflinePlayMode;
|
}
|
#endif
|
}
|
|
// Initialize YooAssetService
|
await YooAssetService.Instance.InitializeAsync(playMode, remoteServices);
|
|
// Register as IYooAssetBridge for Launch assembly cross-assembly access
|
YooAssetBridgeHolder.Register(YooAssetService.Instance);
|
|
// US4 T042: Register default preload configs and execute StartupEssential preload
|
ResourcePreloader.Instance.RegisterDefaultConfigs();
|
await ResourcePreloader.Instance.PreloadAsync("StartupEssential");
|
|
Debug.Log("[YooAssetInitTask] YooAssetService initialized, bridge registered, StartupEssential preloaded.");
|
_initCompleted = true;
|
}
|
catch (System.Exception ex)
|
{
|
Debug.LogError($"[YooAssetInitTask] Failed: {ex}");
|
_initCompleted = true; // Mark done even on failure to avoid blocking pipeline
|
}
|
}
|
|
public override void Update()
|
{
|
timer += Time.deltaTime;
|
|
if (_initCompleted)
|
{
|
done = true;
|
progress = 1f;
|
}
|
else
|
{
|
progress = Mathf.Clamp01(timer / duration);
|
}
|
|
ExceptionReport();
|
}
|
|
public override void End()
|
{
|
expectTime = timer;
|
Debug.LogFormat("{0}执行时长:{1};", GetType().Name, timer);
|
}
|
}
|