国产探花免费观看_亚洲丰满少妇自慰呻吟_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);


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 北安市| 常山县| 乌兰察布市| 会理县| 昭通市| 旬阳县| 棋牌| 会泽县| 南丹县| 乌鲁木齐县| 兴安盟| 洪泽县| 将乐县| 长岭县| 都兰县| 横峰县| 安化县| 神池县| 应城市| 临武县| 无锡市| 疏附县| 迭部县| 娄底市| 怀集县| 内乡县| 凤冈县| 台中县| 右玉县| 武隆县| 洛阳市| 永德县| 华宁县| 民权县| 泊头市| 新田县| 齐河县| 醴陵市| 阳城县| 樟树市| 江门市|