yyl
2026-03-26 1ab047b5fdd933c38ba0519ec2e83a44512ea8d7
Main/ResModule/YooAssetService.cs
@@ -1,4 +1,4 @@
// ============================================================================
// ============================================================================
// YooAssetService.cs — YooAsset 封装服务
// 实现 IYooAssetService 和 IYooAssetBridge,替代 AssetBundleUtility
// ============================================================================
@@ -171,6 +171,11 @@
            }
            Debug.Log($"[YooAssetService] Initialized {_packages.Count}/{allPkgs.Length} packages with PlayMode={playMode}. " +
                $"Active: [{string.Join(", ", _packages.Keys)}]");
#if UNITY_WEBGL
            // WebGL 诊断:输出每个包的 Manifest 和 Bundle 详情
            DiagDumpPackageStatus();
#endif
        }
        /// <summary>
@@ -450,7 +455,7 @@
            var package = FindPackageForAsset(location);
            Debug.LogError($"[YooAssetService] LoadAssetAsync: Loading asset '{location}' of package '{package.PackageName}'.");
            // Debug.LogError($"[YooAssetService] LoadAssetAsync: Loading asset '{location}' of package '{package.PackageName}'.");
            return await ExecuteWithRetryAsync(async () =>
            {
@@ -533,7 +538,17 @@
                    throw new InvalidOperationException($"LoadRawFileTextAsync failed for '{location}': {handle.LastError}");
                }
#if UNITY_ANDROID && !UNITY_EDITOR
                // 真机:StreamingAssets在apk内,需用UnityWebRequest读取
                string filePath = handle.GetRawFilePath();
                using var uwr = UnityEngine.Networking.UnityWebRequest.Get(filePath);
                await uwr.SendWebRequest().ToUniTask(cancellationToken: ct);
                if (uwr.result != UnityEngine.Networking.UnityWebRequest.Result.Success)
                    throw new InvalidOperationException($"LoadRawFileTextAsync UWR failed for '{location}': {uwr.error}");
                return uwr.downloadHandler.text;
#else
                return handle.GetRawFileText();
#endif
            }, $"LoadRawFileTextAsync('{location}')", ct);
        }
@@ -553,7 +568,17 @@
                    throw new InvalidOperationException($"LoadRawFileBytesAsync failed for '{location}': {handle.LastError}");
                }
#if UNITY_ANDROID && !UNITY_EDITOR
                // 真机:StreamingAssets在apk内,需用UnityWebRequest读取
                string filePath = handle.GetRawFilePath();
                using var uwr = UnityEngine.Networking.UnityWebRequest.Get(filePath);
                await uwr.SendWebRequest().ToUniTask(cancellationToken: ct);
                if (uwr.result != UnityEngine.Networking.UnityWebRequest.Result.Success)
                    throw new InvalidOperationException($"LoadRawFileBytesAsync UWR failed for '{location}': {uwr.error}");
                return uwr.downloadHandler.data;
#else
                return handle.GetRawFileData();
#endif
            }, $"LoadRawFileBytesAsync('{location}')", ct);
        }
@@ -788,5 +813,35 @@
            var package = FindPackageForAsset(location);
            return package.LoadAllAssetsSync<T>(location);
        }
#if UNITY_WEBGL
        /// <summary>
        /// WebGL 诊断:输出每个 YooAsset 包的初始化状态和资源数量。
        /// </summary>
        private void DiagDumpPackageStatus()
        {
            var sb = new System.Text.StringBuilder();
            sb.AppendLine("[YooAssetService][WebGL-Diag] Package status dump:");
            foreach (var kv in _packages)
            {
                var pkg = kv.Value;
                var status = pkg.InitializeStatus;
                // 尝试获取清单中的资源信息数量
                int assetCount = 0;
                try
                {
                    // GetAssetInfos 返回该包所有资源信息
                    var infos = pkg.GetAssetInfos(string.Empty);
                    assetCount = infos != null ? infos.Length : -1;
                }
                catch
                {
                    assetCount = -1;
                }
                sb.AppendLine($"  [{kv.Key}] status={status}, assetInfoCount={assetCount}");
            }
            Debug.Log(sb.ToString());
        }
#endif
    }
}