public abstract class Singleton<T> where T : new() {
private static T _instance;
private static readonly object locker = new object();
public static T Ins {
get {
if(_instance == null) {
lock(locker) {
if(_instance == null) {
_instance = new T();
}
}
}
return _instance;
}
private set { }
}
}
继承MonoBehaviour单例
public class PathHelper : MonoBehaviour {
private static PathHelper _instance = null;
private PathHelper() { }
public static PathHelper Ins {
get {
return _instance;
}
private set {}
}
void Awake() {
if(_instance != null) {
DestroyImmediate(gameObject);
} else {
_instance = this;
}
DontDestroyOnLoad(gameObject);
}
}