using UnityEngine;
using System.Collections.Generic;
public class QSingleton<T> where T :class, new ()
{
public static T _instance;
// public static readonly object syslock = new object();
public QSingleton()
{
}
public static T getInstance()
{
//if (_instance == null)
//{
//lock (syslock)
//{
if (_instance == null)
{
_instance = new T();
}
//}
// }
return _instance;
}
using System.Collections.Generic;
public class QSingleton<T> where T :class, new ()
{
public static T _instance;
// public static readonly object syslock = new object();
public QSingleton()
{
}
public static T getInstance()
{
//if (_instance == null)
//{
//lock (syslock)
//{
if (_instance == null)
{
_instance = new T();
}
//}
// }
return _instance;
}
}
using UnityEngine;
using System.Collections.Generic;
public class SingClass : QSingleton<SingClass>
{
public int a = 1;
// Use this for initialization
public void St () {
Debug.Log(12);
Debug.Log(a);
}
}
using UnityEngine;
using System.Collections;
public class Script : MonoBehaviour {
void Start () {
SingClass.getInstance().St();
// Debug.Log(SingClass._instance.a);
}
}
单列模式http://cantellow.iteye.com/blog/838473