Browse Source

Merge pull request #540 from snozbot/master-singleton

Convert singletons to use a single FungusManager singleton
master
Chris Gregan 8 years ago committed by GitHub
parent
commit
a3c09c2cab
  1. 2
      Assets/Fungus/Scripts/Commands/CallMethod.cs
  2. 8
      Assets/Fungus/Scripts/Commands/FadeScreen.cs
  3. 12
      Assets/Fungus/Scripts/Commands/FadeToView.cs
  4. 6
      Assets/Fungus/Scripts/Commands/FadeUI.cs
  5. 8
      Assets/Fungus/Scripts/Commands/MoveToView.cs
  6. 10
      Assets/Fungus/Scripts/Commands/PlayMusic.cs
  7. 8
      Assets/Fungus/Scripts/Commands/PlaySound.cs
  8. 2
      Assets/Fungus/Scripts/Commands/PlayUsfxrSound.cs
  9. 8
      Assets/Fungus/Scripts/Commands/SetAudioPitch.cs
  10. 18
      Assets/Fungus/Scripts/Commands/SetAudioVolume.cs
  11. 4
      Assets/Fungus/Scripts/Commands/StartSwipe.cs
  12. 8
      Assets/Fungus/Scripts/Commands/StopMusic.cs
  13. 4
      Assets/Fungus/Scripts/Commands/StopSwipe.cs
  14. 23
      Assets/Fungus/Scripts/Components/CameraManager.cs
  15. 0
      Assets/Fungus/Scripts/Components/CameraManager.cs.meta
  16. 78
      Assets/Fungus/Scripts/Components/FungusManager.cs
  17. 12
      Assets/Fungus/Scripts/Components/FungusManager.cs.meta
  18. 24
      Assets/Fungus/Scripts/Components/MusicManager.cs
  19. 0
      Assets/Fungus/Scripts/Components/MusicManager.cs.meta
  20. 2
      Assets/Fungus/Scripts/Components/SayDialog.cs
  21. 2
      Assets/Fungus/Scripts/Components/SpriteFader.cs
  22. 11
      Assets/Fungus/Scripts/Components/Writer.cs

2
Assets/Fungus/Scripts/Commands/CallMethod.cs

@ -41,7 +41,7 @@ namespace Fungus.Commands
return;
}
if (delay == 0f)
if (Mathf.Approximately(delay, 0f))
{
CallTheMethod();
}

8
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();

12
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()

6
Assets/Fungus/Scripts/Commands/FadeUI.cs

@ -37,7 +37,7 @@ namespace Fungus.Commands
{
foreach (Image image in go.GetComponentsInChildren<Image>())
{
if (duration == 0f)
if (Mathf.Approximately(duration, 0f))
{
switch (fadeMode)
{
@ -67,7 +67,7 @@ namespace Fungus.Commands
foreach (Text text in go.GetComponentsInChildren<Text>())
{
if (duration == 0f)
if (Mathf.Approximately(duration, 0f))
{
switch (fadeMode)
{
@ -97,7 +97,7 @@ namespace Fungus.Commands
foreach (TextMesh textMesh in go.GetComponentsInChildren<TextMesh>())
{
if (duration == 0f)
if (Mathf.Approximately(duration, 0f))
{
switch (fadeMode)
{

8
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()

10
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();
}

8
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)
{

2
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();
}

8
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)
{

18
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)
{

4
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()

8
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();
}

4
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();
}

23
Assets/Fungus/Scripts/Components/CameraController.cs → Assets/Fungus/Scripts/Components/CameraManager.cs

@ -10,9 +10,9 @@ using Fungus;
namespace Fungus
{
/// <summary>
/// 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.
/// </summary>
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<string, CameraView> storedViews = new Dictionary<string, CameraView>();
protected static CameraController instance;
protected virtual void OnGUI()
{
if (swipePanActive)
@ -302,21 +300,6 @@ namespace Fungus
#region Public members
/// <summary>
/// Returns the CameraController singleton instance.
/// Will create a CameraController game object if none currently exists.
/// </summary>
public static CameraController GetInstance()
{
if (instance == null)
{
GameObject go = new GameObject("CameraController");
instance = go.AddComponent<CameraController>();
}
return instance;
}
/// <summary>
/// Moves camera smoothly through a sequence of Views over a period of time.
/// </summary>
@ -445,7 +428,7 @@ namespace Fungus
swipePanActive = false;
if (duration == 0f)
if (Mathf.Approximately(duration, 0f))
{
// Move immediately
camera.orthographicSize = targetSize;

0
Assets/Fungus/Scripts/Components/CameraController.cs.meta → Assets/Fungus/Scripts/Components/CameraManager.cs.meta

78
Assets/Fungus/Scripts/Components/FungusManager.cs

@ -0,0 +1,78 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/// <summary>
/// Fungus manager singleton. Manages access to all Fungus singletons in a consistent manner.
/// </summary>
[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<CameraManager>();
MusicManager = GetComponent<MusicManager>();
}
/// <summary>
/// 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.
/// </summary>
void OnDestroy ()
{
applicationIsQuitting = true;
}
#region Public methods
/// <summary>
/// Gets the camera manager singleton instance.
/// </summary>
public CameraManager CameraManager { get; private set; }
/// <summary>
/// Gets the music manager singleton instance.
/// </summary>
public MusicManager MusicManager { get; private set; }
/// <summary>
/// Gets the FungusManager singleton instance.
/// </summary>
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<FungusManager>();
}
return instance;
}
}
}
#endregion
}
}

12
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:

24
Assets/Fungus/Scripts/Components/MusicController.cs → Assets/Fungus/Scripts/Components/MusicManager.cs

@ -10,10 +10,8 @@ namespace Fungus
/// Music playback persists across scene loads.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class MusicController : MonoBehaviour
public class MusicManager : MonoBehaviour
{
protected static MusicController instance;
protected virtual void Start()
{
GetComponent<AudioSource>().playOnAwake = false;
@ -22,22 +20,6 @@ namespace Fungus
#region Public members
/// <summary>
/// Returns the MusicController singleton instance.
/// Will create a MusicController game object if none currently exists.
/// </summary>
public static MusicController GetInstance()
{
if (instance == null)
{
GameObject go = new GameObject("MusicController");
DontDestroyOnLoad(go);
instance = go.AddComponent<MusicController>();
}
return instance;
}
/// <summary>
/// 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<AudioSource>();
if (duration == 0f)
if (Mathf.Approximately(duration, 0f))
{
audio.pitch = pitch;
if (onComplete != null)

0
Assets/Fungus/Scripts/Components/MusicController.cs.meta → Assets/Fungus/Scripts/Components/MusicManager.cs.meta

2
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;
}

2
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)

11
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);
});
}

Loading…
Cancel
Save