美文网首页
Unity中的单例总结

Unity中的单例总结

作者: RE_my_world | 来源:发表于2019-03-01 15:40 被阅读0次

Unity中的单例总结

单例模式是程序员在编程时接触到的最基础设计模式之一,简单一句话概括就是对象的唯一实例。

在unity中的单例模板有以下几种

非Monobehavior下的单例

  • 使用反射的单例模式
using System;
using System.Reflection;

public abstract class Singleton<T> where T : Singleton<T>
{
  protected Singleton()
  {
    Init();
  }

  protected static T instance = null;

  public static T Instance
  {
    get
    {
      if (instance == null)
      {
        var constructors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
        var constructor = Array.Find(constructors, c => c.GetParameters().Length == 0);
        if (constructor == null)
          throw new Exception("non-public constructor not find");
        instance = constructor.Invoke(null) as T;
      }
      return instance;
    }
  }

  protected virtual void Init() { }
  
}

使用反射获取非公有 构造无参函数,并进行初始化

  • 使用关键字 new() 限定,必须含有无参构造函数的单例
using System;
using System.Reflection;

public abstract class Singleton<T> where T : Singleton<T>, new()
{
  protected Singleton()
  {
    Init();
  }

  protected static T instance = null;

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

  protected virtual void Init() { }
    
}

使用new()关键字限定无参构造函数,子类继承时必须实现公有构造函数

  • 使用双重锁的单例模式
using System;
using System.Reflection;

public abstract class Singleton<T> where T : Singleton<T>
{
  protected Singleton()
  {
    Init();
  }

  protected static T instance = null;

  private static readonly object objectLock = new object();

  public static T Instance
  {
    get
    {
      if (instance == null)
      {
        lock (objectLock)
          if (instance == null)
          {
            ...初始化过程...
          }
      }
      return instance;
    }
  }

  protected virtual void Init() { }
  
}

使用 双重锁确保单例在多线程初始化时的线程安全性

MonoBehavior的单例实现

using UnityEngine;

public class BehaviorSingleton<T> : MonoBehaviour where T : BehaviorSingleton<T>
{
  private static T instance = null;

  public static T Instance
  {
    get
    {
      if (instance == null)
      {
        instance = FindObjectOfType<T>();
        if (instance == null)
        {
          var gameObject = new GameObject(typeof(T).Name);
          DontDestroyOnLoad(gameObject);
          instance = gameObject.AddComponent<T>();
        }
      }
      return instance;
    }
  }

  private void Awake()
  {
    //确保唯一性
    if (instance == null)
    {
      DontDestroyOnLoad(gameObject);
      gameObject.name = typeof(T).Name;
      instance = GetComponent<T>();
      InstanceAwake();
    }
    else
      Destroy(gameObject);
  }


  private void OnApplicationQuit()
  {
    instance.InstanceDestroy();
    instance = null;
  }

  protected virtual void InstanceAwake() { }
  protected virtual void InstanceDestroy() { }

}

相关文章

  • Unity中的单例总结

    Unity中的单例总结 单例模式是程序员在编程时接触到的最基础设计模式之一,简单一句话概括就是对象的唯一实例。 在...

  • Unity3D-单例类Instance

    今天介绍Unity中所有使用的单例类 万能单例类 雨落随风提供单例类

  • 单例

    1.MonoBehavior单例 其实在unity中,如果脚本是继承monobehavior,那么使用起单例来更加...

  • 单例模式和Unity中单例的区别

    单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模...

  • 单例-unity

    或者定义属性Instance访问

  • Unity单例

    在游戏场景切换或者实例化代码中,我们不希望某一代码或者类在此场景中多次出现,从而导致数据的混乱或者调用混淆。另一种...

  • Android Studio 集成Unity 工程并相互调用

    新建Unity工程 unity泛型单例脚本 SingletonUnity.cs 调用Java脚本 MobPlugi...

  • C# 单例

    Unity 单例模式[https://www.jianshu.com/p/97e4758ff4b4] 单例模式在开...

  • Android 丨 单例模式

    面试过程中,单例模式总是会被问及,所以抽时间总结了一份单例相关的笔记 单例概念 单例模式是一种对象的创建模式,它用...

  • iOS开发技巧(Swift版):单例创建

    在些项目的时候, 我们往往会用到单例模式,.相比OC中单例的写法, 结合Swift的语法特点,总结一下3种单例模式...

网友评论

      本文标题:Unity中的单例总结

      本文链接:https://www.haomeiwen.com/subject/dziduqtx.html