Pull up singleton behaviour.
This commit is contained in:
25
Assets/Cryville/Common/Unity/SingletonBehaviour.cs
Normal file
25
Assets/Cryville/Common/Unity/SingletonBehaviour.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Cryville.Common.Unity {
|
||||||
|
public abstract class SingletonBehaviour<TSelf> : MonoBehaviour where TSelf : SingletonBehaviour<TSelf> {
|
||||||
|
static TSelf s_instance;
|
||||||
|
public static TSelf Instance {
|
||||||
|
get {
|
||||||
|
return s_instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool _validInstance;
|
||||||
|
protected virtual void Awake() {
|
||||||
|
if (s_instance != null) {
|
||||||
|
Debug.LogErrorFormat("Duplicate singleton behaviour {0}", typeof(TSelf));
|
||||||
|
Destroy(gameObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
s_instance = (TSelf)this;
|
||||||
|
_validInstance = true;
|
||||||
|
}
|
||||||
|
protected virtual void OnDestroy() {
|
||||||
|
if (_validInstance) s_instance = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Cryville/Common/Unity/SingletonBehaviour.cs.meta
Normal file
11
Assets/Cryville/Common/Unity/SingletonBehaviour.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9dd02eadcf1c6e745939a75a797c9093
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
|
using Cryville.Common.Unity;
|
||||||
using Cryville.Crtr.Browsing.Actions;
|
using Cryville.Crtr.Browsing.Actions;
|
||||||
using Cryville.Crtr.Config.UI;
|
using Cryville.Crtr.Config.UI;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Cryville.Crtr.Browsing.UI {
|
namespace Cryville.Crtr.Browsing.UI {
|
||||||
public class ResourceBrowserMaster : MonoBehaviour {
|
public class ResourceBrowserMaster : SingletonBehaviour<ResourceBrowserMaster> {
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
ConfigPanelMaster m_configPanel;
|
ConfigPanelMaster m_configPanel;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
@@ -21,7 +22,7 @@ namespace Cryville.Crtr.Browsing.UI {
|
|||||||
|
|
||||||
public ActionManager Actions { get; private set; }
|
public ActionManager Actions { get; private set; }
|
||||||
|
|
||||||
void Awake() {
|
protected override void Awake() {
|
||||||
Actions = new ActionManager();
|
Actions = new ActionManager();
|
||||||
Actions.Register(new PlayChartAction());
|
Actions.Register(new PlayChartAction());
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,11 @@
|
|||||||
using Discord;
|
using Discord;
|
||||||
using System;
|
using System;
|
||||||
#endif
|
#endif
|
||||||
using UnityEngine;
|
using Cryville.Common.Unity;
|
||||||
|
|
||||||
namespace Cryville.Crtr {
|
namespace Cryville.Crtr {
|
||||||
internal class DiscordController : MonoBehaviour {
|
internal class DiscordController : SingletonBehaviour<DiscordController> {
|
||||||
#if COMPILE
|
#if COMPILE
|
||||||
public static DiscordController Instance;
|
|
||||||
|
|
||||||
const long CLIENT_ID = 1059021675578007622L;
|
const long CLIENT_ID = 1059021675578007622L;
|
||||||
|
|
||||||
Discord.Discord dc;
|
Discord.Discord dc;
|
||||||
@@ -20,7 +18,6 @@ namespace Cryville.Crtr {
|
|||||||
long launchTime;
|
long launchTime;
|
||||||
|
|
||||||
void Start() {
|
void Start() {
|
||||||
Instance = this;
|
|
||||||
launchTime = (long)(DateTime.UtcNow - DateTime.UnixEpoch).TotalSeconds;
|
launchTime = (long)(DateTime.UtcNow - DateTime.UnixEpoch).TotalSeconds;
|
||||||
try {
|
try {
|
||||||
dc = new Discord.Discord(CLIENT_ID, (UInt64)CreateFlags.NoRequireDiscord);
|
dc = new Discord.Discord(CLIENT_ID, (UInt64)CreateFlags.NoRequireDiscord);
|
||||||
|
|||||||
@@ -1,24 +1,13 @@
|
|||||||
using System;
|
using Cryville.Common.Unity;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Cryville.Crtr.UI {
|
namespace Cryville.Crtr.UI {
|
||||||
internal class Master : MonoBehaviour {
|
internal class Master : SingletonBehaviour<Master> {
|
||||||
static Master s_instance;
|
|
||||||
public static Master Instance { get { return s_instance; } }
|
|
||||||
|
|
||||||
#pragma warning disable IDE0044
|
#pragma warning disable IDE0044
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private GameObject m_menu;
|
private GameObject m_menu;
|
||||||
#pragma warning restore IDE0044
|
#pragma warning restore IDE0044
|
||||||
|
|
||||||
void Awake() {
|
|
||||||
if (s_instance != null) {
|
|
||||||
Destroy(gameObject);
|
|
||||||
throw new InvalidOperationException("Attempted to initialize a singleton twice.");
|
|
||||||
}
|
|
||||||
s_instance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal void ShowMenu() { m_menu.SetActive(true); }
|
internal void ShowMenu() { m_menu.SetActive(true); }
|
||||||
internal void HideMenu() { m_menu.SetActive(false); }
|
internal void HideMenu() { m_menu.SetActive(false); }
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using Cryville.Common.Unity;
|
||||||
using Cryville.Common.Unity.UI;
|
using Cryville.Common.Unity.UI;
|
||||||
using Cryville.Crtr.Browsing.UI;
|
using Cryville.Crtr.Browsing.UI;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@@ -6,7 +7,7 @@ using UnityEngine;
|
|||||||
using unity = UnityEngine;
|
using unity = UnityEngine;
|
||||||
|
|
||||||
namespace Cryville.Crtr.UI {
|
namespace Cryville.Crtr.UI {
|
||||||
public class Menu : MonoBehaviour {
|
internal class Menu : SingletonBehaviour<Menu> {
|
||||||
#pragma warning disable IDE0044
|
#pragma warning disable IDE0044
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
ResourceBrowserMaster m_browserMaster;
|
ResourceBrowserMaster m_browserMaster;
|
||||||
@@ -24,7 +25,7 @@ namespace Cryville.Crtr.UI {
|
|||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
int totalTasks = 0;
|
int totalTasks = 0;
|
||||||
#pragma warning disable IDE0051
|
#pragma warning disable IDE0051
|
||||||
void Awake() {
|
protected override void Awake() {
|
||||||
Game.Init();
|
Game.Init();
|
||||||
m_contents.SetActive(true);
|
m_contents.SetActive(true);
|
||||||
PushTitle("Chart Browser");
|
PushTitle("Chart Browser");
|
||||||
|
|||||||
@@ -1,23 +1,11 @@
|
|||||||
using System;
|
using Cryville.Common.Unity;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UI;
|
|
||||||
|
|
||||||
namespace Cryville.Crtr.UI {
|
namespace Cryville.Crtr.UI {
|
||||||
public class PopupManager : MonoBehaviour {
|
public class PopupManager : SingletonBehaviour<PopupManager> {
|
||||||
static PopupManager s_instance;
|
|
||||||
public static PopupManager Instance { get { return s_instance; } }
|
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
GameObject m_popupPrefab;
|
GameObject m_popupPrefab;
|
||||||
|
|
||||||
void Awake() {
|
|
||||||
if (s_instance != null) {
|
|
||||||
Destroy(gameObject);
|
|
||||||
throw new InvalidOperationException("Attempted to initialize a singleton twice.");
|
|
||||||
}
|
|
||||||
s_instance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Create(string msg) {
|
public void Create(string msg) {
|
||||||
Instantiate(m_popupPrefab, transform, false).GetComponent<Popup>().Message = msg;
|
Instantiate(m_popupPrefab, transform, false).GetComponent<Popup>().Message = msg;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user