在進行編輯器擴展時,建立組件的方法通常都會提供GUIStyle參數,能夠讓咱們自定義樣式。修改背景圖,字體大小,顏色等等。數組
好比,建立Button組件的方法:public static bool Button(string text, GUIStyle style, params GUILayoutOption[] options);編輯器
建立時能夠傳遞一個GUIStyle參數來自定義樣式。工具
在GUI.skin.customStyles數組中,unity定義了不少自帶的樣式。有的需求能夠直接在這些樣式裏面來找符合需求的樣式。怎麼找呢?寫個工具:字體
using UnityEngine; using UnityEditor; public class GUIStyleViewer : EditorWindow { private Vector2 scrollVector2 = Vector2.zero; private string search = ""; [MenuItem("UFramework/GUIStyle查看器")] public static void InitWindow() { EditorWindow.GetWindow(typeof(GUIStyleViewer)); } void OnGUI() { GUILayout.BeginHorizontal("HelpBox"); GUILayout.Space(30); search = EditorGUILayout.TextField("", search, "SearchTextField", GUILayout.MaxWidth(position.x / 3)); GUILayout.Label("", "SearchCancelButtonEmpty"); GUILayout.EndHorizontal(); scrollVector2 = GUILayout.BeginScrollView(scrollVector2); foreach (GUIStyle style in GUI.skin.customStyles) { if (style.name.ToLower().Contains(search.ToLower())) { DrawStyleItem(style); } } GUILayout.EndScrollView(); } void DrawStyleItem(GUIStyle style) { GUILayout.BeginHorizontal("box"); GUILayout.Space(40); EditorGUILayout.SelectableLabel(style.name); GUILayout.FlexibleSpace(); EditorGUILayout.SelectableLabel(style.name, style); GUILayout.Space(40); EditorGUILayout.SelectableLabel("", style, GUILayout.Height(40), GUILayout.Width(40)); GUILayout.Space(50); if (GUILayout.Button("複製GUIStyle名字")) { TextEditor textEditor = new TextEditor(); textEditor.text = style.name; textEditor.OnFocus(); textEditor.Copy(); } GUILayout.EndHorizontal(); GUILayout.Space(10); } }
點擊菜單欄UFramework->GUIStyle查看器:spa
在裏面尋找咱們想要的樣式便可。code
咱們這樣建立一個button按鈕:blog
if (GUILayout.Button("Button", GUILayout.Width(100), GUILayout.Height(30))) { OnClickButton(); }
這樣不傳GUIStyle樣式參數就是使用默認樣式,以下:string
如今咱們的需求是點擊後,把Button按鈕設置位不可點擊的樣式,咱們在GUIStyle查看器裏尋址咱們須要的樣式,it
看到ObjectPickerBackground跟咱們須要的樣式差很少,決定使用這個樣式io
怎麼使用這個樣式呢?
點擊複製GUIStyle名字按鈕,會把該GUIStyle樣式的名字 "ObjectPickerBackground" 複製到剪切板。而後在unity中用該名字定義GUIStyle:
GUIStyle styleApply = new GUIStyle("ObjectPickerBackground"); //直接用樣式名定義 styleApply.alignment = TextAnchor.MiddleCenter; //該樣式文字偏左了,咱們想讓它居中對齊 if (GUILayout.Button("Button", styleApply, GUILayout.Width(100), GUILayout.Height(30))) { OnClickButton(); }
好了,Button的樣式就變成這樣了