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

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

Unity 單例模式

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

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 朝阳市| 台湾省| 闽侯县| 平泉县| 武冈市| 山东| 共和县| 丹江口市| 交城县| 河南省| 鄂州市| 淮滨县| 宜宾市| 石景山区| 团风县| 永昌县| 蓬安县| 仙居县| 唐海县| 毕节市| 土默特左旗| 纳雍县| 温泉县| 安丘市| 墨竹工卡县| 双峰县| 临颍县| 宜昌市| 兰溪市| 太和县| 宝丰县| 新巴尔虎右旗| 松潘县| 富顺县| 光山县| 康定县| 武定县| 六安市| 锦州市| 喀什市| 彭州市|