From 08859e63d41f2515bb18d05e68dc4035f32b9b52 Mon Sep 17 00:00:00 2001 From: David Huss <dh@widerstandzwecklos.at> Date: Fri, 4 Dec 2020 09:48:57 +0100 Subject: [PATCH] Rename variables fix some behaviour --- Assets/SoundManager.cs | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/Assets/SoundManager.cs b/Assets/SoundManager.cs index 6901256..831b5d8 100644 --- a/Assets/SoundManager.cs +++ b/Assets/SoundManager.cs @@ -1,17 +1,19 @@ -using UnityEngine; +using UnityEngine; using UnityEngine.Events; using System.Collections; using System; +[RequireComponent(typeof(AudioSource))] +[RequireComponent(typeof(Collider))] public class SoundManager : MonoBehaviour { - public Boolean stop_on_exit = false; - public Boolean pause_on_exit = false; - public Boolean start_on_enter = true; - public Boolean start_after_awake = false; - public Boolean play_only_once = false; - public float start_delay = 0.0f; - public float stop_delay = 0.0f; + public Boolean stopOnExit = false; + public Boolean pauseOnExit = false; + public Boolean startOnEnter = true; + public Boolean startAfterAwake = false; + public Boolean playOnlyOnce = false; + public float startDelay = 0.0f; + public float stopDelay = 0.0f; Boolean never_played_before = true; AudioSource source; @@ -19,40 +21,42 @@ public class SoundManager : MonoBehaviour void Awake() { source = GetComponent<AudioSource>(); - if (start_after_awake) + source.playOnAwake = false; + source.Stop(); + if (startAfterAwake) { - Invoke("Play", start_delay); + Invoke("Play", startDelay); } } void OnTriggerEnter(Collider other) { - if (start_on_enter) + if (startOnEnter) { // Start sound only if we want to "restart on enter" // Otherwise only start it, if it isn't playing already if (never_played_before || !source.isPlaying) { - Invoke("Play", start_delay); + Invoke("Play", startDelay); } } } void OnTriggerExit(Collider other) { - if (stop_on_exit) + if (stopOnExit) { - Invoke("Stop", stop_delay); + Invoke("Stop", stopDelay); } - else if (pause_on_exit) + else if (pauseOnExit) { - Invoke("Pause", stop_delay); + Invoke("Pause", stopDelay); } } void Play() { - if (play_only_once) { + if (playOnlyOnce) { if (!Data.instance.PlayedSound(source.name)) { source.Play(); never_played_before = false; @@ -61,6 +65,8 @@ public class SoundManager : MonoBehaviour }else{ source.Play(); never_played_before = false; + Debug.LogFormat("Registered as Played: {0}", source.name); + Data.instance.RegisterSound(source.name); } } @@ -73,4 +79,4 @@ public class SoundManager : MonoBehaviour { source.Pause(); } -} \ No newline at end of file +} -- GitLab