HKI Core
AudioSystem.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using HKI.Core.Init;
3 using HKI.Core.Loc;
4 
5 namespace HKI.Core.Audio
6 {
12  public class AudioSystem : ScriptableObject, IInit
13  {
14  // Public variables
15  public static AudioSystem Instance { get; private set; }
16 
17  [SerializeField] bool UseMusicAudioPlayer = true;
18  [SerializeField] AudioPlayer.AudioPlayerSettings MusicAudioPlayerSettings = new AudioPlayer.AudioPlayerSettings();
19  [SerializeField] bool UseAmbientAudioPlayer = true;
20  [SerializeField] AudioPlayer.AudioPlayerSettings AmbientAudioPlayerSettings = new AudioPlayer.AudioPlayerSettings();
21  [SerializeField] bool UseFxAudioPlayer = true;
22  [SerializeField] AudioPlayer.AudioPlayerSettings FxAudioPlayerSettings = new AudioPlayer.AudioPlayerSettings();
23  [SerializeField] bool UseUiFxAudioPlayer = true;
24  [SerializeField] AudioPlayer.AudioPlayerSettings UiFxAudioPlayerSettings = new AudioPlayer.AudioPlayerSettings();
25  [SerializeField] bool UseVoicesAudioPlayer = true;
26  [SerializeField] AudioPlayer.AudioPlayerSettings VoicesAudioPlayerSettings = new AudioPlayer.AudioPlayerSettings();
27 
28  // Private variables
29  SimpleAudioPlayer musicAudioPlayer = null;
30  SimpleAudioPlayer ambientAudioPlayer = null;
31  SimpleAudioPlayer fxAudioPlayer = null;
32  SimpleAudioPlayer uiFxAudioPlayer = null;
33  SimpleAudioPlayer voicesAudioPlayer = null;
34 
35  GameObject audioListenerGO = null;
36 
37  // Getter
38  public SimpleAudioPlayer MusicAudioPlayer { get { return musicAudioPlayer; } }
39  public SimpleAudioPlayer AmbientAudioPlayer { get { return ambientAudioPlayer; } }
40  public SimpleAudioPlayer FxAudioPlayer { get { return fxAudioPlayer; } }
41  public SimpleAudioPlayer UiFxAudioPlayer { get { return uiFxAudioPlayer; } }
42  public SimpleAudioPlayer VoicesAudioPlayer { get { return voicesAudioPlayer; } }
43 
44  // Init function
45  public void Init()
46  {
47  if(Instance = null)
48  Instance = this;
49 
50  audioListenerGO = new GameObject("Audio Listener");
51 
52  audioListenerGO.AddComponent<AudioListener>();
53  DontDestroyOnLoad(audioListenerGO);
54 
55  if (UseMusicAudioPlayer)
56  musicAudioPlayer = CreateAudioPlayer("Music", MusicAudioPlayerSettings);
57 
58  if(UseAmbientAudioPlayer)
59  ambientAudioPlayer = CreateAudioPlayer("Ambient", AmbientAudioPlayerSettings);
60 
61  if(UseFxAudioPlayer)
62  fxAudioPlayer = CreateAudioPlayer("Fx", FxAudioPlayerSettings);
63 
64  if(UseUiFxAudioPlayer)
65  uiFxAudioPlayer = CreateAudioPlayer("Ui Fx", UiFxAudioPlayerSettings);
66 
67  if(UseVoicesAudioPlayer)
68  voicesAudioPlayer = CreateAudioPlayer("Voices", VoicesAudioPlayerSettings);
69  }
70 
71  // OnDisable function
72  void OnDisable()
73  {
74  if(audioListenerGO != null)
75  Destroy(audioListenerGO);
76 
77  if (UseMusicAudioPlayer && musicAudioPlayer != null)
78  Destroy(musicAudioPlayer);
79 
80  if(UseAmbientAudioPlayer && ambientAudioPlayer != null)
81  Destroy(ambientAudioPlayer);
82 
83  if(UseFxAudioPlayer && fxAudioPlayer != null)
84  Destroy(fxAudioPlayer);
85 
86  if(UseUiFxAudioPlayer && uiFxAudioPlayer != null)
87  Destroy(uiFxAudioPlayer);
88 
89  if(UseVoicesAudioPlayer && voicesAudioPlayer != null)
90  Destroy(voicesAudioPlayer);
91 
92  if(Instance == this)
93  Instance = null;
94  }
95 
97  {
98  musicAudioPlayer.PlaySoundClip(soundclip);
99  }
100 
102  {
103  if(playlist != null)
104  musicAudioPlayer.PlaySoundClip(playlist.GetNextClip());
105  else
106  Debug.LogError("(AudioSystem) you are trying to play the next sound clip from a playlist with the music audio player but the playlist is null!");
107  }
108 
110  {
111  ambientAudioPlayer.PlaySoundClip(soundClip);
112  }
113 
115  {
116  if(playlist != null)
117  ambientAudioPlayer.PlaySoundClip(playlist.GetNextClip());
118  else
119  Debug.LogError("(AudioSystem) you are trying to play the next sound clip from a playlist with the ambient audio player but the playlist is null!");
120  }
121 
123  {
124  fxAudioPlayer.PlaySoundClip(soundClip);
125  }
126 
128  {
129  if(playlist != null)
130  fxAudioPlayer.PlaySoundClip(playlist.GetNextClip());
131  else
132  Debug.LogError("(AudioSystem) you are trying to play the next sound clip from a playlist with the fx audio player but the playlist is null!");
133  }
134 
136  {
137  uiFxAudioPlayer.PlaySoundClip(soundClip);
138  }
139 
141  {
142  if (playlist != null)
143  uiFxAudioPlayer.PlaySoundClip(playlist.GetNextClip());
144  else
145  Debug.LogError("(AudioSystem) you are trying to play the next sound clip from a playlist with the ui fx audio player but the playlist is null!");
146  }
147 
149  {
150  voicesAudioPlayer.PlaySoundClip(soundClip);
151  }
152 
154  {
155  if (playlist != null)
156  voicesAudioPlayer.PlaySoundClip(playlist.GetNextClip());
157  else
158  Debug.LogError("(AudioSystem) you are trying to play the next sound clip from a playlist with the voices audio player but the playlist is null!");
159  }
160 
161  public void PlayAudioLocalization(AudioLocalization audioLocalization)
162  {
163  voicesAudioPlayer.PlayAudioLocalization(audioLocalization);
164  }
165 
166  // Helper functions
167  SimpleAudioPlayer CreateAudioPlayer(string apName, AudioPlayer.AudioPlayerSettings settings)
168  {
169  if(settings == null)
170  return null;
171 
172  GameObject apGO = new GameObject(apName);
173  SimpleAudioPlayer ap = apGO.AddComponent<SimpleAudioPlayer>();
174 
175  ap.Setup(settings);
176 
177  return ap;
178  }
179  }
180 }
void PlayNextSoundClipOfPlaylistWithMusicAudioPlayer(Playlist playlist)
Definition: AudioSystem.cs:101
This ScriptableObject contains the data of a playlist for the AudioPlayer.
Definition: Playlist.cs:19
void PlaySoundClipWithVoicesAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:148
void PlayNextSoundClipOfPlaylistWithUiFxAudioPlayer(Playlist playlist)
Definition: AudioSystem.cs:140
This interface allows the initialization of anything by the Initialzer.
Definition: IInit.cs:8
void PlayAudioLocalization(AudioLocalization audioLocalization)
void Setup(AudioPlayerSettings settings)
SimpleAudioPlayer CreateAudioPlayer(string apName, AudioPlayer.AudioPlayerSettings settings)
Definition: AudioSystem.cs:167
void PlayNextSoundClipOfPlaylistWithVoicesAudioPlayer(Playlist playlist)
Definition: AudioSystem.cs:153
void PlayAudioLocalization(AudioLocalization audioLocalization)
Definition: AudioSystem.cs:161
The AudioSystem is responsble of the creation and access of the general purpose AudioPlayers.
Definition: AudioSystem.cs:12
This is a AudioPlayer that can play playlist or SoundClips. It allows to play multiple clips at once ...
void PlaySoundClipWithFxAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:122
SoundClip GetNextClip()
Definition: Playlist.cs:56
void PlaySoundClipWithAmbientAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:109
void PlayNextSoundClipOfPlaylistWithFxAudioPlayer(Playlist playlist)
Definition: AudioSystem.cs:127
void PlaySoundClipWithUiFxAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:135
void PlayNextSoundClipOfPlaylistWithAmbientAudioPlayer(Playlist playlist)
Definition: AudioSystem.cs:114
This is an extention of the SimpleAudioPlayer. It allows the automatic Setup of the SimpleAudioPlayer...
Definition: AudioPlayer.cs:12
This is a container ScriptableObject that holds all data required to play a AudioClip.
Definition: SoundClip.cs:11
void PlaySoundClipWithMusicAudioPlayer(SoundClip soundclip)
Definition: AudioSystem.cs:96
This ScriptableObject contains as a container for all audio localization information.