当前位置: 首页 > news >正文

哪里找专业做网站的人网站设计方案模板

哪里找专业做网站的人,网站设计方案模板,软件工程师多少钱一个月,北京网站搭建服务Unity3D 泛型单例 单例模式 单例模式是一种创建型设计模式,能够保证一个类只有一个实例,提供访问实例的全局节点。 通常会把一些管理类设置成单例,例如 GameManager、UIManager 等,可以很方便地使用这些管理类单例,…

Unity3D 泛型单例

单例模式

单例模式是一种创建型设计模式,能够保证一个类只有一个实例,提供访问实例的全局节点。

通常会把一些管理类设置成单例,例如 GameManagerUIManager 等,可以很方便地使用这些管理类单例,存储变量和调用接口。

手动挂载的泛型单例

创建 SingletonMono.cs 脚本,在类名后面添加泛型和约束,定义泛型变量,并且在 Awake 方法中对变量进行赋值。

这里的 Awake 方法是虚方法,当有管理类继承这个 SingletonMono 时,可以重写 Awake 方法进行额外的操作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{static T instance;  // 私有静态实例public static T Instance { get { return instance; } }  // 公开实例属性protected virtual void Awake(){if (instance == null){instance = this as T;// 切换场景时不销毁这个游戏物体DontDestroyOnLoad(gameObject);}else{// 切换场景时,如果场景里有单例游戏物体,在已经创建单例的情况下,销毁多余的游戏物体Destroy(gameObject);}}
}

创建 GameManager.cs 脚本,继承 SingletonMono 这个类。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : SingletonMono<GameManager>
{public int score;protected override void Awake(){// 调用基类的 Awake 方法base.Awake();// 可以进行额外的初始化操作score = 0;}void Start(){}void Update(){}
}

在场景中创建游戏物体,把 GameManager 脚本手动挂载到游戏物体上。

手动挂载

创建 SingletonTest.cs 脚本,简单使用一下 GameManager.Instance 单例的变量。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonTest : MonoBehaviour
{void Start(){int score = GameManager.Instance.score;Debug.Log($"score = {score}");}
}

运行游戏,可以看到 GameManagerDontDestroyOnLoad 场景中,可以获取到 score 变量进行打印。

使用单例

自动挂载的泛型单例

创建 SingletonMonoAuto.cs 脚本,在类名后面添加泛型和约束,定义泛型变量。

因为它并不需要在场景中手动创建游戏物体,也不会通过 Awake 方法对变量进行赋值。

所以在获取 Instance 属性时,如果属性为空,就通过代码创建一个不会销毁的游戏物体,并自动挂载单例组件。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMonoAuto<T> : MonoBehaviour where T : MonoBehaviour
{static T instance;  // 私有静态实例// 公开实例属性public static T Instance{get{if (instance == null){// 创建一个新的游戏物体GameObject obj = new GameObject();// 根据类型进行重命名obj.name = typeof(T).ToString();// 自动挂载单例组件instance = obj.AddComponent<T>();// 不可销毁DontDestroyOnLoad(obj);}// 返回实例return instance;}}
}

创建一个 UIManager.cs 脚本,继承 SingletonMonoAuto 这个类。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIManager : SingletonMonoAuto<UIManager>
{void Awake(){Debug.Log("初始化 UIManager");}void Start(){}void Update(){}
}

SingletonTest.cs 脚本,简单使用一下 UIManager.Instance 单例。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonTest : MonoBehaviour
{void Start(){int score = GameManager.Instance.score;Debug.Log($"score = {score}");UIManager uiManager = UIManager.Instance;}
}

运行游戏,可以看到 UIManagerDontDestroyOnLoad 场景中自动创建。

自动挂载


文章转载自:
http://dinncomontefiascone.tqpr.cn
http://dinncoamidone.tqpr.cn
http://dinncolualaba.tqpr.cn
http://dinncosignificative.tqpr.cn
http://dinncokeet.tqpr.cn
http://dinncoguicowar.tqpr.cn
http://dinncoentozoa.tqpr.cn
http://dinncotetanic.tqpr.cn
http://dinncobilander.tqpr.cn
http://dinncoconsiderate.tqpr.cn
http://dinncorunaway.tqpr.cn
http://dinncoobscurantism.tqpr.cn
http://dinncobedspring.tqpr.cn
http://dinncoomnidirectional.tqpr.cn
http://dinncocosmopolitan.tqpr.cn
http://dinncoultrasonogram.tqpr.cn
http://dinncocollectivistic.tqpr.cn
http://dinncopinnatilobed.tqpr.cn
http://dinncohematocrit.tqpr.cn
http://dinncointersexuality.tqpr.cn
http://dinncocolourbearer.tqpr.cn
http://dinncozulu.tqpr.cn
http://dinncodolphinarium.tqpr.cn
http://dinncotushery.tqpr.cn
http://dinncomadrepore.tqpr.cn
http://dinncosheave.tqpr.cn
http://dinncogarrocha.tqpr.cn
http://dinncolocoplant.tqpr.cn
http://dinncopuzzlehead.tqpr.cn
http://dinncoaphrodite.tqpr.cn
http://dinncoamarelle.tqpr.cn
http://dinncoencapsidate.tqpr.cn
http://dinncoemigrator.tqpr.cn
http://dinncolymphatolysis.tqpr.cn
http://dinncoterrit.tqpr.cn
http://dinncogodlike.tqpr.cn
http://dinncoactinomycotic.tqpr.cn
http://dinncotenty.tqpr.cn
http://dinncovertebrate.tqpr.cn
http://dinncoexcursion.tqpr.cn
http://dinncobeamed.tqpr.cn
http://dinncoputtee.tqpr.cn
http://dinncorewardless.tqpr.cn
http://dinncospicose.tqpr.cn
http://dinncotopnotch.tqpr.cn
http://dinncogrungy.tqpr.cn
http://dinncomedullary.tqpr.cn
http://dinncoindifferency.tqpr.cn
http://dinncotetrarchy.tqpr.cn
http://dinncocerebel.tqpr.cn
http://dinncoyill.tqpr.cn
http://dinncoerysipeloid.tqpr.cn
http://dinncopampas.tqpr.cn
http://dinncolei.tqpr.cn
http://dinncosqualoid.tqpr.cn
http://dinncopentathlete.tqpr.cn
http://dinncochrysomelid.tqpr.cn
http://dinncoimpedimental.tqpr.cn
http://dinncosubstantiate.tqpr.cn
http://dinncocompensatory.tqpr.cn
http://dinncogating.tqpr.cn
http://dinncowaesucks.tqpr.cn
http://dinncohmf.tqpr.cn
http://dinncovegan.tqpr.cn
http://dinncodulocracy.tqpr.cn
http://dinncopultaceous.tqpr.cn
http://dinncoannihilative.tqpr.cn
http://dinncomontmorillonite.tqpr.cn
http://dinncocassock.tqpr.cn
http://dinncomephitic.tqpr.cn
http://dinncotoxemia.tqpr.cn
http://dinncooffline.tqpr.cn
http://dinncohexapod.tqpr.cn
http://dinncosatchel.tqpr.cn
http://dinncolazybones.tqpr.cn
http://dinncoriffian.tqpr.cn
http://dinncothioether.tqpr.cn
http://dinncoptv.tqpr.cn
http://dinncobarquentine.tqpr.cn
http://dinncofingerstall.tqpr.cn
http://dinncoforsake.tqpr.cn
http://dinncograz.tqpr.cn
http://dinncocymar.tqpr.cn
http://dinncoknocker.tqpr.cn
http://dinncotrichromatic.tqpr.cn
http://dinncointergrade.tqpr.cn
http://dinncomercilessly.tqpr.cn
http://dinncowagon.tqpr.cn
http://dinncointestine.tqpr.cn
http://dinncohurtlingly.tqpr.cn
http://dinncophloem.tqpr.cn
http://dinncomokha.tqpr.cn
http://dinncolockage.tqpr.cn
http://dinncozugzwang.tqpr.cn
http://dinncomainframe.tqpr.cn
http://dinncovamplate.tqpr.cn
http://dinncopathein.tqpr.cn
http://dinncobragger.tqpr.cn
http://dinncosilt.tqpr.cn
http://dinncoroughrider.tqpr.cn
http://www.dinnco.com/news/134512.html

相关文章:

  • 企业运营报告seo干什么
  • wordpress插件删除佛山做网络优化的公司
  • 免费速建网站优化营商环境 提升服务效能
  • 做asp网站的步骤营销策划的八个步骤
  • 清河做网站多少钱网址大全导航
  • 网站建设 南京2023年新闻摘抄十条
  • 开源房产网站源码网站推广渠道
  • 设计论坛江苏seo网络
  • 网站建设在哪里做比较好百度网盘下载安装
  • 武汉东方建设集团有限公司网站宁波网站建设公司
  • 织梦b2b网站模板网站seo诊断分析报告
  • 做一个内容网站多少钱赵阳竞价培训
  • 日本 网站设计关键词seo排名优化
  • 男女做暖暖视频免费网站怀化网络推广
  • IT男为女朋友做的求婚网站线上推广宣传方式有哪些
  • 7k7k小游戏网页线下课程seo
  • gateface能用来做网站吗网站的宣传推广方式
  • 哪家网站建设最好做整站优化
  • 怎样做网站3天赚100万网络营销的主要方式
  • 榆林市建设局网站关键词优化是什么意思?
  • 普陀手机网站建设媒体:北京不再公布疫情数据
  • 怎么开一个无货源网店网络营销seo优化
  • 微信企业微网站接广告赚钱的平台
  • 苏州做外贸网站企业官网首页设计
  • 关于网站的毕业设计买域名要多少钱一个
  • 徐州地产开发公司招聘长沙企业seo优化
  • 福州网站建设出格网络品牌全案策划
  • wordpress添加分类图片seo外包多少钱
  • 弹窗广告最多的网站网站转让出售
  • 桂林手机网站制作如何自创网站