通过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#的了。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; public class GUIStyleViewer : EditorWindow { Vector2 scrollPosition = new Vector2(,);
string search = "";
GUIStyle textStyle; private static GUIStyleViewer window;
[MenuItem("Tools/GUIStyleViewer", false, )]
private static void OpenStyleViewer()
{
window = GetWindow<GUIStyleViewer>(false, "查看内置GUIStyle");
} void OnGUI()
{
if (textStyle == null)
{
textStyle = new GUIStyle("HeaderLabel");
textStyle.fontSize = ;
} GUILayout.BeginHorizontal("HelpBox");
GUILayout.Label("点击示例,可以将其名字复制下来", textStyle);
GUILayout.FlexibleSpace();
GUILayout.Label("Search:");
search = EditorGUILayout.TextField(search);
GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
GUILayout.Label("示例", textStyle, GUILayout.Width());
GUILayout.Label("名字", textStyle, GUILayout.Width());
GUILayout.EndHorizontal(); scrollPosition = GUILayout.BeginScrollView(scrollPosition); foreach (var style in GUI.skin.customStyles)
{
if (style.name.ToLower().Contains(search.ToLower()))
{
GUILayout.Space();
GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
if (GUILayout.Button(style.name, style, GUILayout.Width()))
{
EditorGUIUtility.systemCopyBuffer = style.name ;
Debug.LogError(style.name);
}
EditorGUILayout.SelectableLabel(style.name, GUILayout.Width());
GUILayout.EndHorizontal();
}
} GUILayout.EndScrollView();
}
}
系统GUIStyle预览
效果如下: