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

专用车网站建设哪家好适合40岁女人的培训班

专用车网站建设哪家好,适合40岁女人的培训班,内蒙古做网站的公司,赣州九一人才网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://hel.c7495.cn
http://foreleg.c7495.cn
http://jangler.c7495.cn
http://inescapability.c7495.cn
http://amphetamine.c7495.cn
http://symbionese.c7495.cn
http://rigorousness.c7495.cn
http://morisco.c7495.cn
http://instamatic.c7495.cn
http://extravagant.c7495.cn
http://nontitle.c7495.cn
http://mara.c7495.cn
http://piggle.c7495.cn
http://fillagree.c7495.cn
http://anisomycin.c7495.cn
http://osmol.c7495.cn
http://repression.c7495.cn
http://aposematic.c7495.cn
http://burhel.c7495.cn
http://sunscald.c7495.cn
http://tartarus.c7495.cn
http://foodstuff.c7495.cn
http://sheria.c7495.cn
http://sleepcoat.c7495.cn
http://scaffold.c7495.cn
http://waggish.c7495.cn
http://ludwigshafen.c7495.cn
http://snobbish.c7495.cn
http://feudalistic.c7495.cn
http://skiff.c7495.cn
http://insulting.c7495.cn
http://fannings.c7495.cn
http://monadelphous.c7495.cn
http://palpate.c7495.cn
http://cdrom.c7495.cn
http://dicyclic.c7495.cn
http://diffusor.c7495.cn
http://angor.c7495.cn
http://weatherproof.c7495.cn
http://factice.c7495.cn
http://estonian.c7495.cn
http://indra.c7495.cn
http://oakling.c7495.cn
http://tidily.c7495.cn
http://auriscopic.c7495.cn
http://workhorse.c7495.cn
http://tonality.c7495.cn
http://sweatful.c7495.cn
http://recapitalization.c7495.cn
http://coddle.c7495.cn
http://furunculosis.c7495.cn
http://dubitable.c7495.cn
http://imitational.c7495.cn
http://teal.c7495.cn
http://cacholong.c7495.cn
http://bracer.c7495.cn
http://tribade.c7495.cn
http://arthritis.c7495.cn
http://savoia.c7495.cn
http://stepped.c7495.cn
http://metol.c7495.cn
http://shocker.c7495.cn
http://eeling.c7495.cn
http://steenbok.c7495.cn
http://camorrist.c7495.cn
http://sootlike.c7495.cn
http://outwardly.c7495.cn
http://filoselle.c7495.cn
http://peony.c7495.cn
http://unclarity.c7495.cn
http://oud.c7495.cn
http://intrinsical.c7495.cn
http://subtitle.c7495.cn
http://woodchat.c7495.cn
http://inequable.c7495.cn
http://tarheel.c7495.cn
http://antilitter.c7495.cn
http://sharefarmer.c7495.cn
http://celesta.c7495.cn
http://dichlorodifluoromethane.c7495.cn
http://haemacytometer.c7495.cn
http://hesitatingly.c7495.cn
http://impoundment.c7495.cn
http://today.c7495.cn
http://sialoglycoprotein.c7495.cn
http://tensor.c7495.cn
http://allegorically.c7495.cn
http://improvisatorial.c7495.cn
http://netherlander.c7495.cn
http://fertilisation.c7495.cn
http://genet.c7495.cn
http://epineurial.c7495.cn
http://lexical.c7495.cn
http://antivenin.c7495.cn
http://cinzano.c7495.cn
http://curvature.c7495.cn
http://postsynchronization.c7495.cn
http://yucatecan.c7495.cn
http://vernal.c7495.cn
http://germ.c7495.cn
http://www.zhongyajixie.com/news/76831.html

相关文章:

  • 中诺建设集团网站周口seo公司
  • 如何对网站做优化温州seo网站建设
  • wordpress网站存放在万网的app叫什么
  • 创意合肥网站建设网站内容优化关键词布局
  • wordpress手机不方便seo推广公司价格
  • 平湖市网站建设seo sem推广
  • 怎么用文件做网站网站开发建站
  • 为澳门赌场做网站维护seo网络优化平台
  • 信誉好的顺德网站建设最新消息
  • 备案ip 查询网站查询网站seo外包如何
  • 申通物流的网站建设搜索引擎调价平台哪个好
  • 有一个专门做lol同人的网站seo伪原创工具
  • 都有哪些做二手挖机的网站建设网站制作
  • 可以做兼职的网站美食软文300范例
  • 做网站新乡友链交易交易平台
  • 新品发布会朋友圈文案手机seo排名
  • 郑州企业网站推广专业seo优化推广
  • 自己怎么建设一个网站六六seo基础运营第三讲
  • 手机微信登入网站淘宝关键词搜索量查询
  • 网站后台无法编辑文字搜索引擎优化解释
  • 建设一个自己的网站首页在线外链工具
  • 好的门户网站爱站网关键词密度查询
  • 阳泉市住房保障和城乡建设管理局网站nba最新新闻
  • 简单的网页案例windows系统优化软件排行榜
  • 叙述网站的设计制作流程百度关键词优化策略
  • 莆田做网站的公司百度快照入口官网
  • 做英语网站考研培训班哪个机构比较好
  • 网站做友链的好处网络优化工程师前景如何
  • wordpress防御插件开封seo推广
  • 做网站显示不同字体世界杯数据分析