From 9dc4228b5c6f09c24ed6e7ad2e7097ac4ec6630c Mon Sep 17 00:00:00 2001 From: Oliver Wong Date: Tue, 19 May 2026 12:04:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8Unity=E5=86=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=9B=B4=E6=8E=A5=E8=B0=83=E6=95=B4=E9=9F=B3=E9=A2=91?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BF=9B=E8=A1=8C=E6=B7=B7=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OCES/AssetsManagement/ResourceLoader.cs | 7 ++- .../StreamingAssetProvider.cs | 22 +++---- .../Audio/HandWritten/Editor/MixingHelper.cs | 57 +++++++++++++++++++ .../HandWritten/Editor/MixingHelper.cs.meta | 3 + 4 files changed, 77 insertions(+), 12 deletions(-) create mode 100644 Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs create mode 100644 Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs.meta diff --git a/Assets/Scripts/OCES/AssetsManagement/ResourceLoader.cs b/Assets/Scripts/OCES/AssetsManagement/ResourceLoader.cs index 77d00e2..dca1cb7 100644 --- a/Assets/Scripts/OCES/AssetsManagement/ResourceLoader.cs +++ b/Assets/Scripts/OCES/AssetsManagement/ResourceLoader.cs @@ -21,10 +21,13 @@ namespace OCES [CanBeNull] internal T LoadSync(string path) where T : Object { + +#if !AUDIO_MIXING if (this.m_cachedObjects.TryGetValue(path, out Object cachedObject)) { return cachedObject as T; } +#endif T newObject; if (this.m_assetProvider is not null) @@ -36,8 +39,10 @@ namespace OCES newObject = Resources.Load(path); Debug.LogWarning($"[ResourceLoader] No IAssetProvider set, falling back to Resources.Load for '{path}'"); } - + +#if !AUDIO_MIXING this.m_cachedObjects.Add(path, newObject); +#endif return newObject; } diff --git a/Assets/Scripts/OCES/AssetsManagement/StreamingAssetProvider.cs b/Assets/Scripts/OCES/AssetsManagement/StreamingAssetProvider.cs index 0260454..25bdad9 100644 --- a/Assets/Scripts/OCES/AssetsManagement/StreamingAssetProvider.cs +++ b/Assets/Scripts/OCES/AssetsManagement/StreamingAssetProvider.cs @@ -23,11 +23,10 @@ namespace OCES { if (typeof(T) != typeof(AudioClip)) { - Debug.LogError($"[StreamingAssetProvider] Load<{typeof(T).Name}> 不支持,仅支持 AudioClip"); - return null; + return Resources.Load(path); } - string fullPath = Path.Combine(Application.streamingAssetsPath, path); + string fullPath = Path.Combine("Audios", Application.streamingAssetsPath, path) + ".wav"; if (!File.Exists(fullPath)) { Debug.LogError($"[StreamingAssetProvider] 文件 {fullPath} 不存在。"); @@ -48,25 +47,26 @@ namespace OCES { if (typeof(T) != typeof(AudioClip)) { - Debug.LogError($"[StreamingAssetProvider] LoadAsync<{typeof(T).Name}> 不支持,仅支持 AudioClip"); - onComplete?.Invoke(null); + ResourceRequest resourceRequest = Resources.LoadAsync(path); + yield return resourceRequest; + onComplete?.Invoke(resourceRequest.asset as T); yield break; } - string fullPath = Path.Combine(Application.streamingAssetsPath, path); + string fullPath = Path.Combine("Audios", Application.streamingAssetsPath, path); string url = "file://" + fullPath; - using UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV); - yield return request.SendWebRequest(); + using UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV); + yield return webRequest.SendWebRequest(); - if (request.result == UnityWebRequest.Result.Success) + if (webRequest.result == UnityWebRequest.Result.Success) { - AudioClip clip = DownloadHandlerAudioClip.GetContent(request); + AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest); onComplete?.Invoke(clip as T); } else { - Debug.LogError($"[StreamingAssetProvider] 加载失败: {url}, 错误: {request.error}"); + Debug.LogError($"[StreamingAssetProvider] 加载失败: {url}, 错误: {webRequest.error}"); onComplete?.Invoke(null); } } diff --git a/Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs b/Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs new file mode 100644 index 0000000..4be0972 --- /dev/null +++ b/Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs @@ -0,0 +1,57 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEditor.Build; + +namespace OCES.Audio.HandWritten.Editor +{ + public static class MixingHelper + { + static readonly List s_selectedBuildTargets = new() + { + NamedBuildTarget.Android, + NamedBuildTarget.iOS, + NamedBuildTarget.Standalone, + }; + + static readonly AudioExtendSettings s_audioExtendSettings = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath("de80878c933394e2da0966a1466fd793")); + + [MenuItem("Tools/OCES/Audio/Enable Mixing")] + public static void EnableMixing() + { + if (s_audioExtendSettings is not null && s_audioExtendSettings.useAssetBundle) + { + EditorUtility.DisplayDialog("Mixing", "已启用Asset Bundle。\n对音频文件的调整不会生效。", "好吧"); + } + + foreach (NamedBuildTarget buildTarget in s_selectedBuildTargets) + { + PlayerSettings.GetScriptingDefineSymbols(buildTarget, out string[] defineArray); + List defineList = defineArray.ToList(); + + if (!defineArray.Contains("AUDIO_MIXING")) + { + defineList.Add("AUDIO_MIXING"); + PlayerSettings.SetScriptingDefineSymbols(buildTarget, defineList.ToArray()); + } + } + } + + [MenuItem("Tools/OCES/Audio/Disable Mixing")] + public static void DisableMixing() + { + foreach (NamedBuildTarget buildTarget in s_selectedBuildTargets) + { + PlayerSettings.GetScriptingDefineSymbols(buildTarget, out string[] defineArray); + List defineList = defineArray.ToList(); + + if (defineList.Contains("AUDIO_MIXING")) + { + defineList.Remove("AUDIO_MIXING"); + PlayerSettings.SetScriptingDefineSymbols(buildTarget, defineList.ToArray()); + } + } + } + } +} diff --git a/Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs.meta b/Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs.meta new file mode 100644 index 0000000..18171b1 --- /dev/null +++ b/Assets/Scripts/OCES/Audio/HandWritten/Editor/MixingHelper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 388d2124804048c1a9ea3c403820b443 +timeCreated: 1779159928 \ No newline at end of file