怎么使用MonoBehaviour的单件

using UnityEngine;
using System;
using System.Collections.Generic;

public class SingletonBehaviour<S> : MonoBehaviour where S : MonoBehaviour
{
    private static S mSingleton;

    public virtual void Awake()
    {
        mSingleton = (S)(MonoBehaviour)this;
       // Debug.LogError("SingletonBehaviour<S> Awake = " + mSingleton.GetType().ToString());
    }

    public static S GetSingleton()
    {
        return mSingleton;
    }

    public static S GetInst()
    {
        return GetSingleton();
    }

    public static S GetInstance()
    {
        return GetSingleton();
    }

    public static S Instance
    {
        get
        {
            return GetSingleton();
        }
    }
}

 

比如一个类A继承MonoBehaviour,当想使用这个类的实例时,不需要去动态创建了,只需让他继承SingletonBehaviour,通过A.GetInstance()就得到了这个类的实例

posted @ 2012-03-16 11:08  渡蓝  阅读(429)  评论(0)    收藏  举报