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

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

Unity 單例模式

2019-11-14 08:45:43
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 本溪市| 武隆县| 湾仔区| 寿宁县| 呼玛县| 柞水县| 郁南县| 新竹市| 澄江县| 年辖:市辖区| 玉溪市| 邛崃市| 罗平县| 关岭| 泰安市| 金门县| 泽州县| 盈江县| 衡山县| 调兵山市| 页游| 巫溪县| 乌兰浩特市| 涟水县| 略阳县| 江孜县| 北川| 双辽市| 泗阳县| 江陵县| 资溪县| 运城市| 黑山县| 礼泉县| 太康县| 山西省| 海宁市| 乐业县| 鄂托克旗| 平乐县| 白玉县|