Skip to content

Instantly share code, notes, and snippets.

@LordNed
Created March 22, 2015 06:12
Show Gist options
  • Save LordNed/5260c381d8e9a646b12e to your computer and use it in GitHub Desktop.
Save LordNed/5260c381d8e9a646b12e to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections.Generic;
public class Pigeons : MonoBehaviour
{
public class PigeonEvent<T> : UnityEvent<T>{}
/// <summary> If true, a GameObject has been created and marked as DontDestroyOnLoad, and this component has been attached. </summary>
private static bool m_isInit = false;
/// <summary> Singleton instance of the Pigeons class. Null if Pigeons hasn't been initialized yet. </summary>
private static Pigeons m_instance;
protected static Pigeons Instance
{
get
{
Init();
return m_instance;
}
}
private int m_subscriptionCount = 0;
private bool m_applicationQuitting;
private Hashtable m_subscriberCallbacks = new Hashtable();
private void UpdateDisplayName()
{
gameObject.name = string.Format("Piegons (Subscribers: {0})", m_subscriptionCount);
}
private void OnApplicationQuit()
{
m_applicationQuitting = true;
}
private static void Init()
{
if (m_isInit)
return;
// Check to see that no one accidentally put one of these guys in the scene already.
Pigeons[] existingPigeons = FindObjectsOfType<Pigeons>();
if(existingPigeons.Length > 0)
{
Debug.LogError("[Pigeons.Messenger] Found existing Pigeons component in the scene. Please do not put one in the scene! Destroying scene copy.");
foreach (Pigeons existing in existingPigeons)
Destroy(existing);
}
GameObject gO = new GameObject("Pigeons (Subscribers: 0)");
m_instance = gO.AddComponent<Pigeons>();
m_isInit = true;
DontDestroyOnLoad(gO);
Debug.Log("[Pigeons.Messenger] An instance of Pigeons was needed in the scene, so it was automatically created.");
}
private void SubscribeInternal<T>(UnityAction<T> messageCallback)
{
if (m_applicationQuitting)
return;
if (!m_subscriberCallbacks.ContainsKey(typeof(T)))
m_subscriberCallbacks[typeof(T)] = new PigeonEvent<T>();
((PigeonEvent<T>)m_subscriberCallbacks[typeof(T)]).AddListener(messageCallback);
m_subscriptionCount++;
UpdateDisplayName();
}
private void UnsubscribeInternal<T>(UnityAction<T> messageCallback)
{
if (m_applicationQuitting)
return;
if (!m_subscriberCallbacks.ContainsKey(typeof(T)))
{
Debug.LogWarning(string.Format("Pigeons.Messenger] Attempted to unsubscribe type {0} but no subscriptions found."));
return;
}
((PigeonEvent<T>)m_subscriberCallbacks[typeof(T)]).RemoveListener(messageCallback);
m_subscriptionCount--;
UpdateDisplayName();
}
private void SendInternal<T>(T message)
{
if (m_applicationQuitting)
return;
((PigeonEvent<T>)m_subscriberCallbacks[typeof(T)]).Invoke(message);
}
#region Public Static API
public static void Subscribe<T>(UnityAction<T> messageCallback)
{
Instance.SubscribeInternal<T>(messageCallback);
}
public static void Unsubscribe<T>(UnityAction<T> messageCallback)
{
Instance.UnsubscribeInternal<T>(messageCallback);
}
public static void Send<T>(T message)
{
Instance.SendInternal<T>(message);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment