HKI Core
AudioSystemEditor.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.Audio;
3 using UnityEditor;
4 using HKI.Core.Loc;
5 using HKI.Core.Editor.Misc;
6 
7 namespace HKI.Core.Audio.Editor
8 {
12  [CustomEditor(typeof(AudioSystem))]
13  public class AudioSystemEditor : UnityEditor.Editor
14  {
15  // Private variables
16  const string useMusicAudioPlayerSPName = "UseMusicAudioPlayer";
17  const string musicAudioPlayerSettingsSPName = "MusicAudioPlayerSettings";
18  const string useAmbientAudioPlayerSPName = "UseAmbientAudioPlayer";
19  const string ambientAudioPlayerSettingsSPName = "AmbientAudioPlayerSettings";
20  const string useFxAudioPlayerSPName = "UseFxAudioPlayer";
21  const string fxAudioPlayerSettingsSPName = "FxAudioPlayerSettings";
22  const string useUiFxAudioPlayerSPName = "UseUiFxAudioPlayer";
23  const string uiFxAudioPlayerSettingsSPName = "UiFxAudioPlayerSettings";
24  const string useVoicesAudioPlayerSPName = "UseVoicesAudioPlayer";
25  const string voicesAudioPlayerSettingsSPName = "VoicesAudioPlayerSettings";
26 
28  ASEAudioPlayerSettingsEditor ambientAPS = null;
31  ASEAudioPlayerSettingsEditor voicesAPS = null;
32 
33  // Create function
34  [MenuItem("HKI/Core/Subsystems/Audio System")]
35  static void CreateAudioSystemMenu()
36  {
37  CreateAudioSystem();
38  }
39 
44  public static void CreateAudioSystem(string rootFolder = null)
45  {
46  if(EditorHelper.FindAndSelectIfExists("Audio System"))
47  return;
48 
49  // Create a new setting system
50  string folder = null;
51 
52  if (!string.IsNullOrEmpty(rootFolder))
53  folder = rootFolder;
54 
55  if (string.IsNullOrEmpty(folder))
56  {
57  folder = EditorHelper.GetFolderFromUser("Select folder for audio system to be saved in", "The given path for the audio system is null or empty");
58  if (string.IsNullOrEmpty(folder))
59  return;
60  }
61 
62  EditorUtility.DisplayProgressBar("Create audio system", "Create audio system", 0.0f);
63  AudioSystem audioSystem = EditorHelper.CreateAsset<AudioSystem>("Audio System", folder);
64 
65  EditorUtility.DisplayProgressBar("Create audio system", "Set settings", 0.5f);
66 
67  SerializedObject audioSystemSO = new SerializedObject(audioSystem);
68 
69  audioSystemSO.Update();
70 
71  AudioMixer audioMixer = EditorHelper.FindAndLoad<AudioMixer>(".mixer");
72 
73  string playlistFolder = folder + "Playlist/";
74 
75  ASEAudioPlayerSettingsEditor.Setup(audioSystemSO.FindProperty(musicAudioPlayerSettingsSPName), AudioPlayer.AudioPlayerSettings.Playmodes.SingleOutputWithCrossfade, 5.0f, true, EditorHelper.CreateAsset<Playlist>("Music Playlist", playlistFolder),
76  audioMixer == null ? null : audioMixer.FindMatchingGroups("Master/Music")[0], null, true);
77 
78  ASEAudioPlayerSettingsEditor.Setup(audioSystemSO.FindProperty(ambientAudioPlayerSettingsSPName), AudioPlayer.AudioPlayerSettings.Playmodes.SingleOutputWithCrossfade, 5.0f, false, EditorHelper.CreateAsset<Playlist>("Ambient Playlist", playlistFolder),
79  audioMixer == null ? null : audioMixer.FindMatchingGroups("Master/Ambient")[0], null, true);
80 
81  ASEAudioPlayerSettingsEditor.Setup(audioSystemSO.FindProperty(fxAudioPlayerSettingsSPName), AudioPlayer.AudioPlayerSettings.Playmodes.MultiOutput, 5.0f, false, EditorHelper.CreateAsset<Playlist>("Fx Playlist", playlistFolder),
82  audioMixer == null ? null : audioMixer.FindMatchingGroups("Master/Fx")[0], null, true);
83 
84  ASEAudioPlayerSettingsEditor.Setup(audioSystemSO.FindProperty(uiFxAudioPlayerSettingsSPName), AudioPlayer.AudioPlayerSettings.Playmodes.MultiOutput, 5.0f, false, EditorHelper.CreateAsset<Playlist>("Ui Fx Playlist", playlistFolder),
85  audioMixer == null ? null : audioMixer.FindMatchingGroups("Master/Ui Fx")[0], null, true);
86 
87  ASEAudioPlayerSettingsEditor.Setup(audioSystemSO.FindProperty(voicesAudioPlayerSettingsSPName), AudioPlayer.AudioPlayerSettings.Playmodes.MultiOutput, 5.0f, false, null,
88  audioMixer == null ? null : audioMixer.FindMatchingGroups("Master/Voices")[0], FindAudioLanguage(), true);
89 
90  audioSystemSO.ApplyModifiedProperties();
91 
92  EditorUtility.DisplayProgressBar("Create audio system", "Create audio system", 1.0f);
93 
94  // Finish
96 
97  Selection.activeObject = audioSystem;
98 
99  EditorUtility.ClearProgressBar();
100  }
101 
102  // OnEnable function
103  void OnEnable()
104  {
105  musicAPS = new ASEAudioPlayerSettingsEditor(serializedObject, "music", useMusicAudioPlayerSPName, musicAudioPlayerSettingsSPName);
106  ambientAPS = new ASEAudioPlayerSettingsEditor(serializedObject, "ambient", useAmbientAudioPlayerSPName, ambientAudioPlayerSettingsSPName);
107  fxAPS = new ASEAudioPlayerSettingsEditor(serializedObject, "fx", useFxAudioPlayerSPName, fxAudioPlayerSettingsSPName);
108  uiFxAPS = new ASEAudioPlayerSettingsEditor(serializedObject, "ui fx", useUiFxAudioPlayerSPName, uiFxAudioPlayerSettingsSPName);
109  voicesAPS = new ASEAudioPlayerSettingsEditor(serializedObject, "voices", useVoicesAudioPlayerSPName, voicesAudioPlayerSettingsSPName);
110  }
111 
112  // OnInspectorGUI function
113  public override void OnInspectorGUI()
114  {
115  serializedObject.Update();
116 
117  musicAPS.DrawEditorGUI();
118 
119  EditorGUILayout.Space();
120 
121  ambientAPS.DrawEditorGUI();
122 
123  EditorGUILayout.Space();
124 
125  fxAPS.DrawEditorGUI();
126 
127  EditorGUILayout.Space();
128 
129  uiFxAPS.DrawEditorGUI();
130 
131  EditorGUILayout.Space();
132 
133  voicesAPS.DrawEditorGUI();
134 
135  serializedObject.ApplyModifiedProperties();
136  }
137 
139  {
140  string[] GUIDs = AssetDatabase.FindAssets("t:" + typeof(Language).Name);
141  if(GUIDs.Length > 0)
142  {
143  for (int i = 0; i < GUIDs.Length; i++)
144  {
145  string assetPath = AssetDatabase.GUIDToAssetPath(GUIDs[i]);
146  Object[] objs = AssetDatabase.LoadAllAssetsAtPath(assetPath);
147  string filename = "Audio Language";
148  foreach(Object obj in objs)
149  {
150  if (obj.name == filename)
151  return (Language)obj;
152  }
153  }
154  }
155 
156  return null;
157  }
158  }
159 }
This ScriptableObject contains the data of a playlist for the AudioPlayer.
Definition: Playlist.cs:19
static void AddScriptableObjectToInitializer(ScriptableObject obj)
This function adds a scriptable object as a subasset to the initializer asset.
Custom inspector for the AudioSystem class.
static bool FindAndSelectIfExists(string filter, string fileEnding=".asset")
Definition: EditorHelper.cs:13
static void Setup(SerializedProperty property, AudioPlayer.AudioPlayerSettings.Playmodes playmode, float fadingTimeInSeconds, bool atSetupStartPlayingPlaylist, Playlist startPlaylist, AudioMixerGroup audioMixerGroup, Language localizationLanguage, bool dontGetDestroyedOnLoad)
This class containes helper functions for editor scripts.
Definition: EditorHelper.cs:10
static string GetFolderFromUser(string panelText, string errorMsg)
Definition: EditorHelper.cs:32
The AudioSystem is responsble of the creation and access of the general purpose AudioPlayers.
Definition: AudioSystem.cs:12
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
Definition: Language.cs:13
This class contains the creation function of the menu for all HKI Core systems.
static Object FindAndLoad(string objectType, string fileEnding=".asset")
Definition: EditorHelper.cs:91
This is an extention of the SimpleAudioPlayer. It allows the automatic Setup of the SimpleAudioPlayer...
Definition: AudioPlayer.cs:12
This class is for code seperation. It&#39;s for drawing the AudioPlayerSettings editor GUI...
static void CreateAudioSystem(string rootFolder=null)
This function creates audio systems and all the other required assets like playlists for the HKI Core...