Unity组件的基类单例模式
来源:互联网 发布:淘宝卖家怎么查访客 编辑:程序博客网 时间:2024/05/20 18:04
0. 背景
单例模式非常常见,在各种语言上都能见到,用处也十分广泛,前一篇文章有讲到python的两种单例模式。当然Unity中组件的单例模式用途也很广,例如游戏中各种Manager
, 使用StartCoroutine
, GetComponent
等方法的单例,本文简要讲述一下Unity中对于组件的单例模式如何构建。
1. 相同之处
首先既然是单例模式,也就符合单例的一些既定规则:
- 构造参数为私有;
- 存在static
的实例变量名instance
;
- 存在static
的方法-Instance
or GetInstance
,以获取instance
,若第一次获取instance
则需要new
一个实例。
2. 不同之处
- 既然是Unity
的组件Component
,在new
组件实例时: 先需要new GameObject
, 然后new Component
, 并把新建的组件实例挂载到GameObject
;
- 当然这个新建的GameObject
不能持久化到Unity Editor
,也就是xxxx.unity
上不能将这个GameObejct
保存进去;
- 作为一个单例的基类其实需要使用到泛型。
3. Singleton
Post Code是最好的解释行为。
首先声明命名空间:
using UnityEngine; // Unity相关APIusing System.Collections;using System;
其次声明单例类和其static
的实例变量名instance
// 泛型T,继承自MonoBehaviour, 保证了此单例是组件的基类public class Singleton<T> : MonoBehaviour where T : MonoBehaviour{ protected static T instance; ...}
然后实现static
的方法-Instance
or GetInstance
:
/** Returns the instance of this singleton. */ public static T Instance //使用c#的get,set Property的写法 { get { if(instance == null){ //第一次调用Singleton<T>.Instance时instance==null if(instantiated) return null; instance = (T) FindObjectOfType(typeof(T)); // 寻找object,找到后转为Component if (instance == null){ //如果没有找到 GameObject go = new GameObject();//新建GameObject go.name = typeof(T).ToString(); instance = go.AddComponent<T>();//把T挂载到go上, instantiated = true; //初始化成功 } } //Destroy instance if the singleton persists into the editor if (Application.isEditor && !Application.isPlaying){ //持久化之前消除该GameObject Destroy(instance); instance = null; } return instance; } }
4. Program
使用Singleton请看如下代码:
//新建一个HttpRequest单例class HttpRequest : Singleton<HttpRequest>{ // Get请求 public WWW GET(string url) { WWW www = new WWW (url); StartCoroutine (WaitForRequest (www)); // 由于是继承自MonoBehaviour,所以可以调用协程函数StartCoroutine while (!www.isDone) {} return www; } // Post请求 public WWW POST(string url, Dictionary<string,string> post) { WWWForm form = new WWWForm(); foreach(KeyValuePair<String,String> post_arg in post) { form.AddField(post_arg.Key, post_arg.Value);//添加表单字段数据 } WWW www = new WWW(url, form); StartCoroutine(WaitForRequest(www)); while (!www.isDone) {} return www; } //枚举器,yield关键字作用 private IEnumerator WaitForRequest(WWW www) { yield return www; // check for errors if (www.error == null) { Debug.Log("WWW Ok!: " + www.text); } else { Debug.Log("WWW Error: "+ www.error); } }}
5. 总结
上述单例介绍简单易用,欢迎大家指正交流。cs脚本文件,请移步csdz的gitbub下载。
1 0
- Unity组件的基类单例模式
- Unity的camera组件
- unity的LOD组件
- unity anim 组件的使用
- Unity 组件的排序问题
- unity network组件的使用
- Unity的 UNet组件介绍
- unity的canvas scaler组件
- Unity-Rigidbody【刚体】组件-Rigidbody.AddForce的ForceMode 力的模式
- Unity客户端框架笔记二(组件实体开发模式的思考)
- VMware的Unity模式
- Unity 组件
- 【Unity】 Unity单例模式的使用
- [Unity]Unity组件崩溃的通用的几种解决办法
- [Unity]Unity 安装的官方标准组件的说明
- unity 的游戏脚本组件的生命周期
- 程序的组件模式
- 程序的组件模式
- Android 的SQLite
- mysql 数据类型选择
- MongoDB shell脚本 自动添加权限与账号密码
- [IO]——打印流
- iOS数据持久化 偏好设置
- Unity组件的基类单例模式
- mysql 时间类型小结
- ActiveX开发流程
- 第七周上机实践项目-项目一-线段类-成员函数
- 【HDU 1555】【水题】How many days?
- Oracle数据库--实用操作(3) PL/SQL
- 求两个整数之间的汉明距离---Salem
- 【综述】3D智能数字化与3D打印:"中国制造"向"中国智造"转变的机遇
- 蓝桥杯第六题,也是全排列。