diff --git a/Assets/Fungus/Scripts/Commands/CallMethod.cs b/Assets/Fungus/Scripts/Commands/CallMethod.cs index df7dc2a5..d0d9e718 100644 --- a/Assets/Fungus/Scripts/Commands/CallMethod.cs +++ b/Assets/Fungus/Scripts/Commands/CallMethod.cs @@ -41,7 +41,7 @@ namespace Fungus.Commands return; } - if (delay == 0f) + if (Mathf.Approximately(delay, 0f)) { CallTheMethod(); } diff --git a/Assets/Fungus/Scripts/Commands/FadeScreen.cs b/Assets/Fungus/Scripts/Commands/FadeScreen.cs index e00a3b11..d8300d4b 100644 --- a/Assets/Fungus/Scripts/Commands/FadeScreen.cs +++ b/Assets/Fungus/Scripts/Commands/FadeScreen.cs @@ -35,18 +35,18 @@ namespace Fungus.Commands public override void OnEnter() { - var cameraController = CameraController.GetInstance(); + var cameraManager = FungusManager.Instance.CameraManager; if (fadeTexture) { - cameraController.ScreenFadeTexture = fadeTexture; + cameraManager.ScreenFadeTexture = fadeTexture; } else { - cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(fadeColor, 32, 32); + cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(fadeColor, 32, 32); } - cameraController.Fade(targetAlpha, duration, delegate { + cameraManager.Fade(targetAlpha, duration, delegate { if (waitUntilFinished) { Continue(); diff --git a/Assets/Fungus/Scripts/Commands/FadeToView.cs b/Assets/Fungus/Scripts/Commands/FadeToView.cs index d49d9e2a..169895fe 100644 --- a/Assets/Fungus/Scripts/Commands/FadeToView.cs +++ b/Assets/Fungus/Scripts/Commands/FadeToView.cs @@ -71,18 +71,18 @@ namespace Fungus.Commands return; } - var cameraController = CameraController.GetInstance(); + var cameraManager = FungusManager.Instance.CameraManager; if (fadeTexture) { - cameraController.ScreenFadeTexture = fadeTexture; + cameraManager.ScreenFadeTexture = fadeTexture; } else { - cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(fadeColor, 32, 32); + cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(fadeColor, 32, 32); } - cameraController.FadeToView(targetCamera, targetView, duration, fadeOut, delegate { + cameraManager.FadeToView(targetCamera, targetView, duration, fadeOut, delegate { if (waitUntilFinished) { Continue(); @@ -97,7 +97,9 @@ namespace Fungus.Commands public override void OnStopExecuting() { - CameraController.GetInstance().Stop(); + var cameraManager = FungusManager.Instance.CameraManager; + + cameraManager.Stop(); } public override string GetSummary() diff --git a/Assets/Fungus/Scripts/Commands/FadeUI.cs b/Assets/Fungus/Scripts/Commands/FadeUI.cs index e4978357..6927a91e 100644 --- a/Assets/Fungus/Scripts/Commands/FadeUI.cs +++ b/Assets/Fungus/Scripts/Commands/FadeUI.cs @@ -37,7 +37,7 @@ namespace Fungus.Commands { foreach (Image image in go.GetComponentsInChildren()) { - if (duration == 0f) + if (Mathf.Approximately(duration, 0f)) { switch (fadeMode) { @@ -67,7 +67,7 @@ namespace Fungus.Commands foreach (Text text in go.GetComponentsInChildren()) { - if (duration == 0f) + if (Mathf.Approximately(duration, 0f)) { switch (fadeMode) { @@ -97,7 +97,7 @@ namespace Fungus.Commands foreach (TextMesh textMesh in go.GetComponentsInChildren()) { - if (duration == 0f) + if (Mathf.Approximately(duration, 0f)) { switch (fadeMode) { diff --git a/Assets/Fungus/Scripts/Commands/MoveToView.cs b/Assets/Fungus/Scripts/Commands/MoveToView.cs index ba14ce11..49aca8b5 100644 --- a/Assets/Fungus/Scripts/Commands/MoveToView.cs +++ b/Assets/Fungus/Scripts/Commands/MoveToView.cs @@ -58,13 +58,13 @@ namespace Fungus.Commands return; } - var cameraController = CameraController.GetInstance(); + var cameraManager = FungusManager.Instance.CameraManager; Vector3 targetPosition = targetView.transform.position; Quaternion targetRotation = targetView.transform.rotation; float targetSize = targetView.ViewSize; - cameraController.PanToPosition(targetCamera, targetPosition, targetRotation, targetSize, duration, delegate { + cameraManager.PanToPosition(targetCamera, targetPosition, targetRotation, targetSize, duration, delegate { if (waitUntilFinished) { Continue(); @@ -79,7 +79,9 @@ namespace Fungus.Commands public override void OnStopExecuting() { - CameraController.GetInstance().Stop(); + var cameraManager = FungusManager.Instance.CameraManager; + + cameraManager.Stop(); } public override string GetSummary() diff --git a/Assets/Fungus/Scripts/Commands/PlayMusic.cs b/Assets/Fungus/Scripts/Commands/PlayMusic.cs index 3add80cd..ddd3bbda 100644 --- a/Assets/Fungus/Scripts/Commands/PlayMusic.cs +++ b/Assets/Fungus/Scripts/Commands/PlayMusic.cs @@ -30,12 +30,10 @@ namespace Fungus.Commands public override void OnEnter() { - var musicController = MusicController.GetInstance(); - if (musicController != null) - { - float startTime = Mathf.Max(0, atTime); - musicController.PlayMusic(musicClip, loop, fadeDuration, startTime); - } + var musicManager = FungusManager.Instance.MusicManager; + + float startTime = Mathf.Max(0, atTime); + musicManager.PlayMusic(musicClip, loop, fadeDuration, startTime); Continue(); } diff --git a/Assets/Fungus/Scripts/Commands/PlaySound.cs b/Assets/Fungus/Scripts/Commands/PlaySound.cs index 8478fa25..b929bb2a 100644 --- a/Assets/Fungus/Scripts/Commands/PlaySound.cs +++ b/Assets/Fungus/Scripts/Commands/PlaySound.cs @@ -39,11 +39,9 @@ namespace Fungus.Commands return; } - var musicController = MusicController.GetInstance(); - if (musicController != null) - { - musicController.PlaySound(soundClip, volume); - } + var musicManager = FungusManager.Instance.MusicManager; + + musicManager.PlaySound(soundClip, volume); if (waitUntilFinished) { diff --git a/Assets/Fungus/Scripts/Commands/PlayUsfxrSound.cs b/Assets/Fungus/Scripts/Commands/PlayUsfxrSound.cs index 53734c64..764370a2 100644 --- a/Assets/Fungus/Scripts/Commands/PlayUsfxrSound.cs +++ b/Assets/Fungus/Scripts/Commands/PlayUsfxrSound.cs @@ -56,7 +56,7 @@ using Fungus.Variables; { _synth.SetParentTransform(ParentTransform); _synth.Play(); - if (waitDuration == 0f) + if (Mathf.Approximately(waitDuration, 0f)) { Continue(); } diff --git a/Assets/Fungus/Scripts/Commands/SetAudioPitch.cs b/Assets/Fungus/Scripts/Commands/SetAudioPitch.cs index 59002c0f..1052a86e 100644 --- a/Assets/Fungus/Scripts/Commands/SetAudioPitch.cs +++ b/Assets/Fungus/Scripts/Commands/SetAudioPitch.cs @@ -36,11 +36,9 @@ namespace Fungus.Commands } }; - var musicController = MusicController.GetInstance(); - if (musicController != null) - { - musicController.SetAudioPitch(pitch, fadeDuration, onComplete); - } + var musicManager = FungusManager.Instance.MusicManager; + + musicManager.SetAudioPitch(pitch, fadeDuration, onComplete); if (!waitUntilFinished) { diff --git a/Assets/Fungus/Scripts/Commands/SetAudioVolume.cs b/Assets/Fungus/Scripts/Commands/SetAudioVolume.cs index f492481c..e36c975b 100644 --- a/Assets/Fungus/Scripts/Commands/SetAudioVolume.cs +++ b/Assets/Fungus/Scripts/Commands/SetAudioVolume.cs @@ -29,16 +29,14 @@ namespace Fungus.Commands public override void OnEnter() { - var musicController = MusicController.GetInstance(); - if (musicController != null) - { - musicController.SetAudioVolume(volume, fadeDuration, () => { - if (waitUntilFinished) - { - Continue(); - } - }); - } + var musicManager = FungusManager.Instance.MusicManager; + + musicManager.SetAudioVolume(volume, fadeDuration, () => { + if (waitUntilFinished) + { + Continue(); + } + }); if (!waitUntilFinished) { diff --git a/Assets/Fungus/Scripts/Commands/StartSwipe.cs b/Assets/Fungus/Scripts/Commands/StartSwipe.cs index cd69e6a1..3f775d8d 100644 --- a/Assets/Fungus/Scripts/Commands/StartSwipe.cs +++ b/Assets/Fungus/Scripts/Commands/StartSwipe.cs @@ -53,9 +53,9 @@ namespace Fungus.Commands return; } - var cameraController = CameraController.GetInstance(); + var cameraManager = FungusManager.Instance.CameraManager; - cameraController.StartSwipePan(targetCamera, viewA, viewB, duration, speedMultiplier, () => Continue() ); + cameraManager.StartSwipePan(targetCamera, viewA, viewB, duration, speedMultiplier, () => Continue() ); } public override string GetSummary() diff --git a/Assets/Fungus/Scripts/Commands/StopMusic.cs b/Assets/Fungus/Scripts/Commands/StopMusic.cs index 5e4abe27..59e8d9d8 100644 --- a/Assets/Fungus/Scripts/Commands/StopMusic.cs +++ b/Assets/Fungus/Scripts/Commands/StopMusic.cs @@ -18,11 +18,9 @@ namespace Fungus.Commands public override void OnEnter() { - var musicController = MusicController.GetInstance(); - if (musicController != null) - { - musicController.StopMusic(); - } + var musicManager = FungusManager.Instance.MusicManager; + + musicManager.StopMusic(); Continue(); } diff --git a/Assets/Fungus/Scripts/Commands/StopSwipe.cs b/Assets/Fungus/Scripts/Commands/StopSwipe.cs index 53863e5c..e947d751 100644 --- a/Assets/Fungus/Scripts/Commands/StopSwipe.cs +++ b/Assets/Fungus/Scripts/Commands/StopSwipe.cs @@ -18,9 +18,9 @@ namespace Fungus.Commands public override void OnEnter() { - var cameraController = CameraController.GetInstance(); + var cameraManager = FungusManager.Instance.CameraManager; - cameraController.StopSwipePan(); + cameraManager.StopSwipePan(); Continue(); } diff --git a/Assets/Fungus/Scripts/Components/CameraController.cs b/Assets/Fungus/Scripts/Components/CameraManager.cs similarity index 95% rename from Assets/Fungus/Scripts/Components/CameraController.cs rename to Assets/Fungus/Scripts/Components/CameraManager.cs index b6824157..93527250 100644 --- a/Assets/Fungus/Scripts/Components/CameraController.cs +++ b/Assets/Fungus/Scripts/Components/CameraManager.cs @@ -10,9 +10,9 @@ using Fungus; namespace Fungus { /// - /// Controller for main camera.Supports several types of camera transition including snap, pan & fade. + /// Manager for main camera. Supports several types of camera transition including snap, pan & fade. /// - public class CameraController : MonoBehaviour + public class CameraManager : MonoBehaviour { [Tooltip("Full screen texture used for screen fade effect.")] [SerializeField] protected Texture2D screenFadeTexture; @@ -51,8 +51,6 @@ namespace Fungus protected Dictionary storedViews = new Dictionary(); - protected static CameraController instance; - protected virtual void OnGUI() { if (swipePanActive) @@ -302,21 +300,6 @@ namespace Fungus #region Public members - /// - /// Returns the CameraController singleton instance. - /// Will create a CameraController game object if none currently exists. - /// - public static CameraController GetInstance() - { - if (instance == null) - { - GameObject go = new GameObject("CameraController"); - instance = go.AddComponent(); - } - - return instance; - } - /// /// Moves camera smoothly through a sequence of Views over a period of time. /// @@ -445,7 +428,7 @@ namespace Fungus swipePanActive = false; - if (duration == 0f) + if (Mathf.Approximately(duration, 0f)) { // Move immediately camera.orthographicSize = targetSize; diff --git a/Assets/Fungus/Scripts/Components/CameraController.cs.meta b/Assets/Fungus/Scripts/Components/CameraManager.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/Components/CameraController.cs.meta rename to Assets/Fungus/Scripts/Components/CameraManager.cs.meta diff --git a/Assets/Fungus/Scripts/Components/FungusManager.cs b/Assets/Fungus/Scripts/Components/FungusManager.cs new file mode 100644 index 00000000..65bc0cad --- /dev/null +++ b/Assets/Fungus/Scripts/Components/FungusManager.cs @@ -0,0 +1,78 @@ +using UnityEngine; +using System.Collections; + +namespace Fungus +{ + /// + /// Fungus manager singleton. Manages access to all Fungus singletons in a consistent manner. + /// + [RequireComponent(typeof(CameraManager))] + [RequireComponent(typeof(MusicManager))] + public sealed class FungusManager : MonoBehaviour + { + static FungusManager instance; + static bool applicationIsQuitting = false; + static object _lock = new object(); + + void Awake() + { + CameraManager = GetComponent(); + MusicManager = GetComponent(); + } + + /// + /// When Unity quits, it destroys objects in a random order. + /// In principle, a Singleton is only destroyed when application quits. + /// If any script calls Instance after it have been destroyed, + /// it will create a buggy ghost object that will stay on the Editor scene + /// even after stopping playing the Application. Really bad! + /// So, this was made to be sure we're not creating that buggy ghost object. + /// + void OnDestroy () + { + applicationIsQuitting = true; + } + + #region Public methods + + /// + /// Gets the camera manager singleton instance. + /// + public CameraManager CameraManager { get; private set; } + + /// + /// Gets the music manager singleton instance. + /// + public MusicManager MusicManager { get; private set; } + + /// + /// Gets the FungusManager singleton instance. + /// + public static FungusManager Instance + { + get + { + if (applicationIsQuitting) + { + Debug.LogWarning("FungusManager.Instance() was called while application is quitting. Returning null instead."); + return null; + } + + lock (_lock) + { + if (instance == null) + { + var go = new GameObject(); + go.name = "FungusManager"; + DontDestroyOnLoad(go); + instance = go.AddComponent(); + } + + return instance; + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Components/FungusManager.cs.meta b/Assets/Fungus/Scripts/Components/FungusManager.cs.meta new file mode 100644 index 00000000..d643e8ac --- /dev/null +++ b/Assets/Fungus/Scripts/Components/FungusManager.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: fa59a268bb22b4646bd9a89fa66582b1 +timeCreated: 1475164406 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Fungus/Scripts/Components/MusicController.cs b/Assets/Fungus/Scripts/Components/MusicManager.cs similarity index 88% rename from Assets/Fungus/Scripts/Components/MusicController.cs rename to Assets/Fungus/Scripts/Components/MusicManager.cs index 8f0c655b..214fad29 100644 --- a/Assets/Fungus/Scripts/Components/MusicController.cs +++ b/Assets/Fungus/Scripts/Components/MusicManager.cs @@ -10,10 +10,8 @@ namespace Fungus /// Music playback persists across scene loads. /// [RequireComponent(typeof(AudioSource))] - public class MusicController : MonoBehaviour + public class MusicManager : MonoBehaviour { - protected static MusicController instance; - protected virtual void Start() { GetComponent().playOnAwake = false; @@ -22,22 +20,6 @@ namespace Fungus #region Public members - /// - /// Returns the MusicController singleton instance. - /// Will create a MusicController game object if none currently exists. - /// - public static MusicController GetInstance() - { - if (instance == null) - { - GameObject go = new GameObject("MusicController"); - DontDestroyOnLoad(go); - instance = go.AddComponent(); - } - - return instance; - } - /// /// Plays game music using an audio clip. /// One music clip may be played at a time. @@ -50,7 +32,7 @@ namespace Fungus return; } - if (fadeDuration == 0f) + if (Mathf.Approximately(fadeDuration, 0f)) { audioSource.clip = musicClip; audioSource.loop = loop; @@ -96,7 +78,7 @@ namespace Fungus { AudioSource audio = GetComponent(); - if (duration == 0f) + if (Mathf.Approximately(duration, 0f)) { audio.pitch = pitch; if (onComplete != null) diff --git a/Assets/Fungus/Scripts/Components/MusicController.cs.meta b/Assets/Fungus/Scripts/Components/MusicManager.cs.meta similarity index 100% rename from Assets/Fungus/Scripts/Components/MusicController.cs.meta rename to Assets/Fungus/Scripts/Components/MusicManager.cs.meta diff --git a/Assets/Fungus/Scripts/Components/SayDialog.cs b/Assets/Fungus/Scripts/Components/SayDialog.cs index 8acf3704..1f015c92 100644 --- a/Assets/Fungus/Scripts/Components/SayDialog.cs +++ b/Assets/Fungus/Scripts/Components/SayDialog.cs @@ -144,7 +144,7 @@ namespace Fungus targetAlpha = 1f; fadeCoolDownTimer = 0.1f; } - else if (fadeWhenDone && fadeCoolDownTimer == 0f) + else if (fadeWhenDone && Mathf.Approximately(fadeCoolDownTimer, 0f)) { targetAlpha = 0f; } diff --git a/Assets/Fungus/Scripts/Components/SpriteFader.cs b/Assets/Fungus/Scripts/Components/SpriteFader.cs index 7d7eb119..f43fff83 100644 --- a/Assets/Fungus/Scripts/Components/SpriteFader.cs +++ b/Assets/Fungus/Scripts/Components/SpriteFader.cs @@ -96,7 +96,7 @@ namespace Fungus } // Early out if duration is zero - if (duration == 0f) + if (Mathf.Approximately(duration, 0f)) { spriteRenderer.color = targetColor; if (onComplete != null) diff --git a/Assets/Fungus/Scripts/Components/Writer.cs b/Assets/Fungus/Scripts/Components/Writer.cs index 4ece9b15..e26baba2 100644 --- a/Assets/Fungus/Scripts/Components/Writer.cs +++ b/Assets/Fungus/Scripts/Components/Writer.cs @@ -665,11 +665,12 @@ namespace Fungus protected virtual void Flash(float duration) { - var cameraController = CameraController.GetInstance(); - cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32); - cameraController.Fade(1f, duration, delegate { - cameraController.ScreenFadeTexture = CameraController.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32); - cameraController.Fade(0f, duration, null); + var cameraManager = FungusManager.Instance.CameraManager; + + cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32); + cameraManager.Fade(1f, duration, delegate { + cameraManager.ScreenFadeTexture = CameraManager.CreateColorTexture(new Color(1f,1f,1f,1f), 32, 32); + cameraManager.Fade(0f, duration, null); }); }