UnityEditor Tabs 分頁(yè)表簽

不確定大家是否需要講解泡垃,還是說(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;
        }
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末氮双,一起剝皮案震驚了整個(gè)濱河市碰酝,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌戴差,老刑警劉巖送爸,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異暖释,居然都是意外死亡袭厂,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)球匕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)纹磺,“玉大人,你說(shuō)我怎么就攤上這事亮曹¢涎睿” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵照卦,是天一觀的道長(zhǎng)式矫。 經(jīng)常有香客問(wèn)我,道長(zhǎng)役耕,這世上最難降的妖魔是什么采转? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮瞬痘,結(jié)果婚禮上氏义,老公的妹妹穿的比我還像新娘。我一直安慰自己图云,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布邻邮。 她就那樣靜靜地躺著竣况,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上丹泉,一...
    開(kāi)封第一講書(shū)人閱讀 51,718評(píng)論 1 305
  • 那天情萤,我揣著相機(jī)與錄音,去河邊找鬼摹恨。 笑死筋岛,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的晒哄。 我是一名探鬼主播睁宰,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼寝凌!你這毒婦竟也來(lái)了柒傻?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤较木,失蹤者是張志新(化名)和其女友劉穎红符,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體伐债,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡预侯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了峰锁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片萎馅。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖祖今,靈堂內(nèi)的尸體忽然破棺而出校坑,到底是詐尸還是另有隱情,我是刑警寧澤千诬,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布耍目,位于F島的核電站,受9級(jí)特大地震影響徐绑,放射性物質(zhì)發(fā)生泄漏邪驮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一傲茄、第九天 我趴在偏房一處隱蔽的房頂上張望毅访。 院中可真熱鬧,春花似錦盘榨、人聲如沸喻粹。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)守呜。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間查乒,已是汗流浹背弥喉。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留玛迄,地道東北人由境。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像蓖议,于是被迫代替她去往敵國(guó)和親虏杰。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容