zoukankan      html  css  js  c++  java
  • Unity编辑器:自定义编辑器样式——GUIStyle

    通过GUIStyle,可以自定义Unity编辑器的样式。

    GUIStyle可以new一个全新的实例,这样,需要自己处理所有自己需要的效果。

    GUIStyle还可以基于已经存在的实例new一个新的实例,这样,只需对原有的效果中不符合自己需求的进行修改。

    就像这样:

    GUIStyle textStyle = new GUIStyle("HeaderLabel");
    textStyle.fontSize = 20;

    一个基于 HeaderLabel  的字体显示风格,然后把字号放大成20;

    然后就可以用这个风格来编制自己的编辑器,如下,文本“示例”二字会按上面定义的风格显示出来。

    GUILayout.Label("示例", textStyle, GUILayout.Width(300));

    Unity编辑器中,按钮,文本,开关等等大部分Layout都可以传入GUIStyle参数,就不多说了。

    那么,到底怎么获得这些系统内置的样式的?

    答案是:GUI.skin.customStyles !遍历这个数组,里面有大量的系统样式,稍作修改,基本就能有不错的效果啦。

    下面,附上一个预览这些样式的方法。

    首先,给出AssetStore上的资源地址:https://assetstore.unity.com/packages/tools/gui/editor-style-viewer-3282

    源代码是js写的,不太习惯。我稍稍修改了一下,改成C#的了。

     1 using System.Collections;
     2 using System.Collections.Generic;
     3 using UnityEngine;
     4 using UnityEditor;
     5 
     6 public class GUIStyleViewer : EditorWindow {
     7 
     8     Vector2 scrollPosition = new Vector2(0,0);
     9     string search = "";
    10     GUIStyle textStyle;
    11 
    12 
    13     private static GUIStyleViewer window;
    14     [MenuItem("Tools/GUIStyleViewer", false, 100)]
    15     private static void OpenStyleViewer()
    16     {
    17         window = GetWindow<GUIStyleViewer>(false, "查看内置GUIStyle");
    18     }
    19 
    20     void OnGUI()
    21     {
    22         if (textStyle == null)
    23         {
    24             textStyle = new GUIStyle("HeaderLabel");
    25             textStyle.fontSize = 20;
    26         }
    27 
    28         GUILayout.BeginHorizontal("HelpBox");
    29         GUILayout.Label("点击示例,可以将其名字复制下来", textStyle);
    30         GUILayout.FlexibleSpace();
    31         GUILayout.Label("Search:");
    32         search = EditorGUILayout.TextField(search);
    33         GUILayout.EndHorizontal();
    34 
    35         GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
    36         GUILayout.Label("示例", textStyle, GUILayout.Width(300));
    37         GUILayout.Label("名字", textStyle, GUILayout.Width(300));
    38         GUILayout.EndHorizontal();
    39 
    40 
    41         scrollPosition = GUILayout.BeginScrollView(scrollPosition);
    42 
    43         foreach (var style in GUI.skin.customStyles)
    44         {
    45             if (style.name.ToLower().Contains(search.ToLower()))
    46             {
    47                 GUILayout.Space(15);
    48                 GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
    49                 if (GUILayout.Button(style.name, style, GUILayout.Width(300)))
    50                 {
    51                     EditorGUIUtility.systemCopyBuffer = style.name ;
    52                     Debug.LogError(style.name);
    53                 }
    54                 EditorGUILayout.SelectableLabel(style.name, GUILayout.Width(300));
    55                 GUILayout.EndHorizontal();
    56             }
    57         }
    58         
    59         GUILayout.EndScrollView();
    60     }
    61 }
    系统GUIStyle预览

    效果如下:

  • 相关阅读:
    WDF驱动中KMDF与UMDF区别
    GTD190018:【翻译】The Case Against Civilization
    GTD190017:【翻译】Transformer: A Novel Neural Network Architecture for Language Understanding
    GTD180016:【翻译】Software Engineering ≠ Computer Science
    GTD180015:【学习】一文看尽深度学习RNN:为啥就它适合语音识别、NLP与机器翻译?
    GTD180014:【翻译】Combinatory_logic
    GTD180013:【翻译】Improving the Realism of Synthetic Images
    GTD180012:【翻译】Visualizing Algorithms
    GTD180011:【翻译】The future of deep learning
    GitHub180001:【翻译】Using 3D Convolutional Neural Networks for Speaker Verification
  • 原文地址:https://www.cnblogs.com/yougoo/p/10073086.html
Copyright © 2011-2022 走看看