Files
chameleon/Assets/Scripts/Audio/Editor/AudioMixerWindow.cs
T

143 lines
2.9 KiB
C#

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);
}
}
}
}