Unity 内置GUIStyle 样例预览

本文详尽展示了Unity中所有内置GUIStyle样式的预览,通过代码示例介绍了如何在Unity编辑器中查看和使用这些样式。适用于游戏开发者和UI设计师,帮助他们更好地掌握Unity界面设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Unity 内置GUIStyle 样例预览

Unity 查看所有GUI默认样式:https://blog.youkuaiyun.com/akof1314/article/details/39297811
参考至:https://pastebin.com/index/JZKDTwnV

  • 效果如下:
    在这里插入图片描述
using UnityEditor;
using UnityEngine;

public sealed class GUIStyleExampleWindow : EditorWindow
{
    private static readonly string[] mList =
    {
        "AboutWIndowLicenseLabel"                       ,
        "AC LeftArrow"                                  ,
        "AC RightArrow"                                 ,
        "AnimationCurveEditorBackground"                ,
        "AnimationEventBackground"                      ,
        "AnimationEventTooltip"                         ,
        "AnimationEventTooltipArrow"                    ,
        "AnimationKeyframeBackground"                   ,
        "AnimationRowEven"                              ,
        "AnimationRowOdd"                               ,
        "AnimationSelectionTextField"                   ,
        "AnimationTimelineTick"                         ,
        "AnimPropDropdown"                              ,
        "AppToolbar"                                    ,
        "AS TextArea"                                   ,
        "BoldLabel"                                     ,
        "BoldToggle"                                    ,
        "ButtonLeft"                                    ,
        "ButtonMid"                                     ,
        "ButtonRight"                                   ,
        "CN Box"                                        ,
        "CN CountBadge"                                 ,
        "CN EntryBackEven"                              ,
        "CN EntryBackOdd"                               ,
        "CN EntryError"                                 ,
        "CN EntryInfo"                                  ,
        "CN EntryWarn"                                  ,
        "CN Message"                                    ,
        "CN StatusError"                                ,
        "CN StatusInfo"                                 ,
        "CN StatusWarn"                                 ,
        "ColorField"                                    ,
        "ColorPicker2DThumb"                            ,
        "ColorPickerBackground"                         ,
        "ColorPickerBox"                                ,
        "ColorPickerHorizThumb"                         ,
        "ColorPickerVertThumb"                          ,
        "Command"                                       ,
        "CommandLeft"                                   ,
        "CommandMid"                                    ,
        "CommandRight"                                  ,
        "ControlLabel"                                  ,
        "CurveEditorLabelTickmarks"                     ,
        "debug_layout_box"                              ,
        "dockarea"                                      ,
        "dockareaOverlay"                               ,
        "dockareaStandalone"                            ,
        "dragtab"                                       ,
        "dragtabbright"                                 ,
        "dragtabdropwindow"                             ,
        "DropDown"                                      ,
        "DropDownButton"                                ,
        "ErrorLabel"                                    ,
        "ExposablePopupItem"                            ,
        "ExposablePopupMenu"                            ,
        "EyeDropperHorizontalLine"                      ,
        "EyeDropperPickedPixel"                         ,
        "EyeDropperVerticalLine"                        ,
        "flow background"                               ,
        "flow navbar back"                              ,
        "flow navbar button"                            ,
        "flow navbar separator"                         ,
        "flow node 0"                                   ,
        "flow node 0 on"                                ,
        "flow node 1"                                   ,
        "flow node 1 on"                                ,
        "flow node 2"                                   ,
        "flow node 2 on"                                ,
        "flow node 3"                                   ,
        "flow node 3 on"                                ,
        "flow node 4"                                   ,
        "flow node 4 on"                                ,
        "flow node 5"                                   ,
        "flow node 5 on"                                ,
        "flow node 6"                                   ,
        "flow node 6 on"                                ,
        "flow node hex 0"                               ,
        "flow node hex 0 on"                            ,
        "flow node hex 1"                               ,
        "flow node hex 1 on"                            ,
        "flow node hex 2"                               ,
        "flow node hex 2 on"                            ,
        "flow node hex 3"                               ,
        "flow node hex 3 on"                            ,
        "flow node hex 4"                               ,
        "flow node hex 4 on"                            ,
        "flow node hex 5"                               ,
        "flow node hex 5 on"                            ,
        "flow node hex 6"                               ,
        "flow node hex 6 on"                            ,
        "flow node titlebar"                            ,
        "flow overlay area left"                        ,
        "flow overlay area right"                       ,
        "flow overlay box"                              ,
        "flow overlay foldout"                          ,
        "flow overlay header lower left"                ,
        "flow overlay header lower right"               ,
        "flow overlay header upper left"                ,
        "flow overlay header upper right"               ,
        "flow shader in 0"                              ,
        "flow shader in 1"                              ,
        "flow shader in 2"                              ,
        "flow shader in 3"                              ,
        "flow shader in 4"                              ,
        "flow shader in 5"                              ,
        "flow shader node 0"                            ,
        "flow shader node 0 on"                         ,
        "flow shader out 0"                             ,
        "flow shader out 1"                             ,
        "flow shader out 2"                             ,
        "flow shader out 3"                             ,
        "flow shader out 4"                             ,
        "flow shader out 5"                             ,
        "flow target in"                                ,
        "flow triggerPin in"                            ,
        "flow triggerPin out"                           ,
        "flow var 0"                                    ,
        "flow var 0 on"                                 ,
        "flow var 1"                                    ,
        "flow var 1 on"                                 ,
        "flow var 2"                                    ,
        "flow var 2 on"                                 ,
        "flow var 3"                                    ,
        "flow var 3 on"                                 ,
        "flow var 4"                                    ,
        "flow var 4 on"                                 ,
        "flow var 5"                                    ,
        "flow var 5 on"                                 ,
        "flow var 6"                                    ,
        "flow var 6 on"                                 ,
        "flow varPin in"                                ,
        "flow varPin out"                               ,
        "flow varPin tooltip"                           ,
        "Foldout"                                       ,
        "FoldOutPreDrop"                                ,
        "GameViewBackground"                            ,
        "Grad Down Swatch"                              ,
        "Grad Down Swatch Overlay"                      ,
        "Grad Up Swatch"                                ,
        "Grad Up Swatch Overlay"                        ,
        "grey_border"                                   ,
        "GridList"                                      ,
        "GridListText"                                  ,
        "GridToggle"                                    ,
        "GroupBox"                                      ,
        "GUIEditor.BreadcrumbLeft"                      ,
        "GUIEditor.BreadcrumbMid"                       ,
        "GV Gizmo DropDown"                             ,
        "HeaderLabel"                                   ,
        "HelpBox"                                       ,
        "Hi Label"                                      ,
        "HorizontalMinMaxScrollbarThumb"                ,
        "hostview"                                      ,
        "IN BigTitle"                                   ,
        "IN BigTitle Inner"                             ,
        "IN ColorField"                                 ,
        "IN DropDown"                                   ,
        "IN Foldout"                                    ,
        "IN FoldoutStatic"                              ,
        "IN Label"                                      ,
        "IN LockButton"                                 ,
        "IN ObjectField"                                ,
        "IN Popup"                                      ,
        "IN SelectedLine"                               ,
        "IN TextField"                                  ,
        "IN ThumbnailSelection"                         ,
        "IN ThumbnailShadow"                            ,
        "IN Title"                                      ,
        "IN TitleText"                                  ,
        "IN Toggle"                                     ,
        "InnerShadowBg"                                 ,
        "InvisibleButton"                               ,
        "LargeButton"                                   ,
        "LargeButtonLeft"                               ,
        "LargeButtonMid"                                ,
        "LargeButtonRight"                              ,
        "LargeDropDown"                                 ,
        "LargeLabel"                                    ,
        "LargePopup"                                    ,
        "LargeTextField"                                ,
        "LightmapEditorSelectedHighlight"               ,
        "ListToggle"                                    ,
        "LockedHeaderBackground"                        ,
        "LockedHeaderButton"                            ,
        "LockedHeaderLabel"                             ,
        "LODBlackBox"                                   ,
        "LODCameraLine"                                 ,
        "LODLevelNotifyText"                            ,
        "LODRendererAddButton"                          ,
        "LODRendererButton"                             ,
        "LODRendererRemove"                             ,
        "LODRenderersText"                              ,
        "LODSceneText"                                  ,
        "LODSliderBG"                                   ,
        "LODSliderRange"                                ,
        "LODSliderRangeSelected"                        ,
        "LODSliderText"                                 ,
        "LODSliderTextSelected"                         ,
        "MeBlendBackground"                             ,
        "MeBlendPosition"                               ,
        "MeBlendTriangleLeft"                           ,
        "MeBlendTriangleRight"                          ,
        "MeLivePlayBackground"                          ,
        "MeLivePlayBar"                                 ,
        "MeTimeLabel"                                   ,
        "MeTransBGOver"                                 ,
        "MeTransitionBack"                              ,
        "MeTransitionBlock"                             ,
        "MeTransitionHandleLeft"                        ,
        "MeTransitionHandleLeftPrev"                    ,
        "MeTransitionHandleRight"                       ,
        "MeTransitionHead"                              ,
        "MeTransitionSelect"                            ,
        "MeTransitionSelectHead"                        ,
        "MeTransOff2On"                                 ,
        "MeTransOffLeft"                                ,
        "MeTransOffRight"                               ,
        "MeTransOn2Off"                                 ,
        "MeTransOnLeft"                                 ,
        "MeTransOnRight"                                ,
        "MeTransPlayhead"                               ,
        "MiniBoldLabel"                                 ,
        "minibutton"                                    ,
        "minibuttonleft"                                ,
        "minibuttonmid"                                 ,
        "minibuttonright"                               ,
        "MiniLabel"                                     ,
        "MiniLabelRight"                                ,
        "MiniMinMaxSliderHorizontal"                    ,
        "MiniMinMaxSliderVertical"                      ,
        "MiniPopup"                                     ,
        "MiniPullDown"                                  ,
        "MiniPullDownLeft"                              ,
        "MiniTextField"                                 ,
        "MiniToolbarButton"                             ,
        "MiniToolbarButtonLeft"                         ,
        "MiniToolbarPopup"                              ,
        "MinMaxHorizontalSliderThumb"                   ,
        "NotificationBackground"                        ,
        "NotificationText"                              ,
        "ObjectField"                                   ,
        "ObjectFieldThumb"                              ,
        "ObjectFieldThumbOverlay"                       ,
        "ObjectFieldThumbOverlay2"                      ,
        "ObjectPickerBackground"                        ,
        "ObjectPickerGroupHeader"                       ,
        "ObjectPickerLargeStatus"                       ,
        "ObjectPickerPreviewBackground"                 ,
        "ObjectPickerResultsEven"                       ,
        "ObjectPickerResultsGrid"                       ,
        "ObjectPickerResultsGridLabel"                  ,
        "ObjectPickerResultsOdd"                        ,
        "ObjectPickerSmallStatus"                       ,
        "ObjectPickerTab"                               ,
        "ObjectPickerToolbar"                           ,
        "OL box"                                        ,
        "OL box NoExpand"                               ,
        "OL Elem"                                       ,
        "OL EntryBackEven"                              ,
        "OL EntryBackOdd"                               ,
        "OL header"                                     ,
        "OL Label"                                      ,
        "OL Minus"                                      ,
        "OL Plus"                                       ,
        "OL TextField"                                  ,
        "OL Title"                                      ,
        "OL Title TextRight"                            ,
        "OL Titleleft"                                  ,
        "OL Titlemid"                                   ,
        "OL Titleright"                                 ,
        "OL Toggle"                                     ,
        "OL ToggleWhite"                                ,
        "PaneOptions"                                   ,
        "PlayerSettingsLevel"                           ,
        "PlayerSettingsPlatform"                        ,
        "Popup"                                         ,
        "PopupBackground"                               ,
        "PopupCurveDropdown"                            ,
        "PopupCurveEditorBackground"                    ,
        "PopupCurveEditorSwatch"                        ,
        "PopupCurveSwatchBackground"                    ,
        "PR DigDownArrow"                               ,
        "PR Insertion"                                  ,
        "PR Label"                                      ,
        "PR Ping"                                       ,
        "PR TextField"                                  ,
        "PreBackground"                                 ,
        "PreButton"                                     ,
        "PreferencesKeysElement"                        ,
        "PreferencesSection"                            ,
        "PreferencesSectionBox"                         ,
        "PreHorizontalScrollbar"                        ,
        "PreHorizontalScrollbarThumb"                   ,
        "PreLabel"                                      ,
        "PreOverlayLabel"                               ,
        "PreSlider"                                     ,
        "PreSliderThumb"                                ,
        "PreToolbar"                                    ,
        "PreToolbar2"                                   ,
        "PreVerticalScrollbar"                          ,
        "PreVerticalScrollbarThumb"                     ,
        "ProfilerBadge"                                 ,
        "ProfilerLeftPane"                              ,
        "ProfilerLeftPaneOverlay"                       ,
        "ProfilerPaneLeftBackground"                    ,
        "ProfilerPaneSubLabel"                          ,
        "ProfilerRightPane"                             ,
        "ProfilerScrollviewBackground"                  ,
        "ProfilerSelectedLabel"                         ,
        "ProgressBarBack"                               ,
        "ProgressBarBar"                                ,
        "ProgressBarText"                               ,
        "ProjectBrowserBottomBarBg"                     ,
        "ProjectBrowserGridLabel"                       ,
        "ProjectBrowserHeaderBgMiddle"                  ,
        "ProjectBrowserHeaderBgTop"                     ,
        "ProjectBrowserIconAreaBg"                      ,
        "ProjectBrowserIconDropShadow"                  ,
        "ProjectBrowserPreviewBg"                       ,
        "ProjectBrowserSubAssetBg"                      ,
        "ProjectBrowserSubAssetBgCloseEnded"            ,
        "ProjectBrowserSubAssetBgDivider"               ,
        "ProjectBrowserSubAssetBgMiddle"                ,
        "ProjectBrowserSubAssetBgOpenEnded"             ,
        "ProjectBrowserSubAssetExpandBtn"               ,
        "ProjectBrowserTopBarBg"                        ,
        "QualitySettingsDefault"                        ,
        "Radio"                                         ,
        "RightLabel"                                    ,
        "RL Background"                                 ,
        "RL DragHandle"                                 ,
        "RL Element"                                    ,
        "RL Footer"                                     ,
        "RL FooterButton"                               ,
        "RL Header"                                     ,
        "SC ViewAxisLabel"                              ,
        "SC ViewLabel"                                  ,
        "SceneViewOverlayTransparentBackground"         ,
        "ScriptText"                                    ,
        "SearchCancelButton"                            ,
        "SearchCancelButtonEmpty"                       ,
        "SearchModeFilter"                              ,
        "SearchTextField"                               ,
        "SelectionRect"                                 ,
        "ServerChangeCount"                             ,
        "ServerUpdateChangeset"                         ,
        "ServerUpdateChangesetOn"                       ,
        "ServerUpdateInfo"                              ,
        "ServerUpdateLog"                               ,
        "ShurikenCheckMark"                             ,
        "ShurikenEffectBg"                              ,
        "ShurikenEmitterTitle"                          ,
        "ShurikenLabel"                                 ,
        "ShurikenLine"                                  ,
        "ShurikenMinus"                                 ,
        "ShurikenModuleBg"                              ,
        "ShurikenModuleTitle"                           ,
        "ShurikenObjectField"                           ,
        "ShurikenPlus"                                  ,
        "ShurikenPopUp"                                 ,
        "ShurikenToggle"                                ,
        "ShurikenValue"                                 ,
        "SimplePopup"                                   ,
        "SliderMixed"                                   ,
        "StaticDropdown"                                ,
        "sv_iconselector_back"                          ,
        "sv_iconselector_button"                        ,
        "sv_iconselector_labelselection"                ,
        "sv_iconselector_selection"                     ,
        "sv_iconselector_sep"                           ,
        "sv_label_0"                                    ,
        "sv_label_1"                                    ,
        "sv_label_2"                                    ,
        "sv_label_3"                                    ,
        "sv_label_4"                                    ,
        "sv_label_5"                                    ,
        "sv_label_6"                                    ,
        "sv_label_7"                                    ,
        "TabWindowBackground"                           ,
        "Tag MenuItem"                                  ,
        "Tag TextField"                                 ,
        "Tag TextField Button"                          ,
        "Tag TextField Empty"                           ,
        "TE NodeBackground"                             ,
        "TE NodeBox"                                    ,
        "TE NodeBoxSelected"                            ,
        "TE NodeLabelBot"                               ,
        "TE NodeLabelTop"                               ,
        "TE PinLabel"                                   ,
        "TE Toolbar"                                    ,
        "TE toolbarbutton"                              ,
        "TE ToolbarDropDown"                            ,
        "TimeScrubber"                                  ,
        "TimeScrubberButton"                            ,
        "TL BaseStateLogicBarOverlay"                   ,
        "TL EndPoint"                                   ,
        "TL InPoint"                                    ,
        "TL ItemTitle"                                  ,
        "TL LeftColumn"                                 ,
        "TL LeftItem"                                   ,
        "TL LogicBar 0"                                 ,
        "TL LogicBar 1"                                 ,
        "TL LogicBar parentgrey"                        ,
        "TL LoopSection"                                ,
        "TL OutPoint"                                   ,
        "TL Playhead"                                   ,
        "TL Range Overlay"                              ,
        "TL RightLine"                                  ,
        "TL Selection H1"                               ,
        "TL Selection H2"                               ,
        "TL SelectionBarCloseButton"                    ,
        "TL SelectionBarPreview"                        ,
        "TL SelectionBarText"                           ,
        "TL SelectionButton"                            ,
        "TL SelectionButton PreDropGlow"                ,
        "TL SelectionButtonName"                        ,
        "TL SelectionButtonNew"                         ,
        "TL tab left"                                   ,
        "TL tab mid"                                    ,
        "TL tab plus left"                              ,
        "TL tab plus right"                             ,
        "TL tab right"                                  ,
        "ToggleMixed"                                   ,
        "Toolbar"                                       ,
        "toolbarbutton"                                 ,
        "ToolbarDropDown"                               ,
        "ToolbarPopup"                                  ,
        "ToolbarSeachCancelButton"                      ,
        "ToolbarSeachCancelButtonEmpty"                 ,
        "ToolbarSeachTextField"                         ,
        "ToolbarSeachTextFieldPopup"                    ,
        "ToolbarSearchField"                            ,
        "ToolbarTextField"                              ,
        "Tooltip"                                       ,
        "U2D.createRect"                                ,
        "U2D.dragDot"                                   ,
        "U2D.dragDotDimmed"                             ,
        "VCS_StickyNote"                                ,
        "VCS_StickyNoteArrow"                           ,
        "VCS_StickyNoteLabel"                           ,
        "VCS_StickyNoteP4"                              ,
        "VerticalMinMaxScrollbarThumb"                  ,
        "VisibilityToggle"                              ,
        "WhiteBoldLabel"                                ,
        "WhiteLabel"                                    ,
        "WhiteLargeLabel"                               ,
        "WhiteMiniLabel"                                ,
        "WinBtnCloseActiveMac"                          ,
        "WinBtnCloseMac"                                ,
        "WinBtnCloseWin"                                ,
        "WinBtnInactiveMac"                             ,
        "WinBtnMaxActiveMac"                            ,
        "WinBtnMaxMac"                                  ,
        "WinBtnMaxWin"                                  ,
        "WinBtnMinActiveMac"                            ,
        "WinBtnMinMac"                                  ,
        "WinBtnMinWin"                                  ,
        "WindowBackground"                              ,
        "WindowBottomResize"                            ,
        "WindowResizeMac"                               ,
        "Wizard Box"                                    ,
        "Wizard Error"                                  ,
        "WordWrapLabel"                                 ,
        "WordWrappedLabel"                              ,
        "WordWrappedMiniLabel"                          ,
        "WrappedLabel"                                  ,
    };

    private Vector2 mScrollPos;

    [MenuItem("Tools/GUIStyle 样例窗口")]
    private static void Example()
    {
        GetWindow<GUIStyleExampleWindow>(true);
    }

    private void OnGUI()
    {
        mScrollPos = EditorGUILayout.BeginScrollView(mScrollPos);
        foreach (var n in mList)
        {
            EditorGUILayout.BeginHorizontal(GUILayout.Height(48));
            EditorGUILayout.SelectableLabel(n);
            EditorGUILayout.Toggle(false, n);
            EditorGUILayout.EndHorizontal();
            GUILayout.Box(
                string.Empty,
                GUILayout.Width(position.width - 24),
                GUILayout.Height(1)
            );
        }
        EditorGUILayout.EndScrollView();
    }
}
<think>我们正在讨论Unity Editor中的GUIStyle Box的使用方法。根据引用[1]和引用[2]中的代码片段,我们可以看到一些GUI元素的用法,但并没有直接展示GUIStyle Box的示。不过,我们可以结合Unity的文档和常见的用法来回答。 GUIStyle Box是Unity GUI系统中用于绘制一个矩形框的控件,通常用于背景、边框或者简单的图片显示。它可以接受一个纹理(Texture)或者一个GUIStyle来定义其外观。 在Unity Editor脚本中,我们经常使用GUILayout.Box或GUI.Box来绘制一个Box。其中,GUILayout.Box会自动布局,而GUI.Box需要指定位置和大小。 下面是一个简单的示,展示如何在EditorWindow中使用GUIStyle Box: ```csharp using UnityEditor; using UnityEngine; public class ExampleWindow : EditorWindow { [MenuItem("Example/Show Window")] public static void ShowWindow() { GetWindow<ExampleWindow>("Example Window"); } // 定义一个GUIStyle,可以设置背景、边框等 private GUIStyle boxStyle; void OnEnable() { // 初始化GUIStyle boxStyle = new GUIStyle(); // 设置一个背景(这里使用默认的皮肤中的box背景) boxStyle.normal.background = GUI.skin.box.normal.background; // 设置边距和内边距等 boxStyle.margin = new RectOffset(10, 10, 10, 10); boxStyle.padding = new RectOffset(5, 5, 5, 5); // 也可以设置字体式等 boxStyle.fontStyle = FontStyle.Bold; boxStyle.alignment = TextAnchor.MiddleCenter; } void OnGUI() { // 使用GUILayout.Box,并应用我们定义的boxStyle GUILayout.Box("This is a box with custom style", boxStyle, GUILayout.Width(200), GUILayout.Height(50)); // 也可以使用纹理 Texture2D tex = Texture2D.whiteTexture; // 这里使用一个白色纹理,实际中可以用自己的纹理 GUILayout.Box(tex, boxStyle, GUILayout.Width(100), GUILayout.Height(100)); // 或者使用GUI.Box,需要指定矩形区域 Rect rect = new Rect(10, 100, 200, 50); GUI.Box(rect, "GUI.Box Example", boxStyle); } } ``` 在这个示中: 1. 我们创建了一个EditorWindow,并在OnEnable中初始化了一个GUIStyle。 2. 我们设置了GUIStyle的背景为默认的box背景(也可以设置成其他纹理),并调整了边距和内边距。 3. 在OnGUI中,我们使用了三种方式绘制Box: - 使用GUILayout.Box绘制一个带有文本的Box,并应用自定义式。 - 使用GUILayout.Box绘制一个纹理(这里使用白色纹理)的Box。 - 使用GUI.Box在指定位置绘制一个Box。 注意:在Editor脚本中,我们通常使用GUILayout来简化布局,因为它会自动排列控件。但如果需要精确控制位置,可以使用GUI方法。 另外,我们也可以直接使用Unity内置GUIStyle如"box"式。如: ```csharp GUILayout.Box("Built-in box style", GUI.skin.box); ``` 或者使用EditorGUIUtility提供的式,如: ```csharp GUILayout.Box("Editor box", EditorStyles.helpBox); ``` 其中,`EditorStyles.helpBox`是编辑器自带的一个帮助框式。 希望这些示能帮助你理解GUIStyle Box在Unity Editor中的使用方法。 §§相关问题§§ 1. 如何在Unity Editor中自定义GUIStyle? 2. Unity Editor中有哪些内置GUIStyle? 3. 如何在Unity GUI中同时使用GUILayout和GUI控件? 4. 如何为GUIStyle Box添加点击事件?</think>### Unity Editor GUIStyle Box 使用方法示Unity Editor 开发中,`GUIStyle Box` 用于创建具有自定义式的矩形区域,常用于背景面板、按钮或装饰元素。以下是详细使用方法和示: #### 基础用法 ```csharp using UnityEditor; using UnityEngine; public class StyleBoxWindow : EditorWindow { [MenuItem("Tools/StyleBox Demo")] static void Init() { GetWindow<StyleBoxWindow>("GUIStyle Box").Show(); } void OnGUI() { // 创建自定义GUIStyle GUIStyle boxStyle = new GUIStyle(GUI.skin.box) { normal = { background = MakeTex(2, 2, new Color(0.2f, 0.3f, 0.8f, 0.5f)) }, border = new RectOffset(5, 5, 5, 5), padding = new RectOffset(10, 10, 10, 10), fontSize = 14, fontStyle = FontStyle.Bold, alignment = TextAnchor.MiddleCenter }; // 使用Box GUILayout.Box("自定义式盒子", boxStyle, GUILayout.Width(200), GUILayout.Height(100)); } // 创建纯色纹理的工具方法 Texture2D MakeTex(int width, int height, Color col) { Color[] pix = new Color[width * height]; for (int i = 0; i < pix.Length; i++) pix[i] = col; Texture2D result = new Texture2D(width, height); result.SetPixels(pix); result.Apply(); return result; } } ``` #### 关键参数说明 1. **背景设置**: ```csharp normal.background = MakeTex(2, 2, Color.blue) // 自定义背景色 ``` 2. **边框控制**: ```csharp border = new RectOffset(left, right, top, bottom) // 九宫格切片边界 ``` 3. **内容排版**: ```csharp padding = new RectOffset(10, 10, 10, 10) // 内边距 alignment = TextAnchor.MiddleCenter // 文本对齐 fontSize = 14 // 字体大小 ``` #### 实际应用场景 1. **编辑器面板背景**: ```csharp GUILayout.BeginVertical(GUI.skin.box); GUILayout.Label("配置面板"); // ...其他控件 GUILayout.EndVertical(); ``` 2. **带式的按钮**: ```csharp if (GUILayout.Button("保存", boxStyle)) { // 保存逻辑 } ``` 3. **分组容器**: ```csharp EditorGUILayout.BeginVertical("Box"); EditorGUILayout.LabelField("对象属性", EditorStyles.boldLabel); // ...属性字段 EditorGUILayout.EndVertical(); ``` #### 使用内置Unity 提供预设式(`EditorStyles` 类): ```csharp // 帮助信息框 GUILayout.Box("需要填写所有字段", EditorStyles.helpBox); // 工具栏背景 GUILayout.BeginHorizontal(EditorStyles.toolbar); // ...工具栏内容 GUILayout.EndHorizontal(); ``` #### 注意事项 1. **性能优化**: - 避免每帧创建新 `GUIStyle`,在 `OnEnable()` 中初始化 - 使用 `GUI.skin` 获取默认式避免重复创建 2. **动态式**: ```csharp boxStyle.normal.textColor = isValid ? Color.black : Color.red; ``` 3. **高级效果**: - 使用 `hover.background` 实现悬停效果 - 设置 `margin` 控制外部间距 - 结合 `GUIContent` 添加图标和提示文本 > 提示:通过 `EditorGUIUtility.isProSkin` 可检测当前是否使用专业版深色主题,实现主题适配[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值