Skip to content

Instantly share code, notes, and snippets.

@jessefreeman
Created July 22, 2015 01:06
Show Gist options
  • Save jessefreeman/2d7684629d6bf44e924d to your computer and use it in GitHub Desktop.
Save jessefreeman/2d7684629d6bf44e924d to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class GameObjectUtil
{
public static string singletonContainer = "SingletonContainer";
private static GameObject poolCollection
{
get {
if (_poolCollection == null)
{
_poolCollection = new GameObject("PoolCollection");
}
return _poolCollection;
}
}
private static GameObject _poolCollection;
private static Dictionary<RecycleGameObject, ObjectPool> pools = new Dictionary<RecycleGameObject, ObjectPool>();
private static Dictionary<Type, MonoBehaviour> singletonInstances = new Dictionary<Type, MonoBehaviour>();
public static GameObject Instantiate(GameObject prefab, Vector3 pos, bool restart = true)
{
GameObject instance = null;
var recycleScript = prefab.GetComponent<RecycleGameObject>();
if (recycleScript != null)
{
var pool = GetObjectPool(recycleScript);
instance = pool.NextObject(pos, restart).gameObject;
instance.transform.parent = pool.gameObject.transform;
}
else
{
instance = GameObject.Instantiate(prefab) as GameObject;
instance.transform.position = pos;
}
return instance;
}
public static void Destroy(GameObject gameObject)
{
var recycleGameObject = gameObject.GetComponent<RecycleGameObject>();
if (recycleGameObject != null)
{
recycleGameObject.Shutdown();
}
else
{
GameObject.Destroy(gameObject);
}
}
public static void ClearPools()
{
pools.Clear();
}
private static ObjectPool GetObjectPool(RecycleGameObject reference)
{
ObjectPool pool = null;
if (pools.ContainsKey(reference))
pool = pools[reference];
else
{
var poolContainer = new GameObject(reference.gameObject.name + "ObjectPool");
pool = poolContainer.AddComponent<ObjectPool>();
pool.transform.SetParent(poolCollection.transform);
pool.prefab = reference;
pools.Add(reference, pool);
}
return pool;
}
private static T CreateSingleton<T>() where T : MonoBehaviour
{
var type = typeof(T);
// Debug.Log("Creating Singleton " + type);
if (!HasSingleton<T>())
{
var singletonsContainer = GameObject.Find(singletonContainer);
if (singletonsContainer == null)
singletonsContainer = new GameObject(singletonContainer);
var script = singletonsContainer.AddComponent<T>();
singletonInstances.Add(type, script);
}
return singletonInstances[type] as T;
}
public static void RegisterSingleton<T>(MonoBehaviour instance, bool destroyDouplicate = true) where T : MonoBehaviour
{
var type = typeof(T);
// Check to see if it will override the existing instance
if (destroyDouplicate)
DeleteSingleton<T>();
// Create the instance
if (!HasSingleton<T>())
singletonInstances.Add(type, instance);
else
{
throw new Exception("Instance of "+type+" already exists");
}
}
public static T GetSingleton<T>() where T : MonoBehaviour
{
var type = typeof(T);
if (!HasSingleton<T>())
CreateSingleton<T>();
return singletonInstances[type] as T;
}
public static void UnregisterSingleton<T>(MonoBehaviour instance) where T : MonoBehaviour
{
}
public static void DeleteSingleton<T>() where T : MonoBehaviour
{
var type = typeof (T);
if (HasSingleton<T>())
{
singletonInstances.Remove(type);
}
}
public static bool HasSingleton<T>() where T : MonoBehaviour
{
var type = typeof(T);
return singletonInstances.ContainsKey(type);
}
public static void CleanupSingletons()
{
singletonInstances.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment