From 1ad03cc2f91d75e80fc3dc42e2ac1fadc9a2bfec Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期三, 11 二月 2026 16:10:43 +0800
Subject: [PATCH] Merge branch 'master' into h5version
---
Main/ResModule/ResManager.cs | 244 ++++++++++++++++++++++++++++++++++++++----------
1 files changed, 193 insertions(+), 51 deletions(-)
diff --git a/Main/ResModule/ResManager.cs b/Main/ResModule/ResManager.cs
index ddedebe..bdf5de0 100644
--- a/Main/ResModule/ResManager.cs
+++ b/Main/ResModule/ResManager.cs
@@ -5,6 +5,12 @@
using System.IO;
using UnityEngine.Video;
using Spine.Unity;
+using UnityEngine.UI;
+using Cysharp.Threading.Tasks;
+using System.Threading;
+using ProjSG.Resource;
+
+
@@ -67,7 +73,7 @@
{typeof(Sprite), "png"},
{typeof(Texture2D), "jpg"},
{typeof(Texture), "jpg"},
- { typeof(Shader), "shader"},
+ {typeof(Shader), "shader"},
{typeof(TextAsset), "txt"},
{typeof(AudioClip), "wav"},
{typeof(Font), "ttf"},
@@ -123,23 +129,37 @@
}
#endif
- public T LoadAsset<T> (string directory, string name) where T : UnityEngine.Object
+ //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
{
- T asset = null;
+ 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);
+ }
+
+ return LoadAssetInternal<T>(directory, name, needExt);
}
- private T LoadAssetInternal<T>(string directory, string name) where T : UnityEngine.Object
+ //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("\\", "/");
- var path = ($"Assets/ResourcesOut/{directory}/{name}" + GetExtension(typeof(T))).Replace("//", "/").Trim().Replace("\\", "/");
if (!AssetSource.isUseAssetBundle)
{
#if UNITY_EDITOR
@@ -148,16 +168,10 @@
}
else
{
- if (directory == "UI" || directory == "UIComp" || directory.StartsWith("Sprite") || directory == "Battle/Prefabs")
- {
- directory = "UI/" + directory;
- }
- else if (name == "Hero_001")
- {
- directory = "UI/Hero/SpineRes";
- }
- var assetInfo = new AssetInfo(directory.ToLower(), name.ToLower());
- asset = AssetBundleUtility.Instance.Sync_LoadAsset(assetInfo, typeof(T)) as T;
+ // US1: Route through YooAssetService sync wrapper (transitional)
+ #pragma warning disable CS0612, CS0618
+ asset = YooAssetService.Instance.LoadAssetSync<T>(path);
+ #pragma warning restore CS0612, CS0618
}
if (asset == null)
@@ -168,6 +182,7 @@
return asset;
}
+ [System.Obsolete("US2: Use LoadConfigAsync returning UniTask<string[]> instead.")]
public string[] LoadConfig(string name)
{
string path = string.Empty;
@@ -187,16 +202,28 @@
private Sprite LoadSprite(string atlasName, string spriteName)
{
-#if UNITY_EDITOR
- SpriteAtlas atlas = LoadAsset<SpriteAtlas>("Sprite", atlasName.Replace("Sprite/", ""));
- return atlas.GetSprite(spriteName);
-#else
- return ResManager.Instance.LoadAssetInternal<Sprite>(atlasName, spriteName);
-#endif
+ if (!AssetSource.isUseAssetBundle)
+ {
+ #pragma warning disable CS0618 // Obsolete 鈥� legacy sync fallback
+ SpriteAtlas atlas = LoadAsset<SpriteAtlas>("Sprite", atlasName.Replace("Sprite/", ""));
+ #pragma warning restore CS0618
+ if (null == atlas)
+ {
+ return null;
+ }
+ return atlas.GetSprite(spriteName);
+ }
+ else
+ return LoadAssetInternal<Sprite>(atlasName, spriteName);
}
- public void LoadAssetAsync<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
+ //needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�
+ [System.Obsolete("US2: Use LoadAssetAsync<T>(directory, name, needExt) returning UniTask<T> instead.")]
+ 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))
{
@@ -204,32 +231,29 @@
return;
}
- LoadAssetAsyncInternal<T>(directory, name, callBack);
+ LoadAssetAsyncInternal<T>(directory, name, callBack, needExt);
}
private void LoadSpriteAsync<T>(string atlasName, string spriteName, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
{
-#if !UNITY_EDITOR
- LoadAssetAsync<SpriteAtlas>(atlasName, spriteName, (isLoaded, atlas) => {
- if (isLoaded)
+ if (!AssetSource.isUseAssetBundle)
+ {
+ // Editor 妯″紡涓嬪彲鐩存帴鍔犺浇 sprite
+ LoadAssetAsyncInternal<T>(atlasName, spriteName, callBack);
+ }
+ else
+ {
+ // AB 妯″紡涓嬬洿鎺ュ姞杞藉崟鐙殑 Sprite 鏂囦欢锛圷ooAsset 鑷姩澶勭悊 SpriteAtlas 渚濊禆锛�
+ LoadAssetAsyncInternal<Sprite>(atlasName, spriteName, (isLoaded, sprite) =>
{
- SpriteAtlas _atlas = atlas as SpriteAtlas;
- callBack?.Invoke(isLoaded, _atlas.GetSprite(spriteName));
- }
- else
- {
- callBack?.Invoke(false, null);
- }
- });
-#else
- // 缂栬緫鍣ㄤ笅鍙互鐩存帴鍔犺浇娌″暐闂
- LoadAssetAsyncInternal<T>(atlasName, spriteName, callBack);
-#endif
+ callBack?.Invoke(isLoaded, sprite);
+ });
+ }
}
- private void LoadAssetAsyncInternal<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
+ 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}", GetExtension(typeof(T))).Replace("//", "/");
+ var path = string.Concat($"Assets/ResourcesOut/{directory}/{name}", (needExt ? GetExtension(typeof(T)) : "")).Replace("//", "/");
if (!AssetSource.isUseAssetBundle)
{
@@ -240,24 +264,36 @@
}
else
{
- var assetInfo = new AssetInfo(directory.ToLower(), name.ToLower());
- AssetBundleUtility.Instance.Co_LoadAsset(assetInfo, callBack);
+ // US1: Route through YooAssetService async
+ CoLoadViaYooAsset<T>(path, callBack).Forget();
}
}
- public void UnloadAsset(string assetBundleName, string assetName)
+ private async UniTaskVoid CoLoadViaYooAsset<T>(string path, Action<bool, UnityEngine.Object> callBack, CancellationToken ct = default) where T : UnityEngine.Object
{
- if (!AssetSource.isUseAssetBundle)
- return;
-
- AssetBundleUtility.Instance.UnloadAsset(assetBundleName, assetName);
+ 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);
+ }
}
+ [System.Obsolete("US1: Use YooAssetService.ReleaseHandle or UnloadUnusedAssetsAsync instead.")]
+ public void UnloadAsset(string assetBundleName, string assetName)
+ {
+ // US1: AssetBundleUtility unload no longer effective since assets loaded via YooAsset.
+ // Proper unload handled via YooAssetService handle-based release.
+ }
+
+ [System.Obsolete("US1: Use YooAssetService.UnloadUnusedAssetsAsync instead.")]
public void UnloadAssetBundle(string assetBundleName, bool unloadAllLoadedObjects, bool includeDependenice)
{
- if (!AssetSource.isUseAssetBundle)
- return;
- AssetBundleUtility.Instance.UnloadAssetBundle(assetBundleName, unloadAllLoadedObjects, includeDependenice);
+ // US1: AssetBundleUtility unload no longer effective since assets loaded via YooAsset.
}
public string GetAssetFilePath(string _assetKey)
@@ -271,5 +307,111 @@
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("\\", "/");
+ name = name.Replace("\\", "/");
+
+ if (typeof(T) == typeof(Sprite))
+ {
+ return await LoadSpriteAsyncUniTask(directory, name, ct) as T;
+ }
+
+ if (typeof(T) == typeof(SkeletonDataAsset))
+ {
+ if (name.Contains("/"))
+ {
+ directory += name.Substring(0, name.LastIndexOf("/"));
+ name = name.Substring(name.LastIndexOf("/") + 1);
+ }
+ }
+
+ var path = ($"Assets/ResourcesOut/{directory}/{name}" + (needExt ? GetExtension(typeof(T)) : ""))
+ .Replace("//", "/").Trim().Replace("\\", "/");
+
+ if (!AssetSource.isUseAssetBundle)
+ {
+#if UNITY_EDITOR
+ return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
+#else
+ return null;
+#endif
+ }
+
+ return await YooAssetService.Instance.LoadAssetAsync<T>(path, ct: ct);
+ }
+
+ /// <summary>
+ /// US4: 寮傛鍔犺浇璧勬簮骞惰蛋缂撳瓨灞傦紙缂撳瓨鍛戒腑鐩存帴杩斿洖锛屾湭鍛戒腑鍒欏姞杞藉苟缂撳瓨锛夈��
+ /// </summary>
+ public async UniTask<T> LoadAssetCachedAsync<T>(string directory, string name, bool needExt = true, CancellationToken ct = default) where T : UnityEngine.Object
+ {
+ directory = directory.Replace("\\", "/");
+ name = name.Replace("\\", "/");
+
+ var path = ($"Assets/ResourcesOut/{directory}/{name}" + (needExt ? GetExtension(typeof(T)) : ""))
+ .Replace("//", "/").Trim().Replace("\\", "/");
+
+ if (!AssetSource.isUseAssetBundle)
+ {
+#if UNITY_EDITOR
+ return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
+#else
+ return null;
+#endif
+ }
+
+ return await ResourceCacheManager.Instance.GetOrLoadAsync<T>(path);
+ }
+
+ private async UniTask<Sprite> LoadSpriteAsyncUniTask(string atlasName, string spriteName, CancellationToken ct = default)
+ {
+ if (!AssetSource.isUseAssetBundle)
+ {
+ var atlas = await LoadAssetAsync<SpriteAtlas>("Sprite", atlasName.Replace("Sprite/", ""), ct: ct);
+ return atlas?.GetSprite(spriteName);
+ }
+ else
+ {
+ var path = $"Assets/ResourcesOut/{atlasName}/{spriteName}.png"
+ .Replace("//", "/").Trim().Replace("\\", "/");
+ return await YooAssetService.Instance.LoadAssetAsync<Sprite>(path, ct: ct);
+ }
+ }
+
+ /// <summary>
+ /// 寮傛鍔犺浇閰嶇疆鏂囦欢锛圲niTask 鐗堟湰锛夈��
+ /// WebGL 骞冲彴浣跨敤 YooAsset RawFile 寮傛鍔犺浇锛屽叾浠栧钩鍙颁娇鐢ㄧ嚎绋嬫睜銆�
+ /// </summary>
+ public async UniTask<string[]> LoadConfigAsync(string name, CancellationToken ct = default)
+ {
+#if UNITY_WEBGL && !UNITY_EDITOR
+ // WebGL 涓嶆敮鎸佸绾跨▼鍜� File.ReadAllLines锛屼娇鐢� YooAsset RawFile
+ try
+ {
+ var text = await ProjSG.Resource.YooAssetService.Instance.LoadRawFileTextAsync($"config/{name}", ct);
+ if (!string.IsNullOrEmpty(text))
+ {
+ return text.Split(new[] { "\r\n", "\n" }, System.StringSplitOptions.None);
+ }
+ }
+ catch (System.Exception ex)
+ {
+ UnityEngine.Debug.LogError($"[ResManager] LoadConfigAsync WebGL fallback failed for '{name}': {ex.Message}");
+ }
+ return System.Array.Empty<string>();
+#else
+ #pragma warning disable CS0618 // LoadConfig is obsolete 鈥� used here as thread-pool fallback for non-WebGL
+ return await UniTask.RunOnThreadPool(() => LoadConfig(name));
+ #pragma warning restore CS0618
+#endif
+ }
}
\ No newline at end of file
--
Gitblit v1.8.0