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.Config.UI;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Crtr.Browsing.UI {
|
||||
public class ResourceBrowserMaster : MonoBehaviour {
|
||||
public class ResourceBrowserMaster : SingletonBehaviour<ResourceBrowserMaster> {
|
||||
[SerializeField]
|
||||
ConfigPanelMaster m_configPanel;
|
||||
[SerializeField]
|
||||
@@ -21,7 +22,7 @@ namespace Cryville.Crtr.Browsing.UI {
|
||||
|
||||
public ActionManager Actions { get; private set; }
|
||||
|
||||
void Awake() {
|
||||
protected override void Awake() {
|
||||
Actions = new ActionManager();
|
||||
Actions.Register(new PlayChartAction());
|
||||
|
||||
|
@@ -6,13 +6,11 @@
|
||||
using Discord;
|
||||
using System;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
using Cryville.Common.Unity;
|
||||
|
||||
namespace Cryville.Crtr {
|
||||
internal class DiscordController : MonoBehaviour {
|
||||
internal class DiscordController : SingletonBehaviour<DiscordController> {
|
||||
#if COMPILE
|
||||
public static DiscordController Instance;
|
||||
|
||||
const long CLIENT_ID = 1059021675578007622L;
|
||||
|
||||
Discord.Discord dc;
|
||||
@@ -20,7 +18,6 @@ namespace Cryville.Crtr {
|
||||
long launchTime;
|
||||
|
||||
void Start() {
|
||||
Instance = this;
|
||||
launchTime = (long)(DateTime.UtcNow - DateTime.UnixEpoch).TotalSeconds;
|
||||
try {
|
||||
dc = new Discord.Discord(CLIENT_ID, (UInt64)CreateFlags.NoRequireDiscord);
|
||||
|
@@ -1,24 +1,13 @@
|
||||
using System;
|
||||
using Cryville.Common.Unity;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Cryville.Crtr.UI {
|
||||
internal class Master : MonoBehaviour {
|
||||
static Master s_instance;
|
||||
public static Master Instance { get { return s_instance; } }
|
||||
|
||||
internal class Master : SingletonBehaviour<Master> {
|
||||
#pragma warning disable IDE0044
|
||||
[SerializeField]
|
||||
private GameObject m_menu;
|
||||
#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 HideMenu() { m_menu.SetActive(false); }
|
||||
|
||||
|
@@ -1,3 +1,4 @@
|
||||
using Cryville.Common.Unity;
|
||||
using Cryville.Common.Unity.UI;
|
||||
using Cryville.Crtr.Browsing.UI;
|
||||
using System.Collections.Generic;
|
||||
@@ -6,7 +7,7 @@ using UnityEngine;
|
||||
using unity = UnityEngine;
|
||||
|
||||
namespace Cryville.Crtr.UI {
|
||||
public class Menu : MonoBehaviour {
|
||||
internal class Menu : SingletonBehaviour<Menu> {
|
||||
#pragma warning disable IDE0044
|
||||
[SerializeField]
|
||||
ResourceBrowserMaster m_browserMaster;
|
||||
@@ -24,7 +25,7 @@ namespace Cryville.Crtr.UI {
|
||||
bool initialized = false;
|
||||
int totalTasks = 0;
|
||||
#pragma warning disable IDE0051
|
||||
void Awake() {
|
||||
protected override void Awake() {
|
||||
Game.Init();
|
||||
m_contents.SetActive(true);
|
||||
PushTitle("Chart Browser");
|
||||
|
@@ -1,23 +1,11 @@
|
||||
using System;
|
||||
using Cryville.Common.Unity;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Crtr.UI {
|
||||
public class PopupManager : MonoBehaviour {
|
||||
static PopupManager s_instance;
|
||||
public static PopupManager Instance { get { return s_instance; } }
|
||||
|
||||
public class PopupManager : SingletonBehaviour<PopupManager> {
|
||||
[SerializeField]
|
||||
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) {
|
||||
Instantiate(m_popupPrefab, transform, false).GetComponent<Popup>().Message = msg;
|
||||
}
|
||||
|
Reference in New Issue
Block a user