yyl
2026-05-11 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ============================================================================
// 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);
    }
}