?????個人主頁:@元宇宙-秩沅
????? hallo 歡迎 點贊?? 收藏? 留言?? 加關(guān)注?!
????? 本文由 秩沅 原創(chuàng)
????? 收錄于專欄:UnityUI篇實戰(zhàn)
????IMGUI封裝實踐【二】?
?前言?
缺點1:無法在編譯過程進(jìn)行可視化調(diào)整
缺點2:無法分辨率自適應(yīng)
??(A) 封裝可視化腳本控制基類
此圖可忽略
-
UML類圖
-
性能優(yōu)化代碼
-
完整代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目: ______________
//___________功能: 讓所有組件都可視化編輯
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
[ExecuteAlways] //關(guān)鍵特性
public class ShowAllContorlRoot : MonoBehaviour
{
private ControlFather[] allContorls; //里氏替換原則,裝所有子類
private void Start()
{
allContorls = this.GetComponentsInChildren<ControlFather>();
}
private void OnGUI()
{
foreach (ControlFather item in allContorls)
{
//1.表示沒有運行時(編譯時)也來獲取,Start是點擊運行時可以獲取
//2.避免了運行時重復(fù)執(zhí)行該API。消耗性能
if( !Application.isPlayer)
{
allContorls = this.GetComponentsInChildren<ControlFather>();
}
item.Judge();
}
}
}
??(B) 控件創(chuàng)建及其封裝——按鈕
- 按鈕封裝代碼
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//-------------------------------------
//—————————————————————————————————————
//___________項目: ______________
//___________功能: 創(chuàng)建按鈕
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Button : ControlFather
{
public event UnityAction triggerEvent;
//事件的添加在此起到妙用
//只要在外部給予響應(yīng)函數(shù)
protected override void OffDrawStyle()
{
if(GUI.Button(ContorlPosition.LastPos ,ContorlContent))
{
triggerEvent?.Invoke();
}
}
protected override void OnDrawstyle()
{
if(GUI.Button(ContorlPosition.LastPos, ContorlContent,ContorlStyle ) )
{
triggerEvent?.Invoke();
}
}
}
??(C) 開始創(chuàng)建預(yù)制體包
- 目的: 便于套用
代碼集結(jié)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________項目: ______________
//___________功能: 讓所有組件都可視化編輯
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
[ExecuteAlways] //關(guān)鍵特性
public class ShowAllContorlRoot : MonoBehaviour
{
private ControlFather[] allContorls; //里氏替換原則,裝所有子類
private void Start()
{
allContorls = this.GetComponentsInChildren<ControlFather>();
}
private void OnGUI()
{
foreach (ControlFather item in allContorls)
{
//1.表示沒有運行時(編譯時)也來獲取,Start是點擊運行時可以獲取
//2.避免了運行時重復(fù)執(zhí)行該API。消耗性能
if( !Application.isPlayer)
{
allContorls = this.GetComponentsInChildren<ControlFather>();
}
item.Judge();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//-------------------------------------
//—————————————————————————————————————
//___________項目: ______________
//___________功能: 創(chuàng)建按鈕
//___________創(chuàng)建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class Button : ControlFather
{
public event UnityAction triggerEvent;
//事件的添加在此起到妙用
//只要在外部給予響應(yīng)函數(shù)
protected override void OffDrawStyle()
{
if(GUI.Button(ContorlPosition.LastPos ,ContorlContent))
{
triggerEvent?.Invoke();
}
}
protected override void OnDrawstyle()
{
if(GUI.Button(ContorlPosition.LastPos, ContorlContent,ContorlStyle ) )
{
triggerEvent?.Invoke();
}
}
}
ImGUI又稱為Dear ImGui,它是與平臺無關(guān)的C++輕量級跨平臺圖形界面庫,沒有任何第三方依賴,可以將ImGUI的源碼直接加到項目中使用,也可以編譯成dll, ImGUI使用DX或者OpenGL進(jìn)行界面渲染,對于畫面質(zhì)量要求較高,例如客戶端游戲,4k/8k視頻播放時,用ImGUI是很好的選擇,當(dāng)然,你得非常熟悉DirectX或者OpenGL,不然就是寶劍在手,屠龍無力。相對于Qt、MFC、DuiLib、SOUI等,ImGUI的拓展性更好,也更輕量級,當(dāng)然對于開發(fā)者的要求也更高.
?????
?【Unityc#專題篇】之c#進(jìn)階篇】
?【Unityc#專題篇】之c#核心篇】
?【Unityc#專題篇】之c#基礎(chǔ)篇】
?【Unity-c#專題篇】之c#入門篇】
?【Unityc#專題篇】—進(jìn)階章題單實踐練習(xí)
?【Unityc#專題篇】—基礎(chǔ)章題單實踐練習(xí)
?【Unityc#專題篇】—核心章題單實踐練習(xí)
你們的點贊?? 收藏? 留言?? 關(guān)注?是我持續(xù)創(chuàng)作,輸出優(yōu)質(zhì)內(nèi)容的最大動力!、文章來源:http://www.zghlxwxcb.cn/news/detail-612045.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-612045.html
到了這里,關(guān)于【Unity之IMGUI腳本封裝】—編譯模式下控件可視化及其封裝的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!