中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

商城網(wǎng)站源碼下載seo排名軟件有用嗎

商城網(wǎng)站源碼下載,seo排名軟件有用嗎,xp怎么做網(wǎng)站,服裝設計學院Unity3D 泛型單例 單例模式 單例模式是一種創(chuàng)建型設計模式,能夠保證一個類只有一個實例,提供訪問實例的全局節(jié)點。 通常會把一些管理類設置成單例,例如 GameManager、UIManager 等,可以很方便地使用這些管理類單例,…

Unity3D 泛型單例

單例模式

單例模式是一種創(chuàng)建型設計模式,能夠保證一個類只有一個實例,提供訪問實例的全局節(jié)點。

通常會把一些管理類設置成單例,例如 GameManagerUIManager 等,可以很方便地使用這些管理類單例,存儲變量和調(diào)用接口。

手動掛載的泛型單例

創(chuàng)建 SingletonMono.cs 腳本,在類名后面添加泛型和約束,定義泛型變量,并且在 Awake 方法中對變量進行賦值。

這里的 Awake 方法是虛方法,當有管理類繼承這個 SingletonMono 時,可以重寫 Awake 方法進行額外的操作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{static T instance;  // 私有靜態(tài)實例public static T Instance { get { return instance; } }  // 公開實例屬性protected virtual void Awake(){if (instance == null){instance = this as T;// 切換場景時不銷毀這個游戲物體DontDestroyOnLoad(gameObject);}else{// 切換場景時,如果場景里有單例游戲物體,在已經(jīng)創(chuàng)建單例的情況下,銷毀多余的游戲物體Destroy(gameObject);}}
}

創(chuàng)建 GameManager.cs 腳本,繼承 SingletonMono 這個類。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : SingletonMono<GameManager>
{public int score;protected override void Awake(){// 調(diào)用基類的 Awake 方法base.Awake();// 可以進行額外的初始化操作score = 0;}void Start(){}void Update(){}
}

在場景中創(chuàng)建游戲物體,把 GameManager 腳本手動掛載到游戲物體上。

手動掛載

創(chuàng)建 SingletonTest.cs 腳本,簡單使用一下 GameManager.Instance 單例的變量。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonTest : MonoBehaviour
{void Start(){int score = GameManager.Instance.score;Debug.Log($"score = {score}");}
}

運行游戲,可以看到 GameManagerDontDestroyOnLoad 場景中,可以獲取到 score 變量進行打印。

使用單例

自動掛載的泛型單例

創(chuàng)建 SingletonMonoAuto.cs 腳本,在類名后面添加泛型和約束,定義泛型變量。

因為它并不需要在場景中手動創(chuàng)建游戲物體,也不會通過 Awake 方法對變量進行賦值。

所以在獲取 Instance 屬性時,如果屬性為空,就通過代碼創(chuàng)建一個不會銷毀的游戲物體,并自動掛載單例組件。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMonoAuto<T> : MonoBehaviour where T : MonoBehaviour
{static T instance;  // 私有靜態(tài)實例// 公開實例屬性public static T Instance{get{if (instance == null){// 創(chuàng)建一個新的游戲物體GameObject obj = new GameObject();// 根據(jù)類型進行重命名obj.name = typeof(T).ToString();// 自動掛載單例組件instance = obj.AddComponent<T>();// 不可銷毀DontDestroyOnLoad(obj);}// 返回實例return instance;}}
}

創(chuàng)建一個 UIManager.cs 腳本,繼承 SingletonMonoAuto 這個類。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIManager : SingletonMonoAuto<UIManager>
{void Awake(){Debug.Log("初始化 UIManager");}void Start(){}void Update(){}
}

SingletonTest.cs 腳本,簡單使用一下 UIManager.Instance 單例。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonTest : MonoBehaviour
{void Start(){int score = GameManager.Instance.score;Debug.Log($"score = {score}");UIManager uiManager = UIManager.Instance;}
}

運行游戲,可以看到 UIManagerDontDestroyOnLoad 場景中自動創(chuàng)建。

自動掛載

http://www.risenshineclean.com/news/27977.html

相關文章:

  • 自學黑客編程入門優(yōu)化設計卷子答案
  • 上海服裝品牌網(wǎng)站建設seo課程培訓班費用
  • 房地產(chǎn)微網(wǎng)站模板西安關鍵詞推廣
  • 邯鄲網(wǎng)站設計申請搜索引擎排名營銷
  • 網(wǎng)站建站報價表什么軟件引流客源最快
  • 南京建行網(wǎng)站東莞seo優(yōu)化排名推廣
  • 工業(yè)網(wǎng)站素材廣告公司推廣軟文
  • 北京軟件開發(fā)公司怎么樣網(wǎng)站網(wǎng)頁的優(yōu)化方法
  • 企業(yè)信息網(wǎng)站模板網(wǎng)站策劃書怎么寫
  • 瀏覽網(wǎng)站手機響貴陽seo網(wǎng)站推廣
  • 建設一個網(wǎng)站需要的空間有哪些方法病毒式營銷方法
  • 做外匯網(wǎng)站短視頻seo詢盤獲客系統(tǒng)軟件
  • 網(wǎng)站備案回訪電話號碼全文搜索引擎有哪些
  • 珠海網(wǎng)站優(yōu)化公司百度搜索官網(wǎng)
  • 網(wǎng)站設計網(wǎng)站制作seo是指什么崗位
  • 阿里云服務器租用價格關鍵詞排名seo
  • 網(wǎng)站正在建設中 頁面渠道推廣
  • 本地網(wǎng)站建設的步驟過程企業(yè)網(wǎng)站多少錢一年
  • 南昌建設銀行網(wǎng)站網(wǎng)站頁面優(yōu)化內(nèi)容包括哪些
  • 深圳貿(mào)易網(wǎng)站開發(fā)優(yōu)化師是做什么的
  • 一級a做爰片阿v祥仔網(wǎng)站手機優(yōu)化大師哪個好
  • 怎么樣自己做網(wǎng)站接訂單seo前線
  • 山東大學網(wǎng)站設計與建設網(wǎng)站推廣的工作內(nèi)容
  • wap網(wǎng)站多少錢網(wǎng)絡優(yōu)化是干什么的
  • 網(wǎng)站制作設及的技術山西網(wǎng)絡營銷seo
  • 王串場街網(wǎng)站建設公司全網(wǎng)營銷培訓
  • 新聞網(wǎng)站建設策劃長沙網(wǎng)站推廣 下拉通推廣
  • 正規(guī)的網(wǎng)站建設seo博客網(wǎng)站
  • 網(wǎng)站建設的稅收分類編碼怎么自己創(chuàng)建一個網(wǎng)站
  • 有一套源碼做網(wǎng)站還差什么新的數(shù)據(jù)新聞