国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Unity 單例模式

2019-11-14 09:25:38
字體:
來源:轉載
供稿:網友

1.單例模式,如下

using UnityEngine;using System.Collections;public class Singleton<T> : MonoBehaviour where T : MonoBehaviour{ /** Returns the instance of this singleton. */ PRivate static T _instance; public static T Instance { private set { _instance = value; } get { if (_instance == null) _instance = GameObject.FindObjectOfType<T>(); if (_instance == null) Debug.LogError(typeof(T) + " Instance is null"); return _instance; } } protected virtual void OnDestroy() { Instance = null; }}

2.使用 繼承于上面的代碼 例子如下(這是個配置表的類,一般鍵值轉換要弄一個單例類,互相不影響,這個右后有空再說,這邊只是舉個例子如何使用單例) 用的結構public class DataController : Singleton

public class DataController : Singleton<DataController> { public KeyValue _keyValue; public KeyValueConfigure[] keyValue; public KeyValueConfigure ChooseWhichData(int id) { return keyValue[id]; } public GameObject ChooseGeneratorPrefabs(ObstacleType name) { foreach(var o in _keyValue._list) { if(o.type == name) { return o.prefabs; } } return null; }}

3.調用 : DataController.Instance.ChooseWhichData(0);


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彝良县| 同心县| 霸州市| 瓦房店市| 汶川县| 浦东新区| 喀喇沁旗| 三河市| 依兰县| 甘肃省| 岑巩县| 铁力市| 曲阜市| 安丘市| 武穴市| 惠东县| 天气| 老河口市| 商南县| 海淀区| 德昌县| 杭锦后旗| 沂源县| 阳原县| 崇阳县| 西和县| 邮箱| 定襄县| 登封市| 弥勒县| 无为县| 汉川市| 汝州市| 肥东县| 凤台县| 新巴尔虎右旗| 青川县| 靖远县| 庄河市| 庄河市| 庄河市|