From 1ab047b5fdd933c38ba0519ec2e83a44512ea8d7 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期四, 26 三月 2026 17:46:11 +0800
Subject: [PATCH] webgl代码合并 1
---
Main/ResModule/ResManager.cs | 49 +++++++++++++++++++++++++++++++++++--------------
1 files changed, 35 insertions(+), 14 deletions(-)
diff --git a/Main/ResModule/ResManager.cs b/Main/ResModule/ResManager.cs
index 8ec4613..47471ba 100644
--- a/Main/ResModule/ResManager.cs
+++ b/Main/ResModule/ResManager.cs
@@ -189,10 +189,7 @@
public async UniTask<T> LoadAssetAsync<T>(string directory, string name, bool needExt = true) where T : UnityEngine.Object
{
- // 鏂扮増鐩存帴璧� YooAsset/寮傛鍔犺浇
- // 杩欓噷鍋囪鏈� YooAssetService.Instance.LoadAssetAsync<T>(...) 鍙敤
- string path = directory.EndsWith("/") ? directory + name : directory + "/" + name;
- return await YooAssetService.Instance.LoadAssetAsync<T>(path);
+ 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
@@ -246,7 +243,9 @@
public void UnloadAsset(string directory, string assetName)
{
- string path = directory.EndsWith("/") ? directory + assetName : directory + "/" + assetName;
+ directory = directory.Replace("\\", "/").TrimEnd('/');
+ assetName = assetName.Replace("\\", "/");
+ string path = ($"Assets/ResourcesOut/{directory}/{assetName}").Replace("//", "/");
YooAssetService.Instance.UnloadAsset(path);
}
@@ -289,7 +288,13 @@
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
@@ -312,7 +317,13 @@
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);
+ if (!string.IsNullOrEmpty(pathExt2) && pathExt2 != pathExt2.ToLower())
+ path = path.Substring(0, path.Length - pathExt2.Length) + pathExt2.ToLower();
+ }
if (!AssetSource.isUseAssetBundle)
{
#if UNITY_EDITOR
@@ -336,7 +347,12 @@
{
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 UNITY_WEBGL
+ if (sprite == null)
+ Debug.LogWarning($"[ResManager][WebGL-Diag] Sprite load returned NULL: path={path}");
+#endif
+ return sprite;
}
}
@@ -344,12 +360,17 @@
/// 寮傛鍔犺浇閰嶇疆鏂囦欢锛圲niTask 鐗堟湰锛夈��
/// AB 妯″紡浣跨敤 YooAsset RawFile 寮傛鍔犺浇锛岄潪 AB 妯″紡鐩存帴璇绘枃浠躲��
/// </summary>
- public async UniTask<string[]> LoadConfigAsync(string name, CancellationToken ct = default)
+ public async UniTask<string[]> LoadConfigAsync(string name, bool needExt = true, CancellationToken ct = default)
{
// AB 妯″紡锛堝惈 WebGL锛�: 浣跨敤 YooAsset 鍔犺浇閰嶇疆鏂囦欢
if (AssetSource.isUseAssetBundle)
{
- var location = $"Assets/ResourcesOut/Config/{name}.txt";
+ // 鍒ゆ柇涓�涓嬫槸鍚﹀師鏉ュ凡缁忓寘鍚�.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
@@ -360,9 +381,9 @@
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);
+ 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)
@@ -374,7 +395,7 @@
// 闈� AB 妯″紡: 鐩存帴璇绘枃浠讹紙Editor 寮�鍙戞ā寮忥級
#if UNITY_EDITOR
- string path = ResourcesPath.CONFIG_FODLER + "/" + name + ".txt";
+ string path = ResourcesPath.CONFIG_FODLER + "/" + name + (needExt ? ".txt" : "");
return await UniTask.RunOnThreadPool(() => File.ReadAllLines(path));
#else
return System.Array.Empty<string>();
--
Gitblit v1.8.0