不確定大家是否需要講解泡垃,還是說(shuō)只要有代碼就夠了,也沒(méi)什么人給我留言渠驼,所以我就按自己的想法來(lái)說(shuō)了蜈块。
首先Tabs表簽的需求是普遍的。做表簽的方式也有很多迷扇,我只說(shuō)一個(gè)我習(xí)慣用的吧百揭。如果有大神有更好的方法希望也能留言交流,相互學(xué)習(xí)蜓席。萬(wàn)分感謝器一。
1 Tabs標(biāo)簽頁(yè)的實(shí)現(xiàn)
首先定義一個(gè)枚舉,作為狀態(tài)切換基礎(chǔ)邏輯表簽
enum ETAB
{
State_Config, //狀態(tài)配置
GUI_Style, //系統(tǒng)GUI
Unity_Icon, //系統(tǒng)ICON
}
定義一個(gè)字符串?dāng)?shù)組厨内,用來(lái)顯示對(duì)應(yīng)表簽的文字描述祈秕,當(dāng)然你也可以用其他各種容器各種對(duì)象,比方說(shuō)Texture數(shù)組雏胃。
static string[] TAB = new string[] { "狀態(tài)配置", "系統(tǒng)GUI", "系統(tǒng)ICON" };
GUIContent[] GUIArr = new GUIContent[]{ EditorGUIUtility.IconContent("d_BuildSettings.Switch") ,
EditorGUIUtility.IconContent("d_BuildSettings.PS4") ,
EditorGUIUtility.IconContent("d_BuildSettings.XboxOne") };
下面是分頁(yè)表簽的簡(jiǎn)單的選中切換狀態(tài)的邏輯请毛。比較簡(jiǎn)單就不細(xì)說(shuō)了直接看代碼就好了。主要是我也不太會(huì)白話瞭亮。
private void OnGUI()
{
GUILayout.BeginHorizontal();
for (int i = 0; i < TAB.Length; ++i)
{
if (i == (int)m_Tab)
m_tabStyle = m_tabBtnOnStyle;
else
m_tabStyle = m_tabBtnStyle;
if (GUILayout.Button(TAB[i], m_tabStyle, GUILayout.Width(90)))
m_Tab = (ETAB)i;
}
GUILayout.EndHorizontal();
GUILayout.Space(5);
switch (m_Tab)
{
case ETAB.State_Config:
// todo some gui logic
break;
case ETAB.GUI_Style:
// todo some gui logic
break;
case ETAB.Unity_Icon:
// todo some gui logic
break;
}
}
2 Unity編輯器中的全部系統(tǒng)樣式 All GUIStyles in Unity Editor
private void OnGUI()
{
m_scrollPosition = GUILayout.BeginScrollView(m_scrollPosition);
foreach (var style in GUI.skin.customStyles)
{
GUILayout.Space(5);
GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
if (GUILayout.Button(style.name, style, GUILayout.Width(280)))
{
EditorGUIUtility.systemCopyBuffer = style.name;
Debug.LogError(style.name);
}
GUILayout.Space(20);
EditorGUILayout.SelectableLabel(style.name, GUILayout.Width(200));
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
Unity 編輯器中的系統(tǒng)圖標(biāo) All Icons in UnityEditor
這一塊的話上一帖已經(jīng)講過(guò)了方仿。這里就不再多贅述了。
private void InitIconList()
{
if (m_Icons == null || m_Icons.Count == 0)
{
m_Icons = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(Texture)));
m_Icons.Sort((pA, pB) => System.String.Compare(pA.name, pB.name, System.StringComparison.OrdinalIgnoreCase));
}
}
private void OnGUI()
{
InitIconList();
GUILayout.BeginHorizontal("HelpBox");
m_scrollPosition = GUILayout.BeginScrollView(m_scrollPosition);
if (m_Icons != null)
{
for (int i = 0; i < m_Icons.Count; i++)
{
if (i % 5 == 0)
{
GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
for (int j = 0; j < 5; j++)
{
if (i+j >= m_Icons.Count)
break;
var icon = m_Icons[i+j];
GUILayout.BeginVertical();
if (GUILayout.Button((Texture)icon, GUILayout.Width(100), GUILayout.Height(100)))
{
m_te.text = icon.name;
m_te.SelectAll();
m_te.Copy();
this.ShowNotification(new GUIContent("Unity 體統(tǒng)不表:" + icon.name + "已經(jīng)復(fù)制到剪切板!"));
}
GUILayout.BeginHorizontal();
GUILayout.Label((i+j).ToString(), GUILayout.Width(20));
GUILayout.Label(icon.name, m_iconNameStyle, GUILayout.Width(80));
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
GUILayout.Space(15);
}
}
}
GUILayout.EndScrollView();
}
完整代碼
下面是GIF的完整代碼仙蚜,方便需要的人自取此洲。如果發(fā)現(xiàn)有bug的話記得給我留言,謝謝了
/********************************************************************
Copyright (C) 2020 STUPID DOG STUDIO
類(lèi) 名:TabDemo.cs
創(chuàng)建時(shí)間:2021-04-07 19:04:02
作 者:Birth.Fat
描 述:
版 本:1.0
*********************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TabDemo : EditorWindow
{
Vector2 m_scrollPosition = new Vector2(0, 0);
List<UnityEngine.Object> m_Icons;
TextEditor m_te = new TextEditor();
enum ETAB
{
State_Config, //狀態(tài)配置
GUI_Style, //系統(tǒng)GUI
Unity_Icon, //系統(tǒng)ICON
}
static string[] TAB = new string[] { "狀態(tài)配置", "系統(tǒng)GUI", "系統(tǒng)ICON" };
GUIContent[] GUIArr;
ETAB m_Tab = ETAB.State_Config;
static private Vector2 m_winSize = new Vector2(800, 750);
GUIStyle m_tabBtnStyle;
GUIStyle m_tabBtnOnStyle;
GUIStyle m_textStyle;
GUIStyle m_labStyle;
GUIStyle m_tabTextSytel;
GUIStyle m_tabStyle;
GUIStyle m_iconNameStyle;
GUIStyle m_saveBtnStyle;
GUIStyle m_toggleStyle;
GUIStyle m_titleStyle;
[MenuItem("Tools/Tab表簽")]
static void AddWindow()
{
Rect wr = new Rect(0, 0, 600, 600);
TabDemo window = (TabDemo)EditorWindow.GetWindowWithRect(typeof(TabDemo), wr, true, "Unity Tab表簽");
window.Show();
}
//------------------------------------------------------
private void OnGUI()
{
InitGUIStyle();
InitIconTabs();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
//for (int i = 0; i < TAB.Length; ++i)
for (int i = 0; i < GUIArr.Length; ++i)
{
GUILayout.Space(3);
if (i == (int)m_Tab)
m_tabStyle = m_tabBtnOnStyle;
else
m_tabStyle = m_tabBtnStyle;
//if (GUILayout.Button(TAB[i], m_tabStyle, GUILayout.Width(90)))
if (GUILayout.Button(GUIArr[i], m_tabStyle, GUILayout.Width(90)))
{
m_Tab = (ETAB)i;
}
}
GUILayout.EndHorizontal();
GUILayout.Space(5);
switch (m_Tab)
{
case ETAB.State_Config:
StateConfig();
break;
case ETAB.GUI_Style:
SystemGUI();
break;
case ETAB.Unity_Icon:
SystemIcon();
break;
}
}
private void StateConfig()
{
GUILayout.BeginVertical("PopupCurveSwatchBackground");
GUILayout.Space(5);
GUILayout.Label("青玉案·元夕", m_textStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("【作者】辛棄疾 【朝代】宋", m_titleStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("東風(fēng)夜放花千樹(shù)", m_labStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("更吹落鳍征、星如雨", m_labStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("寶馬雕車(chē)香滿路", m_labStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("鳳簫聲動(dòng)黍翎,玉壺光轉(zhuǎn),一夜魚(yú)龍舞", m_labStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("蛾兒雪柳黃金縷", m_labStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("笑語(yǔ)盈盈暗香去", m_labStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("眾里尋他千百度", m_labStyle, GUILayout.Width(590));
GUILayout.Space(5);
GUILayout.Label("驀然回首艳丛,那人卻在匣掸,燈火闌珊處", m_labStyle, GUILayout.Width(590));
GUILayout.EndVertical();
}
private void SystemGUI()
{
m_scrollPosition = GUILayout.BeginScrollView(m_scrollPosition);
foreach (var style in GUI.skin.customStyles)
{
GUILayout.Space(5);
GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
if (GUILayout.Button(style.name, style, GUILayout.Width(280)))
{
EditorGUIUtility.systemCopyBuffer = style.name;
Debug.LogError(style.name);
}
GUILayout.Space(20);
EditorGUILayout.SelectableLabel(style.name, GUILayout.Width(200));
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
private void SystemIcon()
{
InitIconList();
GUILayout.BeginHorizontal("HelpBox");
m_scrollPosition = GUILayout.BeginScrollView(m_scrollPosition);
if (m_Icons != null)
{
for (int i = 0; i < m_Icons.Count; i++)
{
if (i % 5 == 0)
{
GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
for (int j = 0; j < 5; j++)
{
if (i+j >= m_Icons.Count)
break;
var icon = m_Icons[i+j];
GUILayout.BeginVertical();
if (GUILayout.Button((Texture)icon, GUILayout.Width(100), GUILayout.Height(100)))
{
m_te.text = icon.name;
m_te.SelectAll();
m_te.Copy();
this.ShowNotification(new GUIContent("Unity 體統(tǒng)不表:" + icon.name + "已經(jīng)復(fù)制到剪切板!"));
}
GUILayout.BeginHorizontal();
GUILayout.Label((i+j).ToString(), GUILayout.Width(20));
GUILayout.Label(icon.name, m_iconNameStyle, GUILayout.Width(80));
GUILayout.EndHorizontal();
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
GUILayout.Space(15);
}
}
}
GUILayout.EndScrollView();
}
private void InitIconList()
{
if (m_Icons == null || m_Icons.Count == 0)
{
m_Icons = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(Texture)));
m_Icons.Sort((pA, pB) => System.String.Compare(pA.name, pB.name, System.StringComparison.OrdinalIgnoreCase));
}
}
//------------------------------------------------------
private void InitIconTabs()
{
GUIArr = new GUIContent[]{ EditorGUIUtility.IconContent("d_BuildSettings.Switch") ,
EditorGUIUtility.IconContent("d_BuildSettings.PS4") ,
EditorGUIUtility.IconContent("d_BuildSettings.XboxOne") };
}
//------------------------------------------------------
private void InitGUIStyle()
{
if (m_tabTextSytel == null)
{
m_tabTextSytel = new GUIStyle("BoldLabel");
}
if (m_tabBtnStyle == null)
{
m_tabBtnStyle = new GUIStyle("flow node 0");
m_tabBtnStyle.alignment = TextAnchor.MiddleCenter;
m_tabBtnStyle.fontStyle = m_tabTextSytel.fontStyle;
m_tabBtnStyle.fontSize = 18;
//color = m_tabBtnStyle.normal.textColor;
}
if (m_tabBtnOnStyle == null)
{
m_tabBtnOnStyle = new GUIStyle("flow node 1 on");
m_tabBtnOnStyle.alignment = TextAnchor.MiddleCenter;
m_tabBtnOnStyle.fontStyle = m_tabTextSytel.fontStyle;
m_tabBtnOnStyle.fontSize = 18;
}
if (m_textStyle == null)
{
m_textStyle = new GUIStyle("HeaderLabel");
m_textStyle.fontSize = 20;
m_textStyle.alignment = TextAnchor.MiddleCenter;
}
if(m_labStyle == null)
{
m_labStyle = new GUIStyle("CenteredLabel");
m_labStyle.fontSize = 18;
}
if(m_titleStyle == null)
{
m_titleStyle = new GUIStyle("AM VuValue");
m_titleStyle.alignment = TextAnchor.MiddleRight;
m_titleStyle.fontSize = 15;
}
if (m_iconNameStyle == null)
{
m_iconNameStyle = new GUIStyle("WarningOverlay");
m_iconNameStyle.fontSize = 12;
}
if (m_toggleStyle == null)
{
m_toggleStyle = new GUIStyle("OL ToggleWhite");
}
if (m_saveBtnStyle == null)
{
m_saveBtnStyle = new GUIStyle("flow node 1");
m_saveBtnStyle.fontSize = 20;
m_saveBtnStyle.fixedHeight = 40;
m_saveBtnStyle.alignment = TextAnchor.MiddleCenter;
}
}
}