?????個(gè)人主頁:@元宇宙-秩沅
????? hallo 歡迎 點(diǎn)贊?? 收藏? 留言?? 加關(guān)注?!
????? 本文由 秩沅 原創(chuàng)
????? 收錄于專欄:unityUI專題篇
?單例模式管理面板對(duì)象?
??前言
???
??(A)常用關(guān)鍵API
??(B)需求分析
??(C)開始場(chǎng)景制作
- API:簡(jiǎn)單的旋轉(zhuǎn)
??(D)邏輯封裝——面板基類
解析:
1.采用單例模式,原因: 單例模式無法被纂改基類,適合當(dāng)管理者
2.基類提取了子類所有的共性: 即面板的顯示和隱藏
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
// 項(xiàng)目:
// 功能: 開始面板的基類(單例模式)
// 創(chuàng)建者:秩沅
//_____________________________________
//-------------------------------------
public class BeginFather<T>: MonoBehaviour where T : class
{
static private T planeFather;
static public T PlaneFather => planeFather;
private void Awake()
{
planeFather = this as T ; //到時(shí)被繼承時(shí)的類型會(huì)不同,需加上泛型約束class
}
public virtual void Hidden()
{
this.gameObject.SetActive(false);
}
public virtual void Show()
{
this.gameObject.SetActive(true);
}
}
??(E)邏輯封裝——主界面類
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 開始面板類
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class BeginPlane : BeginFather<BeginPlane>
{
//獲取開始面板中的各個(gè)按鈕控件
public Button but1;
public Button but2;
public Button but3;
public Button but4;
private void Start()
{
but1.triggerEvent += () => { SceneManager.LoadScene("Start"); }; //點(diǎn)擊“開始游戲”
but2.triggerEvent += () => { Hidden(); }; //點(diǎn)擊“游戲設(shè)置”
but3.triggerEvent += () => { Application.Quit(); }; //點(diǎn)擊“退出游戲”
but4.triggerEvent += () => { Hidden(); }; //點(diǎn)擊“排行榜”
}
}
??(F)設(shè)置界面制作
??(G)邏輯封裝——設(shè)置面板類
- 首先讓面板進(jìn)行合理的顯隱切換
- 當(dāng)按下設(shè)置時(shí),開始界面功能失效并且隱藏,設(shè)置界面激活
- 實(shí)現(xiàn)效果
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 設(shè)置面板功能
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class SetPlane : BeginFather<SetPlane>
{
//將功能控件拖拽對(duì)應(yīng)的成員變量中
public Button closeButt;
public Slider sliderMusic;
public Slider sliderSound;
public ToggleM toggleMusic;
public ToggleM toggleSound;
private float toggle1;
private float toggle2;
private void Start()
{
this.Hidden();
//按鈕中事件的添加
closeButt.triggerEvent += () => {
this.gameObject.SetActive(false);
BeginPlane.SingleInstance.Show();
};
//滑條中事件的添加
sliderMusic.triggerEvent += (value) =>
{
};
sliderSound.triggerEvent += (value) =>
{
};
//多選框中事件的添加
toggleMusic.triggerEvent += (value) =>
{
};
toggleSound .triggerEvent += (value) =>
{
};
}
}
----------------------
此時(shí)開始面板腳本也需要修改
----------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//—————————————————————————————————————
//___________項(xiàng)目: ______________
//___________功能: 開始面板類
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class BeginPlane : BeginFather<BeginPlane>
{
// 獲取開始面板中的各個(gè)按鈕控件
public Button but1;
public Button but2;
public Button but3;
public Button but4;
private void Start()
{
but1.triggerEvent += () => { SceneManager.LoadScene("Start"); }; //點(diǎn)擊“開始游戲”
but2.triggerEvent += () => { Hidden(); SetPlane.SingleInstance.Show(); }; //點(diǎn)擊“游戲設(shè)置”
but3.triggerEvent += () => { Application.Quit(); }; //點(diǎn)擊“退出游戲”
but4.triggerEvent += () => { Hidden(); }; //點(diǎn)擊“排行榜”
}
}
?相關(guān)文章?
?【2023unity游戲制作-mango的冒險(xiǎn)】-6.關(guān)卡設(shè)計(jì)
?【2023unity游戲制作-mango的冒險(xiǎn)】-5.攻擊系統(tǒng)的簡(jiǎn)單實(shí)現(xiàn)
?【2023unity游戲制作-mango的冒險(xiǎn)】-4.場(chǎng)景二的鏡頭和法球特效跟隨
?【2023unity游戲制作-mango的冒險(xiǎn)】-3.基礎(chǔ)動(dòng)作和動(dòng)畫API實(shí)現(xiàn)
?【2023unity游戲制作-mango的冒險(xiǎn)】-2.始畫面API制作
?【2023unity游戲制作-mango的冒險(xiǎn)】-1.場(chǎng)景搭建
?“狂飆”游戲制作—游戲分類圖鑒(網(wǎng)易游學(xué))
?本站最全-unity常用API大全(萬字詳解),不信你不收藏文章來源:http://www.zghlxwxcb.cn/news/detail-556857.html
你們的點(diǎn)贊?? 收藏? 留言?? 關(guān)注?是我持續(xù)創(chuàng)作,輸出優(yōu)質(zhì)內(nèi)容的最大動(dòng)力!文章來源地址http://www.zghlxwxcb.cn/news/detail-556857.html
到了這里,關(guān)于【unity之IMGUI實(shí)踐】單例模式管理面板對(duì)象【一】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!