Skip to content

Commit

Permalink
feat: create an editor tool for audio
Browse files Browse the repository at this point in the history
  • Loading branch information
Long18 committed Feb 21, 2024
1 parent 86c27d5 commit 534ca2a
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 5 deletions.
101 changes: 101 additions & 0 deletions Assets/AudioSystem/Scripts/Editor/AudioCueEventChannelSOEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Collections;
using Long18.AudioSystem.Data;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Long18Editor.AudioSystem
{
[CustomEditor(typeof(AudioCueEventChannelSO))]
public class AudioCueEventChannelSOEditor : Editor
{
private const int FRAMES_TO_WAIT = 360;

[SerializeField] private VisualTreeAsset _visualTreeAsset;

private AudioCueEventChannelSO Target => target as AudioCueEventChannelSO;

private VisualElement _bgmElement;
private VisualElement _sfxElement;
private ObjectField _bgmData;
private ObjectField _sfxData;
private IntegerField _sfxRandomCount;
private Toggle _isPlayRequest;
private Toggle _isSfx;
private Toggle _isAuto;
private Button _playButton;

private bool _isSfxRequest;

public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();

InspectorElement.FillDefaultInspector(root, serializedObject, this);

_visualTreeAsset.CloneTree(root);

_bgmElement = root.Q<VisualElement>("bgm-element");
_sfxElement = root.Q<VisualElement>("sfx-element");

_bgmData = root.Q<ObjectField>("bgm-data");
_sfxData = root.Q<ObjectField>("sfx-data");

_sfxRandomCount = root.Q<IntegerField>("sfx-random-value");

_isPlayRequest = root.Q<Toggle>("audio-request");
_isSfx = root.Q<Toggle>("is-sfx");
_isAuto = root.Q<Toggle>("is-random");

_playButton = root.Q<Button>("play-audio-button");

_isSfx.RegisterValueChangedCallback(ValidAudioDataInterfaceCallback);
_bgmData.RegisterValueChangedCallback(ValidAudioButtonInterfaceCallback);
_sfxData.RegisterValueChangedCallback(ValidAudioButtonInterfaceCallback);

_playButton.clicked += PlayAudio;
_playButton.SetEnabled(_bgmData.value != null || _sfxData.value != null);

return root;
}

private void ValidAudioButtonInterfaceCallback(ChangeEvent<Object> evt)
{
bool isValid = evt.newValue != null || _sfxData.value != null;
_playButton.SetEnabled(isValid);
}

private void ValidAudioDataInterfaceCallback(ChangeEvent<bool> evt)
{
_isSfxRequest = evt.newValue;
_bgmElement.style.display = _isSfxRequest ? DisplayStyle.None : DisplayStyle.Flex;
_sfxElement.style.display = _isSfxRequest ? DisplayStyle.Flex : DisplayStyle.None;
}

private void PlayAudio()
{
AudioCueSO value = _bgmData.value as AudioCueSO;
bool request = _isPlayRequest.value;

if (_isSfxRequest) value = _sfxData.value as AudioCueSO;
if (!_isAuto.value)
{
Target.PlayAudio(value, request);
return;
}

for (int i = 0; i < _sfxRandomCount.value; i++)
{
int framesToWait = FRAMES_TO_WAIT;
while (framesToWait >= 0)
{
framesToWait--;
EditorApplication.QueuePlayerLoopUpdate();
}

Target.PlayAudio(value, request);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions Assets/AudioSystem/Scripts/Editor/AudioCueSOEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Long18.AudioSystem.Data;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;

namespace Long18Editor.AudioSystem
{
[CustomEditor(typeof(AudioCueSO), true)]
public class AudioCueSOEditor : Editor
{
[SerializeField] private VisualTreeAsset _visualTreeAsset;

private AudioCueSO Target => target as AudioCueSO;
private Button _button;

public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();

InspectorElement.FillDefaultInspector(root, serializedObject, this);

_visualTreeAsset.CloneTree(root);

_button = root.Q<Button>("play-audio-button");
_button.SetEnabled(Target.GetClips().Length > 0);

// TODO: Remove after function implementation
_button.SetEnabled(false);

_button.clicked += PlayAudio;

return root;
}

private void PlayAudio()
{
// TODO: Play audio with out event channel
}
}
}
12 changes: 12 additions & 0 deletions Assets/AudioSystem/Scripts/Editor/AudioCueSOEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Assets/AudioSystem/Scripts/Editor/AudioSettingsSOEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class AudioSettingsSOEditor : Editor
[SerializeField] private VisualTreeAsset _visualTreeAsset;

private AudioSettingSO Target => target as AudioSettingSO;
private Slider volumeSlider;
private Slider _volumeSlider;

public override VisualElement CreateInspectorGUI()
{
Expand All @@ -22,17 +22,17 @@ public override VisualElement CreateInspectorGUI()

_visualTreeAsset.CloneTree(root);

volumeSlider = root.Q<Slider>("volume-slider");
volumeSlider.value = Target.Volume;
_volumeSlider = root.Q<Slider>("volume-slider");
_volumeSlider.value = Target.Volume;

volumeSlider.RegisterValueChangedCallback(ChangeValueEditorCallback);
_volumeSlider.RegisterValueChangedCallback(ChangeValueEditorCallback);
Target.OnVolumeChanged += ChangeMasterVolume;

return root;
}

private void ChangeValueEditorCallback(ChangeEvent<float> evt) => Target.Volume = evt.newValue;
private void ChangeMasterVolume(float value) => volumeSlider.value = value;
private void ChangeMasterVolume(float value) => _volumeSlider.value = value;
private void OnDisable() => Target.OnVolumeChanged -= ChangeMasterVolume;
}
}
8 changes: 8 additions & 0 deletions Assets/AudioSystem/Scripts/Editor/Template.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<ui:VisualElement name="VisualElement" style="margin-top: 0; margin-bottom: 10px;" />
<ui:Label tabindex="-1" text="Tool Editor" display-tooltip-when-elided="true" style="font-size: 24px; -unity-font-style: bold; -unity-text-align: upper-center;" />
<ui:Label tabindex="-1" text="Event Audio" display-tooltip-when-elided="true" style="font-size: 12px; -unity-font-style: bold; -unity-text-align: upper-center; margin-top: -3px; margin-bottom: 5px; color: rgb(152, 152, 152);" />
<ui:Toggle label="Request To Play" tooltip="Check true to play, false to pause" name="audio-request" value="true" />
<ui:Toggle label="Is SFX" name="is-sfx" value="false" />
<ui:Foldout text="Data" name="foldout">
<ui:VisualElement name="bgm-element" style="flex-grow: 1;">
<uie:ObjectField label="Audio To Play" name="bgm-data" type="Long18.AudioSystem.Data.AudioCueSO, Long18.AudioSystem" />
</ui:VisualElement>
<ui:VisualElement name="sfx-element" style="flex-grow: 1; display: none;">
<uie:ObjectField label="Audio To Play" name="sfx-data" type="Long18.AudioSystem.Data.AudioCueSO, Long18.AudioSystem" />
<ui:IntegerField label="Random Time" value="1" name="sfx-random-value" tooltip="Number of audio want to play" />
<ui:Toggle label="Loop" name="is-random" />
</ui:VisualElement>
</ui:Foldout>
<ui:Button text="Play Audio" parse-escape-sequences="true" display-tooltip-when-elided="true" name="play-audio-button" style="align-items: center; justify-content: center; align-self: center;" />
</ui:UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<ui:VisualElement name="VisualElement" style="margin-top: 0; margin-bottom: 10px;" />
<ui:Label tabindex="-1" text="Tool Editor" display-tooltip-when-elided="true" style="font-size: 24px; -unity-font-style: bold; -unity-text-align: upper-center;" />
<ui:Label tabindex="-1" text="Audio" display-tooltip-when-elided="true" style="font-size: 12px; -unity-font-style: bold; -unity-text-align: upper-center; margin-top: -3px; margin-bottom: 5px; color: rgb(152, 152, 152);" />
<ui:Button text="Play Audio" parse-escape-sequences="true" display-tooltip-when-elided="true" name="play-audio-button" style="align-items: center; justify-content: center; align-self: center;" />
</ui:UXML>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 534ca2a

Please sign in to comment.