国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

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

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

Unity3D 泛型單例

單例模式

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

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

手動掛載的泛型單例

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

這里的 Awake 方法是虛方法,當(dāng)有管理類繼承這個 SingletonMono 時,可以重寫 Awake 方法進(jìn)行額外的操作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SingletonMono<T> : MonoBehaviour where T : MonoBehaviour
{static T instance;  // 私有靜態(tài)實(shí)例public static T Instance { get { return instance; } }  // 公開實(shí)例屬性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();// 可以進(jìn)行額外的初始化操作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}");}
}

運(yùn)行游戲,可以看到 GameManagerDontDestroyOnLoad 場景中,可以獲取到 score 變量進(jìn)行打印。

使用單例

自動掛載的泛型單例

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

因?yàn)樗⒉恍枰趫鼍爸惺謩觿?chuàng)建游戲物體,也不會通過 Awake 方法對變量進(jìn)行賦值。

所以在獲取 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)實(shí)例// 公開實(shí)例屬性public static T Instance{get{if (instance == null){// 創(chuàng)建一個新的游戲物體GameObject obj = new GameObject();// 根據(jù)類型進(jìn)行重命名obj.name = typeof(T).ToString();// 自動掛載單例組件instance = obj.AddComponent<T>();// 不可銷毀DontDestroyOnLoad(obj);}// 返回實(shí)例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;}
}

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

自動掛載

http://aloenet.com.cn/news/27977.html

相關(guān)文章:

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