Last active
March 25, 2016 14:10
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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