Chris Gregan
8 years ago
committed by
GitHub
22 changed files with 150 additions and 100 deletions
@ -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 |
||||
} |
||||
} |
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2 |
||||
guid: fa59a268bb22b4646bd9a89fa66582b1 |
||||
timeCreated: 1475164406 |
||||
licenseType: Free |
||||
MonoImporter: |
||||
serializedVersion: 2 |
||||
defaultReferences: [] |
||||
executionOrder: 0 |
||||
icon: {instanceID: 0} |
||||
userData: |
||||
assetBundleName: |
||||
assetBundleVariant: |
Loading…
Reference in new issue