Unity Inspector Custom Tabbar

 • 

效果类似于 Terrain Inspector 的顶层 Tabbar,不过按钮之间有间距,这个如果需要完全一样也可以改 GUIStyle 实现。

public class JZSpaceShipControllerEditor : Editor  
    {
        /// <summary>
        /// 飞船控制器编辑器 Tab 类别枚举
        /// </summary>
        enum JZSpaceShipControllerEditorTab
        {
            General,
            Control,
            Weapon
        };

        /// <summary>
        /// Tab Button Inactive State 样式
        /// </summary>
        private static GUIStyle ToggleButtonStyleNormal = null;
        /// <summary>
        /// Tab Button Active State 样式
        /// </summary>
        private static GUIStyle ToggleButtonStyleToggled = null;
        /// <summary>
        /// 飞船控制器编辑器当前选中的 Tab
        /// </summary>
        private JZSpaceShipControllerEditorTab currentTab;

        public override void OnInspectorGUI()
        {
            JZSpaceShipController spaceShipController =
                (JZSpaceShipController) target;

            if (ToggleButtonStyleNormal == null || ToggleButtonStyleToggled == null)
            {
                ToggleButtonStyleNormal = "Button";
                ToggleButtonStyleToggled = new GUIStyle(ToggleButtonStyleNormal);
                ToggleButtonStyleToggled.normal.background = ToggleButtonStyleToggled.active.background;
            }

            GUILayout.BeginHorizontal();
            foreach (JZSpaceShipControllerEditorTab tab in Enum.GetValues(typeof(JZSpaceShipControllerEditorTab)))
            {
                if (GUILayout.Button(tab.ToString(),
                    currentTab == tab ? ToggleButtonStyleToggled : ToggleButtonStyleNormal))
                {
                    currentTab = tab;
                }
            }
            GUILayout.EndHorizontal();

            switch (currentTab)
            {
                case JZSpaceShipControllerEditorTab.General:
                {
                    EditorGUILayout.HelpBox("Space Ship Controller",MessageType.Info);
                }
                    break;
                case JZSpaceShipControllerEditorTab.Control:
                {
                }
                    break;
                case JZSpaceShipControllerEditorTab.Weapon:
                {
                }
                    break;
            }
        }
    }