feat: 在Unity内支持直接调整音频文件进行混音

This commit is contained in:
2026-05-19 12:04:31 +08:00
parent 3c2558f5e7
commit f1c0fb5fae
4 changed files with 77 additions and 12 deletions
@@ -21,10 +21,13 @@ namespace OCES
[CanBeNull] [CanBeNull]
internal T LoadSync<T>(string path) where T : Object internal T LoadSync<T>(string path) where T : Object
{ {
#if !AUDIO_MIXING
if (this.m_cachedObjects.TryGetValue(path, out Object cachedObject)) if (this.m_cachedObjects.TryGetValue(path, out Object cachedObject))
{ {
return cachedObject as T; return cachedObject as T;
} }
#endif
T newObject; T newObject;
if (this.m_assetProvider is not null) if (this.m_assetProvider is not null)
@@ -37,7 +40,9 @@ namespace OCES
Debug.LogWarning($"[ResourceLoader] No IAssetProvider set, falling back to Resources.Load for '{path}'"); Debug.LogWarning($"[ResourceLoader] No IAssetProvider set, falling back to Resources.Load for '{path}'");
} }
#if !AUDIO_MIXING
this.m_cachedObjects.Add(path, newObject); this.m_cachedObjects.Add(path, newObject);
#endif
return newObject; return newObject;
} }
@@ -23,11 +23,10 @@ namespace OCES
{ {
if (typeof(T) != typeof(AudioClip)) if (typeof(T) != typeof(AudioClip))
{ {
Debug.LogError($"[StreamingAssetProvider] Load<{typeof(T).Name}> 不支持,仅支持 AudioClip"); return Resources.Load<T>(path);
return null;
} }
string fullPath = Path.Combine(Application.streamingAssetsPath, path); string fullPath = Path.Combine("Audios", Application.streamingAssetsPath, path) + ".wav";
if (!File.Exists(fullPath)) if (!File.Exists(fullPath))
{ {
Debug.LogError($"[StreamingAssetProvider] 文件 {fullPath} 不存在。"); Debug.LogError($"[StreamingAssetProvider] 文件 {fullPath} 不存在。");
@@ -48,25 +47,26 @@ namespace OCES
{ {
if (typeof(T) != typeof(AudioClip)) if (typeof(T) != typeof(AudioClip))
{ {
Debug.LogError($"[StreamingAssetProvider] LoadAsync<{typeof(T).Name}> 不支持,仅支持 AudioClip"); ResourceRequest resourceRequest = Resources.LoadAsync<T>(path);
onComplete?.Invoke(null); yield return resourceRequest;
onComplete?.Invoke(resourceRequest.asset as T);
yield break; yield break;
} }
string fullPath = Path.Combine(Application.streamingAssetsPath, path); string fullPath = Path.Combine("Audios", Application.streamingAssetsPath, path);
string url = "file://" + fullPath; string url = "file://" + fullPath;
using UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV); using UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV);
yield return request.SendWebRequest(); 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); onComplete?.Invoke(clip as T);
} }
else else
{ {
Debug.LogError($"[StreamingAssetProvider] 加载失败: {url}, 错误: {request.error}"); Debug.LogError($"[StreamingAssetProvider] 加载失败: {url}, 错误: {webRequest.error}");
onComplete?.Invoke(null); onComplete?.Invoke(null);
} }
} }
@@ -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<NamedBuildTarget> s_selectedBuildTargets = new()
{
NamedBuildTarget.Android,
NamedBuildTarget.iOS,
NamedBuildTarget.Standalone,
};
static readonly AudioExtendSettings s_audioExtendSettings = AssetDatabase.LoadAssetAtPath<AudioExtendSettings>(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<string> 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<string> defineList = defineArray.ToList();
if (defineList.Contains("AUDIO_MIXING"))
{
defineList.Remove("AUDIO_MIXING");
PlayerSettings.SetScriptingDefineSymbols(buildTarget, defineList.ToArray());
}
}
}
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 388d2124804048c1a9ea3c403820b443
timeCreated: 1779159928