unity学习——单例模式
来源:互联网 发布:服装设计师 知乎 编辑:程序博客网 时间:2024/06/07 09:29
顾名思义单例模式表达的是只能创建一个实例。通常用在需要统一管理某项实物的时候用到。单例模式我们是通过将构造函数设为私有来实现。
using System.Collections;using System.Collections.Generic;using UnityEngine;public class SounderManager {//不能继承MonoBehaviour private static SounderManager instance;//设置一个私有的静态的SounderManager对象 private SounderManager()//设置私有构造方法 { } public static SounderManager GetInstance()//使用公共静态方法获取唯一的实例 { //如果实例为null说明首次创建,如果不是首次创建,我们直接返回我们之前创建的SoundManager实例instance if (instance == null) instance = new SounderManager(); return instance; } public void Play() { Debug.Log("Play"); }}
当我们在其他类中直接new SoundManager时,vs会直接报错
所以我们不能通过new来 创建对象!!!
using System.Collections;using System.Collections.Generic;using UnityEngine;public class SoundMangagerText : MonoBehaviour { // Use this for initialization void Start () { SounderManager sm = SounderManager.GetInstance(); sm.Play(); } // Update is called once per frame void Update () { }}
我们需要通过公共的静态方法GetInstance来实现。
阅读全文
0 0
- unity学习——单例模式
- Unity脚本——单例模式
- 单例模式 unity
- [unity]单例模式
- unity单例模式
- Unity单例模式
- Unity 单例模式
- Unity单例模式
- Unity单例模式
- Unity脚本——单例模式(转)
- MonoSingleton——Unity中的单例模式
- 【Unity】 Unity单例模式的使用
- Unity中的单例模式
- Unity C#单例模式
- Unity C#单例模式
- Unity 单例模式总结
- 学习笔记2—单例模式
- 设计思想学习—单例模式
- Nginx日志配置
- 自定义View之绘图篇(四):baseLine和FontMetrics
- Android7.1报错
- 172. Factorial Trailing Zeroes
- LeetCode
- unity学习——单例模式
- xlwt写入单元格,xlrd读出单元格
- C#最佳工具集合:IDE、分析、自动化工具等
- Spark学习笔记2
- Loki::CyclicVisitor
- android values适配中 文件夹加载策略解析
- 侧拉菜单
- 使用geotools和jts进行GIS数据生成
- Effective Java中文第二章第2节(个人渣翻)