Add SoundEffect audio system (ScriptableObject-based) with test assets
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
75f5704ab8
commit
0be503c927
22 files changed
+741
No files matched your search
@@ -0,0 +1,142 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
using UnityEditor;
|
||||
using UnityEditor.UIElements;
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace Prototype.Editor
|
||||
{
|
||||
public class AudioMixerWindow : EditorWindow
|
||||
{
|
||||
[MenuItem("Tools/Audio Mixer")]
|
||||
static void Init()
|
||||
{
|
||||
GetWindow<AudioMixerWindow>().titleContent = new("Audio Mixer");
|
||||
}
|
||||
|
||||
AudioMixerGroup audioMixerGroup;
|
||||
|
||||
List<SoundEffect> soundEffects;
|
||||
|
||||
readonly List<(SoundEffect soundEffect, VisualElement element)> soundEffectTracks = new();
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
audioMixerGroup = null;
|
||||
|
||||
soundEffects = null;
|
||||
|
||||
RefreshLayout();
|
||||
}
|
||||
|
||||
void RefreshLayout()
|
||||
{
|
||||
var root = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(
|
||||
AssetDatabase.GUIDToAssetPath(
|
||||
AssetDatabase.FindAssets(
|
||||
$"t:{nameof(VisualTreeAsset)} {nameof(AudioMixerWindow)}"
|
||||
)[0]
|
||||
)
|
||||
).Instantiate();
|
||||
|
||||
rootVisualElement.Clear();
|
||||
rootVisualElement.Add(root);
|
||||
|
||||
root.StretchToParentSize();
|
||||
|
||||
root.Q<ObjectField>().RegisterValueChangedCallback(x =>
|
||||
{
|
||||
audioMixerGroup = x.newValue as AudioMixerGroup;
|
||||
RefreshTracksVisibility();
|
||||
});
|
||||
}
|
||||
|
||||
void OnInspectorUpdate()
|
||||
{
|
||||
var refreshTracksVisibility = false;
|
||||
|
||||
if (soundEffects is null)
|
||||
{
|
||||
soundEffects = new();
|
||||
|
||||
foreach (var guid in AssetDatabase.FindAssets($"t:{nameof(SoundEffect)}"))
|
||||
{
|
||||
soundEffects.Add(
|
||||
AssetDatabase.LoadAssetAtPath<SoundEffect>(
|
||||
AssetDatabase.GUIDToAssetPath(guid)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
RefreshSoundEffectTracks();
|
||||
refreshTracksVisibility = true;
|
||||
}
|
||||
|
||||
if (refreshTracksVisibility)
|
||||
RefreshTracksVisibility();
|
||||
}
|
||||
|
||||
void RefreshSoundEffectTracks()
|
||||
{
|
||||
soundEffectTracks.Clear();
|
||||
|
||||
if (soundEffects is not null)
|
||||
{
|
||||
foreach (var soundEffect in soundEffects)
|
||||
{
|
||||
var track = new VisualElement();
|
||||
track.AddToClassList("track");
|
||||
var applyButton = new Button(soundEffect.ApplyVolumeMultiplierFactor)
|
||||
{
|
||||
text = "Apply"
|
||||
};
|
||||
applyButton.AddToClassList("accent");
|
||||
track.Add(applyButton);
|
||||
track.Add(
|
||||
new AudioMixerSliderGUI
|
||||
{
|
||||
@object = soundEffect,
|
||||
propertyPath = "volumeMultiplierFactor",
|
||||
valueRange = new Vector2(0, 2),
|
||||
defaultValue = 1
|
||||
}
|
||||
);
|
||||
track.Add(
|
||||
new Label(
|
||||
soundEffect.name
|
||||
.Replace(" (Sound Effect)", "")
|
||||
.Replace(" - BGM", "")
|
||||
.Replace(" - SFX", "")
|
||||
)
|
||||
);
|
||||
track.Add(
|
||||
new Button(() => Selection.activeObject = soundEffect)
|
||||
{
|
||||
text = "Select"
|
||||
}
|
||||
);
|
||||
soundEffectTracks.Add((soundEffect, track));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RefreshTracksVisibility()
|
||||
{
|
||||
var content = rootVisualElement.Q<ScrollView>().contentContainer;
|
||||
content.Clear();
|
||||
|
||||
foreach (var (soundEffect, track) in soundEffectTracks)
|
||||
{
|
||||
if (audioMixerGroup &&
|
||||
audioMixerGroup != soundEffect.audioMixerGroup)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
content.Add(track);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user