Skip to content

Instantly share code, notes, and snippets.

@saint-angels
Last active March 25, 2016 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saint-angels/51b4897afe93b168d063 to your computer and use it in GitHub Desktop.
Save saint-angels/51b4897afe93b168d063 to your computer and use it in GitHub Desktop.
Useful Components for Unity3D engine. You probably have a component somewhere where you only want one of them and you probably want to get access to it from everywhere for some reason. So we added SingletonComponent. from http://garry.tv/2015/06/14/unity-tips/
using UnityEngine;
public abstract class SingletonComponent<T> : SingletonComponent where T : MonoBehaviour
{
public static T Instance
{
get { return instance; }
}
private static T instance = default( T );
public override void Setup()
{
if (instance != this) instance = this as T;
}
public override void Clear()
{
if (instance == this) instance = null;
}
}
public abstract class SingletonComponent : MonoBehaviour
{
public abstract void Setup();
public abstract void Clear();
protected virtual void Awake()
{
Setup();
}
protected virtual void OnDestroy()
{
Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment