Unity中的单例方法2

  前沿:

    在最近的一次复习中,我突然发现游戏项目中的单例方法其实大概就3种情况。

      1.不继承自MonoBehaviour的单例。

      2.继承自MonoBehaviour的,并且会随着场景切换而销毁的单例。

      3.继承自MonoBehaviour的,并且不会随着场景切换而销毁的单例。

    所以,今天我将重新的归纳游戏中的单例方法的写法。

  一、不继承自MonoBehaviour的单例。


#region 模块信息
// **********************************************************************
// Copyright (C) 2018 The company name
//
// 文件名(File Name):             Singleton.cs
// 作者(Author):                  Dean1874
// 创建时间(CreateTime):          2018-03-20 15:38:13
// 修改者列表(modifier):
// 模块描述(Module description):
// 
// **********************************************************************
#endregion

using UnityEngine;
using System;

public abstract class Singleton<T> : IDisposable where T : class, new()
{
    protected static T _instance = null;

    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = new T();
            }
            return _instance;
        }
    }

    /// <summary>
    /// 实现的接口 --- Dispose表示将当前资源关闭
    /// </summary>
    public virtual void Dispose()
    {
        
    }
}


 

  二、继承自MonoBehaviour的,并且会随着场景切换而销毁的单例。

#region 模块信息
// **********************************************************************
// Copyright (C) 2018 The company name
//
// 文件名(File Name):             MonoSingleton.cs
// 作者(Author):                  Dean1874
// 创建时间(CreateTime):          2018-03-20 15:30:26
// 修改者列表(modifier):
// 模块描述(Module description):  继承自MonoBehaviour的泛型单例
// 
// **********************************************************************
#endregion

using UnityEngine;

public abstract class MonoSingleton<T> : MonoBehaviour where T  : MonoSingleton<T>
{
    private static T _instance = null;

    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType(typeof(T)) as T;

            }
            return _instance;
        }
    }

}

  三、继承自MonoBehaviour的,并且不会随着场景切换而销毁的单例。

#region 模块信息
// **********************************************************************
// Copyright (C) 2018 The company name
//
// 文件名(File Name):             DontDesMonoSingleton.cs
// 作者(Author):                  Dean1874
// 创建时间(CreateTime):          2018-03-20 15:42:31
// 修改者列表(modifier):
// 模块描述(Module description):
// 
// **********************************************************************
#endregion

using UnityEngine;

public abstract class DontDesMonoSingleton<T> : MonoBehaviour  where T : DontDesMonoSingleton<T>
{
    protected static T _instance = null;

    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                GameObject go = GameObject.Find("DontDesMonoSingleton");
                if (go == null)
                {
                    go = new GameObject("DontDesMonoSingleton");

                    DontDestroyOnLoad(go);
                }

                _instance = go.GetComponent<T>();

                if (_instance == null)
                {
                    _instance = go.AddComponent<T>();
                }
            }

            return _instance;
        }
    }
}

  总结:以上就是三种单例方法的泛型的写法,如何运用,在这里我就不进行实际操作了,使用起来是很简单的。

原文地址:https://www.cnblogs.com/Dean27/p/8609838.html