HKI Core
PlaySoundClip.cs
Go to the documentation of this file.
1 using UnityEngine;
2 
3 namespace HKI.Core.Audio
4 {
8  public class PlaySoundClip : MonoBehaviour
9  {
10  // Enums
11  [System.Serializable]
12  public enum AudioPlayerTypes
13  {
14  Music,
15  Ambient,
16  Voices,
17  FX,
18  UIFX
19  }
20 
21  [System.Serializable]
23  {
24  Start,
25  Enable,
26  Disable,
27  Destroy,
28  Custom
29  }
30 
31  // Public variables
32  [SerializeField] AudioSystem AudioSystem = null;
33 
34  [SerializeField] AudioPlayerTypes AudioPlayerType = AudioPlayerTypes.Music;
35  [SerializeField] AudioPlayerPlayTypes PlayType = AudioPlayerPlayTypes.Enable;
36 
37  [SerializeField] SoundClip SoundClip = null;
38 
39  [SerializeField] float DelayPlayInSeconds = 0.0f;
40 
41  // Start function
42  void Start()
43  {
44  if (PlayType == AudioPlayerPlayTypes.Start)
45  {
46  if (DelayPlayInSeconds > 0)
47  Invoke("Play", DelayPlayInSeconds);
48  else
49  Play();
50  }
51  }
52 
53  // OnEnable function
54  void OnEnable()
55  {
56  if (PlayType == AudioPlayerPlayTypes.Enable)
57  {
58  if (DelayPlayInSeconds > 0)
59  Invoke("Play", DelayPlayInSeconds);
60  else
61  Play();
62  }
63  }
64 
65  // OnEnable function
66  void OnDisable()
67  {
68  if (PlayType == AudioPlayerPlayTypes.Disable)
69  {
70  if (DelayPlayInSeconds > 0)
71  Invoke("Play", DelayPlayInSeconds);
72  else
73  Play();
74  }
75  }
76 
77  // OnDestroy function
78  void OnDestroy()
79  {
80  if (PlayType == AudioPlayerPlayTypes.Destroy)
81  {
82  if (DelayPlayInSeconds > 0)
83  Invoke("Play", DelayPlayInSeconds);
84  else
85  Play();
86  }
87  }
88 
89  // Interface function
90  public void Play()
91  {
92  if(AudioSystem != null)
93  {
94  switch(AudioPlayerType)
95  {
96  case AudioPlayerTypes.Music: AudioSystem.PlaySoundClipWithMusicAudioPlayer(SoundClip); break;
97 
98  case AudioPlayerTypes.Ambient: AudioSystem.PlaySoundClipWithAmbientAudioPlayer(SoundClip); break;
99 
100  case AudioPlayerTypes.Voices: AudioSystem.PlaySoundClipWithVoicesAudioPlayer(SoundClip); break;
101 
102  case AudioPlayerTypes.FX: AudioSystem.PlaySoundClipWithFxAudioPlayer(SoundClip); break;
103 
104  case AudioPlayerTypes.UIFX: AudioSystem.PlaySoundClipWithUiFxAudioPlayer(SoundClip); break;
105  }
106  }
107  }
108  }
109 }
void PlaySoundClipWithVoicesAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:148
The AudioSystem is responsble of the creation and access of the general purpose AudioPlayers.
Definition: AudioSystem.cs:12
This MonoBehaviour allows playing of a SoundClip on one of the general purpose AudioPlayers.
Definition: PlaySoundClip.cs:8
void PlaySoundClipWithFxAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:122
void PlaySoundClipWithAmbientAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:109
void PlaySoundClipWithUiFxAudioPlayer(SoundClip soundClip)
Definition: AudioSystem.cs:135
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