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 | 224 +++++++++++++++++++++++++++++++++----------------------
1 files changed, 133 insertions(+), 91 deletions(-)
diff --git a/Main/ResModule/ResManager.cs b/Main/ResModule/ResManager.cs
index bdf5de0..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;
@@ -9,9 +9,7 @@
using Cysharp.Threading.Tasks;
using System.Threading;
using ProjSG.Resource;
-
-
-
+using YooAsset;
#if UNITY_EDITOR
@@ -86,7 +84,7 @@
public void Init()
{
-
+
}
public void Release()
@@ -129,8 +127,11 @@
}
#endif
+ // ====================================================================
+ // 鍚屾鏂规硶锛堜粎闈� WebGL 骞冲彴锛�
+ // ====================================================================
+#if !UNITY_WEBGL
//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("\\", "/");
@@ -148,7 +149,6 @@
directory += name.Substring(0, name.LastIndexOf("/"));
name = name.Substring(name.LastIndexOf("/") + 1);
}
-
}
return LoadAssetInternal<T>(directory, name, needExt);
@@ -168,10 +168,7 @@
}
else
{
- // 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)
@@ -182,31 +179,29 @@
return asset;
}
- [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";
+ string path = ResourcesPath.CONFIG_FODLER + "/" + name + ".txt";
+ return File.ReadAllLines(path);
}
- else
#endif
- {
- path = AssetVersionUtility.GetAssetFilePath($"config/{name}.txt");
- }
-
- return File.ReadAllLines(path);
+ // 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)
{
- #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;
@@ -214,11 +209,19 @@
return atlas.GetSprite(spriteName);
}
else
- return LoadAssetInternal<Sprite>(atlasName, spriteName);
+ {
+ // YooAsset 浣跨敤瀹屾暣璺緞鐩存帴鍔犺浇 Sprite
+ var path = $"Assets/ResourcesOut/{atlasName}/{spriteName}.png".Replace("//", "/");
+ return YooAssetService.Instance.LoadAssetSync<Sprite>(path);
+ }
}
+#endif
- //needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�
- [System.Obsolete("US2: Use LoadAssetAsync<T>(directory, name, needExt) returning UniTask<T> instead.")]
+ // ====================================================================
+ // 寮傛鍔犺浇鏂规硶锛堟墍鏈夊钩鍙扮粺涓�锛�
+ // ====================================================================
+
+ //needExt 鏄惁闇�瑕佸嚱鏁板唴閮ㄦ坊鍔犲悗缂�锛堝洖璋冪増鏈級
public void LoadAssetAsync<T>(string directory, string name, Action<bool, UnityEngine.Object> callBack, bool needExt = true) where T : UnityEngine.Object
{
directory = directory.Replace("\\", "/");
@@ -230,9 +233,67 @@
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, 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("\\", "/");
+ // 闊抽鏂囦欢鎵╁睍鍚嶇粺涓�杞皬鍐欙紝閬垮厤 .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
{
@@ -264,7 +325,6 @@
}
else
{
- // US1: Route through YooAssetService async
CoLoadViaYooAsset<T>(path, callBack).Forget();
}
}
@@ -283,17 +343,15 @@
}
}
- [System.Obsolete("US1: Use YooAssetService.ReleaseHandle or UnloadUnusedAssetsAsync instead.")]
- public void UnloadAsset(string assetBundleName, string assetName)
+ public void UnloadAsset(string directory, string assetName)
{
- // US1: AssetBundleUtility unload no longer effective since assets loaded via YooAsset.
- // Proper unload handled via YooAssetService handle-based release.
- }
+ if (!AssetSource.isUseAssetBundle)
+ return;
- [System.Obsolete("US1: Use YooAssetService.UnloadUnusedAssetsAsync instead.")]
- public void UnloadAssetBundle(string assetBundleName, bool unloadAllLoadedObjects, bool includeDependenice)
- {
- // US1: AssetBundleUtility unload no longer effective since assets loaded via YooAsset.
+ directory = directory.Replace("\\", "/").TrimEnd('/');
+ assetName = assetName.Replace("\\", "/");
+ string path = ($"Assets/ResourcesOut/{directory}/{assetName}").Replace("//", "/");
+ YooAssetService.Instance.UnloadAsset(path);
}
public string GetAssetFilePath(string _assetKey)
@@ -308,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
{
@@ -358,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
@@ -382,36 +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 鐗堟湰锛夈��
- /// 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