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

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

Unity 單例模式

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

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);


上一篇:回文字符串

下一篇:1029. 舊鍵盤(20)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 腾冲县| 通山县| 阿克苏市| 沁阳市| 偏关县| 雷州市| 淅川县| 岑溪市| 临颍县| 马龙县| 水富县| 叙永县| 江陵县| 东城区| 临安市| 上蔡县| 商丘市| 辽宁省| 北流市| 临沭县| 吉林省| 丰镇市| 沽源县| 沿河| 肥城市| 栾城县| 清河县| 抚松县| 宜昌市| 中阳县| 新竹市| 襄垣县| 旬邑县| 罗源县| 九江市| 平乡县| 黑河市| 大庆市| 余干县| 新乡市| 许昌市|