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預覽

 

效果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM