Unity常用设计模式之单例模式
来源:互联网 发布:淘宝衣服没有水洗标 编辑:程序博客网 时间:2024/05/08 15:05
单例模式:
单例就是游戏从始至终存在的唯一的实例,只需要生成一次,并且到游戏结束才需要销毁。
C#中的单例模式
单例模式的实现通俗来讲就是通过在本类中创建一个自己的实例来实现。在unity当中,如果不需要使用到monobeheviour的话,可以使用这种方式来构建单例。
转载:http://blog.csdn.net/yupu56/article/details/53668688
namespace SingleTest{ public sealed class SingleCla { private static volatile SingleCla instance; private static object syncRoot = new object(); public static SingleCla Instance { //只读 get { //判断instance是不是空 是空就穿件一个实例 if (instance == null) { lock (syncRoot) { if (instance == null) { instance = new SingleCla(); } } } return instance; } } }}
unity中的单例模式
MonoBeheviour和一般的类有几个重要区别,体现在单例模式上有两点。
第一,MonoBehaviour不能使用构造函数进行实例化,只能挂载在GameObject上。
第二,当切换场景时,当前场景中的GameObject都会被销毁(LoadLevel带有additional参数时除外),这种情况下,我们的单例对象也会被销毁。 为了使之不被销毁,我们需要进行DontDestroyOnLoad的处理。同时,为了保持场景当中只有一个实例,我们要对当前场景中的单例进行判断,如果存在其他的实例,则应该将其全部删除。
第一,MonoBehaviour不能使用构造函数进行实例化,只能挂载在GameObject上。
第二,当切换场景时,当前场景中的GameObject都会被销毁(LoadLevel带有additional参数时除外),这种情况下,我们的单例对象也会被销毁。 为了使之不被销毁,我们需要进行DontDestroyOnLoad的处理。同时,为了保持场景当中只有一个实例,我们要对当前场景中的单例进行判断,如果存在其他的实例,则应该将其全部删除。
using UnityEngine;using System.Collections;public class SingleModle : MonoBehaviour { private static volatile SingleModle instance; private static object syncRoot = new object(); public static SingleModle Instance { get { if (instance == null) { lock (syncRoot) { if (instance == null) { SingleModle[] instance = FindObjectsOfType<SingleModle>(); if (instance != null) { for (var i = 0; i < instance.Length; i++) { Destroy(instance[i].gameObject); } } } GameObject go = new GameObject("_SingleMode"); instance = go.AddComponent<SingleModle>(); DontDestroyOnLoad(go); } } return instance; } }// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}如果工程内存在多个单例就需要复制粘贴这些代码。为了避免复制粘贴可使用模版类:
using UnityEngine;using System.Collections;public sealed class SingleModle<T> : MonoBehaviour where T : MonoBehaviour{ private static volatile T instance; private static object syncRoot = new object(); public static T Instance { get { if (instance == null) { lock (syncRoot) if (instance == null) { T[] instance1 = FindObjectsOfType<T>(); if (instance1 != null) { for (var i = 0; i < instance1.Length; i++) { Destroy(instance1[i].gameObject); } } } GameObject go = new GameObject(typeof(T).Name); instance = go.AddComponent<T>(); DontDestroyOnLoad(go); } return instance; } }}
转载:http://blog.csdn.net/yupu56/article/details/53668688
0 0
- Unity常用设计模式之单例模式
- unity 设计模式之单例模式
- 常用设计模式之单例模式
- 常用设计模式之单例模式
- Unity常用的设计模式_单例模式
- unity开发常用设计模式--单例模式
- 【设计模式】常用Java设计模式之单例模式
- 【unity】Unity3d设计模式之单例模式
- Unity的设计模式--单例模式,
- 设计模式---unity中的单例模式
- Java常用设计模式之单例模式(Singleton)
- IOS常用设计模式之---单例模式
- Java中常用的设计模式之单例模式
- java 常用设计模式------单例模式
- 常用的设计模式----单例模式
- 结合Unity浅谈设计模式-单例
- 设计模式之 单例设计模式
- 设计模式之 单例设计模式
- JSON.stringify 语法实例讲解
- HashMap实现原理
- NOI.openjudge 2.1 算数枚举 7647 easy的余数相同问题
- GPU编程语言选择(OpenCL、CUDA 与C++ AMP)
- RJ45
- Unity常用设计模式之单例模式
- C.1 Configuring and compiling the GNU C Library
- CSDN-markdown编辑器的使用
- 解压tar.zx命令
- 返回两数组的交
- QT C++ 与 qml 通信
- EX6 笔记 1
- 对Android系统权限的认识(包含如何获得root权限思路)
- linux redHat 安装jdk