From 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期一, 11 五月 2026 16:20:37 +0800
Subject: [PATCH] Merge branch 'master' into h5version
---
Main/ResModule/ResManager.cs | 327 +++++++++++++++++++++++++++++++++---------------------
1 files changed, 201 insertions(+), 126 deletions(-)
diff --git a/Main/ResModule/ResManager.cs b/Main/ResModule/ResManager.cs
index 8ec4613..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,69 +127,173 @@
}
#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
+ public async UniTask<T> LoadAssetAsync<T>(string directory, string name, bool needExt = true, CancellationToken ct = default) where T : UnityEngine.Object
{
- // 鏂扮増鐩存帴璧� YooAsset/寮傛鍔犺浇
- // 杩欓噷鍋囪鏈� YooAssetService.Instance.LoadAssetAsync<T>(...) 鍙敤
- string path = directory.EndsWith("/") ? directory + name : directory + "/" + name;
- return await YooAssetService.Instance.LoadAssetAsync<T>(path);
+ 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("\\", "/");
+ // 闊抽鏂囦欢鎵╁睍鍚嶇粺涓�杞皬鍐欙紝閬垮厤 .WAV/.MP3 澶у皬鍐欎笉鍖归厤
+ if (typeof(T) == typeof(AudioClip))
+ {
+ var pathExt = System.IO.Path.GetExtension(path);
+ if (!string.IsNullOrEmpty(pathExt) && pathExt != pathExt.ToLower())
+ path = path.Substring(0, path.Length - pathExt.Length) + pathExt.ToLower();
+ }
+ 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);
}
+
+ // ====================================================================
+ // 寮傛鍐呴儴瀹炵幇锛堢粺涓�锛�
+ // ====================================================================
private void LoadSpriteAsync<T>(string atlasName, string spriteName, Action<bool, UnityEngine.Object> callBack) where T : UnityEngine.Object
{
@@ -225,7 +325,6 @@
}
else
{
- // US1: Route through YooAssetService async
CoLoadViaYooAsset<T>(path, callBack).Forget();
}
}
@@ -246,7 +345,12 @@
public void UnloadAsset(string directory, string assetName)
{
- string path = directory.EndsWith("/") ? directory + assetName : directory + "/" + assetName;
+ if (!AssetSource.isUseAssetBundle)
+ return;
+
+ directory = directory.Replace("\\", "/").TrimEnd('/');
+ assetName = assetName.Replace("\\", "/");
+ string path = ($"Assets/ResourcesOut/{directory}/{assetName}").Replace("//", "/");
YooAssetService.Instance.UnloadAsset(path);
}
@@ -262,48 +366,51 @@
}
// ====================================================================
- // US1: New UniTask-based async variants
+ // LoadConfigAsync锛堟墍鏈夊钩鍙扮粺涓�锛�
// ====================================================================
/// <summary>
- /// 寮傛鍔犺浇璧勬簮锛圲niTask 鐗堟湰锛孶S1 鏂板锛夈��
+ /// 寮傛鍔犺浇閰嶇疆鏂囦欢銆�
+ /// AB 妯″紡浣跨敤 YooAsset 寮傛鍔犺浇 TextAsset锛岄潪 AB 妯″紡鐩存帴璇绘枃浠躲��
/// </summary>
- public async UniTask<T> LoadAssetAsync<T>(string directory, string name, bool needExt = true, CancellationToken ct = default) where T : UnityEngine.Object
+ public async UniTask<string[]> LoadConfigAsync(string name, bool needExt = true, CancellationToken ct = default)
{
- directory = directory.Replace("\\", "/");
- name = name.Replace("\\", "/");
-
- if (typeof(T) == typeof(Sprite))
+ if (AssetSource.isUseAssetBundle)
{
- return await LoadSpriteAsyncUniTask(directory, name, ct) as T;
- }
-
- if (typeof(T) == typeof(SkeletonDataAsset))
- {
- if (name.Contains("/"))
+ if (name.EndsWith(".txt") && needExt)
{
- directory += name.Substring(0, name.LastIndexOf("/"));
- name = name.Substring(name.LastIndexOf("/") + 1);
+ 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>();
}
- var path = ($"Assets/ResourcesOut/{directory}/{name}" + (needExt ? GetExtension(typeof(T)) : ""))
- .Replace("//", "/").Trim().Replace("\\", "/");
-
- if (!AssetSource.isUseAssetBundle)
- {
+ // 闈� AB 妯″紡: 鐩存帴璇绘枃浠讹紙Editor 寮�鍙戞ā寮忥級
#if UNITY_EDITOR
- return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(path);
+ string path = ResourcesPath.CONFIG_FODLER + "/" + name + (needExt ? ".txt" : "");
+ return await UniTask.RunOnThreadPool(() => File.ReadAllLines(path));
#else
- return null;
+ return Array.Empty<string>();
#endif
- }
-
- return await YooAssetService.Instance.LoadAssetAsync<T>(path, ct: ct);
}
+ // ====================================================================
+ // 缂撳瓨鍔犺浇 & Sprite 寮傛鍔犺浇锛堟墍鏈夊钩鍙帮級
+ // ====================================================================
+
/// <summary>
- /// US4: 寮傛鍔犺浇璧勬簮骞惰蛋缂撳瓨灞傦紙缂撳瓨鍛戒腑鐩存帴杩斿洖锛屾湭鍛戒腑鍒欏姞杞藉苟缂撳瓨锛夈��
+ /// 寮傛鍔犺浇璧勬簮骞惰蛋缂撳瓨灞傦紙缂撳瓨鍛戒腑鐩存帴杩斿洖锛屾湭鍛戒腑鍒欏姞杞藉苟缂撳瓨锛夈��
/// </summary>
public async UniTask<T> LoadAssetCachedAsync<T>(string directory, string name, bool needExt = true, CancellationToken ct = default) where T : UnityEngine.Object
{
@@ -312,7 +419,13 @@
var path = ($"Assets/ResourcesOut/{directory}/{name}" + (needExt ? GetExtension(typeof(T)) : ""))
.Replace("//", "/").Trim().Replace("\\", "/");
-
+ // 闊抽鏂囦欢鎵╁睍鍚嶇粺涓�杞皬鍐�
+ if (typeof(T) == typeof(AudioClip))
+ {
+ var pathExt2 = System.IO.Path.GetExtension(path);
+ if (!string.IsNullOrEmpty(pathExt2) && pathExt2 != pathExt2.ToLower())
+ path = path.Substring(0, path.Length - pathExt2.Length) + pathExt2.ToLower();
+ }
if (!AssetSource.isUseAssetBundle)
{
#if UNITY_EDITOR
@@ -336,49 +449,11 @@
{
var path = $"Assets/ResourcesOut/{atlasName}/{spriteName}.png"
.Replace("//", "/").Trim().Replace("\\", "/");
- return await YooAssetService.Instance.LoadAssetAsync<Sprite>(path, ct: ct);
+ var sprite = await YooAssetService.Instance.LoadAssetAsync<Sprite>(path, ct: ct);
+ if (sprite == null)
+ 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, CancellationToken ct = default)
- {
- // AB 妯″紡锛堝惈 WebGL锛�: 浣跨敤 YooAsset 鍔犺浇閰嶇疆鏂囦欢
- if (AssetSource.isUseAssetBundle)
- {
- var location = $"Assets/ResourcesOut/Config/{name}.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 text = await ProjSG.Resource.YooAssetService.Instance.LoadRawFileTextAsync(location, ct);
- if (!string.IsNullOrEmpty(text))
- return 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 + ".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