Unity3D C# 单例模式

using UnityEngine;
using System.Collections;

public class MyClass : MonoBehaviour
{
    private static MyClass _instance;
    public static MyClass Instance
    {
        get
        {
            if (!_instance)
            {
                _instance = GameObject.FindObjectOfType(typeof(MyClass)) as MyClass;
                if (!_instance)
                {
                    GameObject container = new GameObject();
                    container.name = "MyClass";
                    _instance = container.AddComponent(typeof(MyClass)) as MyClass;
                }
            }
            return _instance;
        }
    }
}
原文地址

你可能感兴趣的:(Unity,Unity3D,c#,单例)