Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
SoundController.cs
Go to the documentation of this file.
1using UnityEngine;
2
3public class SoundController : MonoBehaviour
4{
5 public enum SpeakerPosition
6 {
14 }
15
16 [Header("Assign AudioSources for each speaker")]
17 [SerializeField] private AudioSource speaker0_FrontLeft;
18 [SerializeField] private AudioSource speaker1_FrontRight;
19 [SerializeField] private AudioSource speaker2_Center;
20 [SerializeField] private AudioSource speaker4_RearLeft;
21 [SerializeField] private AudioSource speaker5_RearRight;
22 [SerializeField] private AudioSource speaker6_SideLeft;
23 [SerializeField] private AudioSource speaker7_SideRight;
24
25 [Header("Select which speaker to play")]
26 [SerializeField] private SpeakerPosition speakerToPlay;
27
28 public void PlaySelected()
29 {
30 AudioSource selectedSource = GetSelectedSource();
31
32 if (selectedSource == null)
33 return;
34
35 if (selectedSource.isPlaying)
36 {
37 selectedSource.Stop();
38 }
39 else
40 {
41 StopAll();
42 selectedSource.Play();
43 }
44 }
45
46 private void StopAll()
47 {
48 speaker0_FrontLeft?.Stop();
49 speaker1_FrontRight?.Stop();
50 speaker2_Center?.Stop();
51 speaker4_RearLeft?.Stop();
52 speaker5_RearRight?.Stop();
53 speaker6_SideLeft?.Stop();
54 speaker7_SideRight?.Stop();
55 }
56
57 private AudioSource GetSelectedSource()
58 {
59 return speakerToPlay switch
60 {
61 SpeakerPosition.Speaker_0_FrontLeft => speaker0_FrontLeft,
62 SpeakerPosition.Speaker_1_FrontRight => speaker1_FrontRight,
63 SpeakerPosition.Speaker_2_Center => speaker2_Center,
64 SpeakerPosition.Speaker_4_RearLeft => speaker4_RearLeft,
65 SpeakerPosition.Speaker_5_RearRight => speaker5_RearRight,
66 SpeakerPosition.Speaker_6_SideLeft => speaker6_SideLeft,
67 SpeakerPosition.Speaker_7_SideRight => speaker7_SideRight,
68 _ => null,
69 };
70 }
71
72#if UNITY_EDITOR
73 [UnityEditor.CustomEditor(typeof(SoundController))]
74 private class SoundControllerEditor : UnityEditor.Editor
75 {
76 public override void OnInspectorGUI()
77 {
78 DrawDefaultInspector();
79
80 SoundController controller = (SoundController)target;
81
82 if (GUILayout.Button("▶ Play/Stop Selected Speaker"))
83 {
84 controller.PlaySelected();
85 }
86
87 if (GUILayout.Button("⏹ Stop All"))
88 {
89 controller.SendMessage("StopAll", SendMessageOptions.DontRequireReceiver);
90 }
91 }
92 }
93#endif
94}