From 97de31e9a015cf139f5293a22e1575a43dfb6733 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期五, 24 四月 2026 14:16:02 +0800
Subject: [PATCH] WEBGL ANDROID适配

---
 Main/ResModule/ResManager.cs |  416 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 235 insertions(+), 181 deletions(-)

diff --git a/Main/ResModule/ResManager.cs b/Main/ResModule/ResManager.cs
index 47471ba..88f84ee 100644
--- a/Main/ResModule/ResManager.cs
+++ b/Main/ResModule/ResManager.cs
@@ -1,4 +1,4 @@
-锘縰sing UnityEngine;
+using UnityEngine;
 using System.Collections.Generic;
 using System;
 using UnityEngine.U2D;
@@ -10,10 +10,6 @@
 using System.Threading;
 using ProjSG.Resource;
 using YooAsset;
-
-
-
-
 
 
 #if UNITY_EDITOR
@@ -131,142 +127,130 @@
     }
 #endif
 
-    //  // needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�
-    // [System.Obsolete("US2: Use LoadAssetAsync<T>(directory, name, needExt) returning UniTask<T> instead.")]
-    // public T LoadAsset<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object
-    // {
-    //     directory = directory.Replace("\\", "/");
-    //     name = name.Replace("\\", "/");
-    //     //  鐗规畩澶勭悊 鍥犱负鏈変竴灞傚浘闆嗙殑鍏崇郴 directory瑕佷紶鍏ョ殑搴旇鏄痑tlas鐨勫悕瀛�
-    //     if (typeof(T) == typeof(Sprite))
-    //     {
-    //         return LoadSprite(directory, name) as T;
-    //     }
-    //     else if (typeof(T) == typeof(SkeletonDataAsset))
-    //     {
-    //         //鏂囦欢鐩綍璋冩暣锛宯ame涓寘鍚簡璺緞
-    //         if (name.Contains("/"))
-    //         {
-    //             directory += name.Substring(0, name.LastIndexOf("/"));
-    //             name = name.Substring(name.LastIndexOf("/") + 1);
-    //         }
+    // ====================================================================
+    // 鍚屾鏂规硶锛堜粎闈� WebGL 骞冲彴锛�
+    // ====================================================================
+#if !UNITY_WEBGL
+    //needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�
+    public T LoadAsset<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object
+    {
+        directory = directory.Replace("\\", "/");
+        name = name.Replace("\\", "/");
+        //  鐗规畩澶勭悊 鍥犱负鏈変竴灞傚浘闆嗙殑鍏崇郴 directory瑕佷紶鍏ョ殑搴旇鏄痑tlas鐨勫悕瀛�
+        if (typeof(T) == typeof(Sprite))
+        {
+            return LoadSprite(directory, name) as T;
+        }
+        else if (typeof(T) == typeof(SkeletonDataAsset))
+        {
+            //鏂囦欢鐩綍璋冩暣锛宯ame涓寘鍚簡璺緞
+            if (name.Contains("/"))
+            {
+                directory += name.Substring(0, name.LastIndexOf("/"));
+                name = name.Substring(name.LastIndexOf("/") + 1);
+            }
+        }
 
-    //     }
+        return LoadAssetInternal<T>(directory, name, needExt);
+    }
 
-    //     return LoadAssetInternal<T>(directory, name, needExt);
-    // }
+    //needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�
+    private T LoadAssetInternal<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object
+    {
+        T asset = null;
+        var path = ($"Assets/ResourcesOut/{directory}/{name}" + (needExt ? GetExtension(typeof(T)) : "")).Replace("//", "/").Trim().Replace("\\", "/");
 
-    // //needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�
-    // private T LoadAssetInternal<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object
-    // {
-    //     // 宸茬鐢ㄥ悓姝ュ姞杞斤紝寮哄埗涓氬姟鍏ㄩ儴璧板紓姝PI銆�
-    //     throw new NotSupportedException("鍚屾璧勬簮鍔犺浇宸茬鐢紝璇蜂娇鐢ㄥ紓姝ユ帴鍙�");
-    // }
+        if (!AssetSource.isUseAssetBundle)
+        {
+#if UNITY_EDITOR
+            asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
+#endif
+        }
+        else
+        {
+            asset = YooAssetService.Instance.LoadAssetSync<T>(path);
+        }
 
-//     [System.Obsolete("US2: Use LoadConfigAsync returning UniTask<string[]> instead.")]
-//     public string[] LoadConfig(string name)
-//     {
-//         string path = string.Empty;
-// #if UNITY_EDITOR
-//         if (!AssetSource.isUseAssetBundle)
-//         {
-//             path = ResourcesPath.CONFIG_FODLER + "/" + name + ".txt";
-//         }
-//         else
-// #endif
-//         {
-//             path = AssetVersionUtility.GetAssetFilePath($"config/{name}.txt");
-//         }
+        if (asset == null)
+        {
+            Debug.LogErrorFormat("LoadAsset() => 鍔犺浇涓嶅埌璧勬簮: {0}", path);
+        }
 
-//         return File.ReadAllLines(path);
-//     }
+        return asset;
+    }
 
+    public string[] LoadConfig(string name)
+    {
+#if UNITY_EDITOR
+        if (!AssetSource.isUseAssetBundle)
+        {
+            string path = ResourcesPath.CONFIG_FODLER + "/" + name + ".txt";
+            return File.ReadAllLines(path);
+        }
+#endif
+        // AB 妯″紡锛氶�氳繃 YooAsset 鍚屾鍔犺浇 TextAsset
+        var location = $"Assets/ResourcesOut/Config/{name}.txt";
+        var textAsset = YooAssetService.Instance.LoadAssetSync<TextAsset>(location);
+        if (textAsset != null && !string.IsNullOrEmpty(textAsset.text))
+            return textAsset.text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
+        Debug.LogError($"[ResManager] LoadConfig failed for '{name}'");
+        return Array.Empty<string>();
+    }
+
+    private Sprite LoadSprite(string atlasName, string spriteName)
+    {
+        if (!AssetSource.isUseAssetBundle)
+        {
+            SpriteAtlas atlas = LoadAsset<SpriteAtlas>("Sprite", atlasName.Replace("Sprite/", ""));
+            if (null == atlas)
+            {
+                return null;
+            }
+            return atlas.GetSprite(spriteName);
+        }
+        else
+        {
+            // YooAsset 浣跨敤瀹屾暣璺緞鐩存帴鍔犺浇 Sprite
+            var path = $"Assets/ResourcesOut/{atlasName}/{spriteName}.png".Replace("//", "/");
+            return YooAssetService.Instance.LoadAssetSync<Sprite>(path);
+        }
+    }
+#endif
+
+    // ====================================================================
+    // 寮傛鍔犺浇鏂规硶锛堟墍鏈夊钩鍙扮粺涓�锛�
+    // ====================================================================
+
+    //needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�锛堝洖璋冪増鏈級
+    public void LoadAssetAsync<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack, bool needExt = true) where T : UnityEngine.Object
+    {
+        directory = directory.Replace("\\", "/");
+        name = name.Replace("\\", "/");
+
+        //  鐗规畩澶勭悊 鍥犱负鏈変竴灞傚浘闆嗙殑鍏崇郴 directory瑕佷紶鍏ョ殑搴旇鏄痑tlas鐨勫悕瀛�
+        if (typeof(T) == typeof(Sprite))
+        {
+            LoadSpriteAsync<T>(directory, name, callBack);
+            return;
+        }
+        else if (typeof(T) == typeof(SkeletonDataAsset))
+        {
+            //鏂囦欢鐩綍璋冩暣锛宯ame涓寘鍚簡璺緞
+            if (name.Contains("/"))
+            {
+                directory += name.Substring(0, name.LastIndexOf("/"));
+                name = name.Substring(name.LastIndexOf("/") + 1);
+            }
+        }
+
+        LoadAssetAsyncInternal<T>(directory, name, callBack, needExt);
+    }
 
     public async UniTask<T> LoadAssetAsync<T>(string directory, string name) where T : UnityEngine.Object
     {
         return await LoadAssetAsync<T>(directory, name, needExt: true);
     }
 
-    public async UniTask<T> LoadAssetAsync<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object
-    {
-        return await LoadAssetAsync<T>(directory, name, needExt, CancellationToken.None);
-    }
-
-    private void LoadSpriteAsync<T>(string atlasName, string spriteName, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
-    {
-        if (!AssetSource.isUseAssetBundle)
-        {
-            // Editor 妯″紡涓嬪彲鐩存帴鍔犺浇 sprite
-            LoadAssetAsyncInternal<T>(atlasName, spriteName, callBack);
-        }
-        else
-        {
-            // AB 妯″紡涓嬬洿鎺ュ姞杞藉崟鐙殑 Sprite 鏂囦欢锛圷ooAsset 鑷姩澶勭悊 SpriteAtlas 渚濊禆锛�
-            LoadAssetAsyncInternal<Sprite>(atlasName, spriteName, (isLoaded, sprite) =>
-            {
-                callBack?.Invoke(isLoaded, sprite);
-            });
-        }
-    }
-
-    private void LoadAssetAsyncInternal<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack, bool needExt = true) where T : UnityEngine.Object
-    {
-        var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", (needExt ? GetExtension(typeof(T)) : "")).Replace("//", "/");
-
-        if (!AssetSource.isUseAssetBundle)
-        {
-#if UNITY_EDITOR
-            var asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
-            callBack?.Invoke(asset != null, asset);
-#endif
-        }
-        else
-        {
-            // US1: Route through YooAssetService async
-            CoLoadViaYooAsset<T>(path, callBack).Forget();
-        }
-    }
-
-    private async UniTaskVoid CoLoadViaYooAsset<T>(string path, Action<bool, UnityEngine.Object> callBack, CancellationToken ct = default) where T : UnityEngine.Object
-    {
-        try
-        {
-            var asset = await YooAssetService.Instance.LoadAssetAsync<T>(path, ct: ct);
-            callBack?.Invoke(asset != null, asset);
-        }
-        catch (Exception ex)
-        {
-            Debug.LogError($"[ResManager] Async load via YooAsset failed: {ex.Message}");
-            callBack?.Invoke(false, null);
-        }
-    }
-
-    public void UnloadAsset(string directory, string assetName)
-    {
-        directory = directory.Replace("\\", "/").TrimEnd('/');
-        assetName = assetName.Replace("\\", "/");
-        string path = ($"Assets/ResourcesOut/{directory}/{assetName}").Replace("//", "/");
-        YooAssetService.Instance.UnloadAsset(path);
-    }
-
-    public string GetAssetFilePath(string _assetKey)
-    {
-        var path = Path.Combine(ExternalStorePath, _assetKey);
-        if (!File.Exists(path))
-        {
-            path = Path.Combine(StreamingAssetPath, _assetKey);
-        }
-
-        return path;
-    }
-
-    // ====================================================================
-    // US1: New UniTask-based async variants
-    // ====================================================================
-
-    /// <summary>
-    /// 寮傛鍔犺浇璧勬簮锛圲niTask 鐗堟湰锛孶S1 鏂板锛夈��
-    /// </summary>
     public async UniTask<T> LoadAssetAsync<T>(string directory, string name, bool needExt = true, CancellationToken ct = default) where T : UnityEngine.Object
     {
         directory = directory.Replace("\\", "/");
@@ -307,8 +291,126 @@
         return await YooAssetService.Instance.LoadAssetAsync<T>(path, ct: ct);
     }
 
+    // ====================================================================
+    // 寮傛鍐呴儴瀹炵幇锛堢粺涓�锛�
+    // ====================================================================
+
+    private void LoadSpriteAsync<T>(string atlasName, string spriteName, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
+    {
+        if (!AssetSource.isUseAssetBundle)
+        {
+            // Editor 妯″紡涓嬪彲鐩存帴鍔犺浇 sprite
+            LoadAssetAsyncInternal<T>(atlasName, spriteName, callBack);
+        }
+        else
+        {
+            // AB 妯″紡涓嬬洿鎺ュ姞杞藉崟鐙殑 Sprite 鏂囦欢锛圷ooAsset 鑷姩澶勭悊 SpriteAtlas 渚濊禆锛�
+            LoadAssetAsyncInternal<Sprite>(atlasName, spriteName, (isLoaded, sprite) =>
+            {
+                callBack?.Invoke(isLoaded, sprite);
+            });
+        }
+    }
+
+    private void LoadAssetAsyncInternal<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack, bool needExt = true) where T : UnityEngine.Object
+    {
+        var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", (needExt ? GetExtension(typeof(T)) : "")).Replace("//", "/");
+
+        if (!AssetSource.isUseAssetBundle)
+        {
+#if UNITY_EDITOR
+            var asset = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
+            callBack?.Invoke(asset != null, asset);
+#endif
+        }
+        else
+        {
+            CoLoadViaYooAsset<T>(path, callBack).Forget();
+        }
+    }
+
+    private async UniTaskVoid CoLoadViaYooAsset<T>(string path, Action<bool, UnityEngine.Object> callBack, CancellationToken ct = default) where T : UnityEngine.Object
+    {
+        try
+        {
+            var asset = await YooAssetService.Instance.LoadAssetAsync<T>(path, ct: ct);
+            callBack?.Invoke(asset != null, asset);
+        }
+        catch (Exception ex)
+        {
+            Debug.LogError($"[ResManager] Async load via YooAsset failed: {ex.Message}");
+            callBack?.Invoke(false, null);
+        }
+    }
+
+    public void UnloadAsset(string directory, string assetName)
+    {
+        if (!AssetSource.isUseAssetBundle)
+            return;
+
+        directory = directory.Replace("\\", "/").TrimEnd('/');
+        assetName = assetName.Replace("\\", "/");
+        string path = ($"Assets/ResourcesOut/{directory}/{assetName}").Replace("//", "/");
+        YooAssetService.Instance.UnloadAsset(path);
+    }
+
+    public string GetAssetFilePath(string _assetKey)
+    {
+        var path = Path.Combine(ExternalStorePath, _assetKey);
+        if (!File.Exists(path))
+        {
+            path = Path.Combine(StreamingAssetPath, _assetKey);
+        }
+
+        return path;
+    }
+
+    // ====================================================================
+    // LoadConfigAsync锛堟墍鏈夊钩鍙扮粺涓�锛�
+    // ====================================================================
+
     /// <summary>
-    /// US4: 寮傛鍔犺浇璧勬簮骞惰蛋缂撳瓨灞傦紙缂撳瓨鍛戒腑鐩存帴杩斿洖锛屾湭鍛戒腑鍒欏姞杞藉苟缂撳瓨锛夈��
+    /// 寮傛鍔犺浇閰嶇疆鏂囦欢銆�
+    /// AB 妯″紡浣跨敤 YooAsset 寮傛鍔犺浇 TextAsset锛岄潪 AB 妯″紡鐩存帴璇绘枃浠躲��
+    /// </summary>
+    public async UniTask<string[]> LoadConfigAsync(string name, bool needExt = true, CancellationToken ct = default)
+    {
+        if (AssetSource.isUseAssetBundle)
+        {
+            if (name.EndsWith(".txt") && needExt)
+            {
+                name = name.Substring(0, name.Length - 4);
+            }
+            var location = $"Assets/ResourcesOut/Config/{name}" + (needExt ? ".txt" : "");
+            try
+            {
+                var asset = await YooAssetService.Instance.LoadAssetAsync(
+                    location, typeof(TextAsset), 0, ct) as TextAsset;
+                if (asset != null && !string.IsNullOrEmpty(asset.text))
+                    return asset.text.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
+            }
+            catch (Exception ex)
+            {
+                Debug.LogError($"[ResManager] LoadConfigAsync YooAsset failed for '{name}': {ex.Message}");
+            }
+            return Array.Empty<string>();
+        }
+
+        // 闈� AB 妯″紡: 鐩存帴璇绘枃浠讹紙Editor 寮�鍙戞ā寮忥級
+#if UNITY_EDITOR
+        string path = ResourcesPath.CONFIG_FODLER + "/" + name + (needExt ? ".txt" : "");
+        return await UniTask.RunOnThreadPool(() => File.ReadAllLines(path));
+#else
+        return Array.Empty<string>();
+#endif
+    }
+
+    // ====================================================================
+    // 缂撳瓨鍔犺浇 & Sprite 寮傛鍔犺浇锛堟墍鏈夊钩鍙帮級
+    // ====================================================================
+
+    /// <summary>
+    /// 寮傛鍔犺浇璧勬簮骞惰蛋缂撳瓨灞傦紙缂撳瓨鍛戒腑鐩存帴杩斿洖锛屾湭鍛戒腑鍒欏姞杞藉苟缂撳瓨锛夈��
     /// </summary>
     public async UniTask<T> LoadAssetCachedAsync<T>(string directory, string name, bool needExt = true, CancellationToken ct = default) where T : UnityEngine.Object
     {
@@ -317,7 +419,7 @@
 
         var path = ($"Assets/ResourcesOut/{directory}/{name}" + (needExt ? GetExtension(typeof(T)) : ""))
             .Replace("//", "/").Trim().Replace("\\", "/");
-        // 闊抽鏂囦欢鎵╁睍鍚嶇粺涓�杞皬鍐欙紝閬垮厤 .WAV/.MP3 澶у皬鍐欎笉鍖归厤
+        // 闊抽鏂囦欢鎵╁睍鍚嶇粺涓�杞皬鍐�
         if (typeof(T) == typeof(AudioClip))
         {
             var pathExt2 = System.IO.Path.GetExtension(path);
@@ -348,58 +450,10 @@
             var path = $"Assets/ResourcesOut/{atlasName}/{spriteName}.png"
                 .Replace("//", "/").Trim().Replace("\\", "/");
             var sprite = await YooAssetService.Instance.LoadAssetAsync<Sprite>(path, ct: ct);
-#if UNITY_WEBGL
             if (sprite == null)
-                Debug.LogWarning($"[ResManager][WebGL-Diag] Sprite load returned NULL: path={path}");
-#endif
+                Debug.LogWarning($"[ResManager] Sprite load returned NULL: path={path}");
             return sprite;
         }
     }
 
-    /// <summary>
-    /// 寮傛鍔犺浇閰嶇疆鏂囦欢锛圲niTask 鐗堟湰锛夈��
-    /// AB 妯″紡浣跨敤 YooAsset RawFile 寮傛鍔犺浇锛岄潪 AB 妯″紡鐩存帴璇绘枃浠躲��
-    /// </summary>
-    public async UniTask<string[]> LoadConfigAsync(string name, bool needExt = true, CancellationToken ct = default)
-    {
-        // AB 妯″紡锛堝惈 WebGL锛�: 浣跨敤 YooAsset 鍔犺浇閰嶇疆鏂囦欢
-        if (AssetSource.isUseAssetBundle)
-        {
-            // 鍒ゆ柇涓�涓嬫槸鍚﹀師鏉ュ凡缁忓寘鍚�.txt 閰嶅悎 needExt 鍋氬吋瀹癸紝閬垮厤璋冪敤鏂逛紶鍏ラ噸澶嶅悗缂�瀵艰嚧璺緞閿欒
-            if (name.EndsWith(".txt") && needExt)
-            {
-                name = name.Substring(0, name.Length - 4);
-            }
-            var location = $"Assets/ResourcesOut/Config/{name}" + (needExt ? ".txt" : "");
-            try
-            {
-#if UNITY_WEBGL
-                // WebGL: WebServerFileSystem 涓嶆敮鎸� LoadRawFileAsync锛屾敼鐢� LoadAssetAsync<TextAsset>
-                // .txt 鏂囦欢鍦� Unity 涓互 TextAsset 褰㈠紡瀵煎叆锛學ebGL 鏀寔姝ゅ姞杞芥柟寮�
-                var asset = await ProjSG.Resource.YooAssetService.Instance.LoadAssetAsync(
-                    location, typeof(UnityEngine.TextAsset), 0, ct) as UnityEngine.TextAsset;
-                if (asset != null && !string.IsNullOrEmpty(asset.text))
-                    return asset.text.Split(new[] { "\r\n", "\n" }, System.StringSplitOptions.None);
-#else
-                var textAsset = await ProjSG.Resource.YooAssetService.Instance.LoadAssetAsync<TextAsset>(location, 0, ct);
-                if (textAsset != null && !string.IsNullOrEmpty(textAsset.text))
-                    return textAsset.text.Split(new[] { "\r\n", "\n" }, System.StringSplitOptions.None);
-#endif
-            }
-            catch (System.Exception ex)
-            {
-                UnityEngine.Debug.LogError($"[ResManager] LoadConfigAsync YooAsset failed for '{name}': {ex.Message}");
-            }
-            return System.Array.Empty<string>();
-        }
-
-        // 闈� AB 妯″紡: 鐩存帴璇绘枃浠讹紙Editor 寮�鍙戞ā寮忥級
-#if UNITY_EDITOR
-        string path = ResourcesPath.CONFIG_FODLER + "/" + name + (needExt ? ".txt" : "");
-        return await UniTask.RunOnThreadPool(() => File.ReadAllLines(path));
-#else
-        return System.Array.Empty<string>();
-#endif
-    }
-
-}
\ No newline at end of file
+}

--
Gitblit v1.8.0