GUIStyleViewer

【代码】

  1 #if UNITY_EDITOR
  2 
  3 using System.Collections.Generic;
  4 using System.Reflection;
  5 using UnityEngine;
  6 using UnityEditor;
  7 
  8 public class GUIStyleViewer : EditorWindow
  9 {
 10     List<GUIStyle> _reflectStyles = null;
 11 
 12     Vector2 _scrollPosition = new Vector2(0, 0);
 13     GUIContent _tempContent = new GUIContent();
 14     string _search = "";
 15 
 16     int _pageIndex = 0;
 17     int _pageCount = 0;
 18     List<GUIStyle> _list = new List<GUIStyle>();
 19 
 20 
 21     [MenuItem("Window/myCustom/GUIStyleViewer", false, 100)]
 22     private static void OpenStyleViewer()
 23     {
 24         GUIStyleViewer window = GetWindow<GUIStyleViewer>(false, "查看内置GUIStyle");
 25     }
 26 
 27     void CheckReflectStyles()
 28     {
 29         if (null != _reflectStyles)
 30             return;
 31 
 32         _reflectStyles = new List<GUIStyle>();
 33         var Cls_GUIStyle = typeof(GUIStyle);
 34         foreach (var propInfo in typeof(EditorStyles).GetProperties(BindingFlags.Static | BindingFlags.Public |
 35                                                                     BindingFlags.NonPublic))
 36         {
 37             object o = propInfo.GetValue(null, null);
 38             if (o.GetType() == Cls_GUIStyle)
 39             {
 40                 _reflectStyles.Add(o as GUIStyle);
 41             }
 42         }
 43     }
 44 
 45     void OnGUI()
 46     {
 47         CheckReflectStyles();
 48 
 49         GUILayout.BeginHorizontal("HelpBox");
 50         {
 51             GUILayout.Label("点击示例,可以将其名字复制下来");
 52             GUILayout.FlexibleSpace();
 53             GUILayout.Label("Search:");
 54             _search = EditorGUILayout.TextField(_search);
 55         }
 56         GUILayout.EndHorizontal();
 57 
 58         GUILayout.Label("用法:");
 59         GUILayout.Label(" GUIStyle textStyle = new GUIStyle(\"HeaderLabel\");");
 60         GUILayout.Label(" textStyle.fontSize = 20;");
 61         GUILayout.Label(" GUILayout.Label(\"示例\", textStyle, GUILayout.Width(300));");
 62 
 63         Filter();
 64 
 65         _scrollPosition = GUILayout.BeginScrollView(_scrollPosition);
 66         for (var i = 0; i < 20; ++i)
 67         {
 68             var index = _pageIndex * 20 + i;
 69             if (index >= _list.Count)
 70                 break;
 71 
 72             var style = _list[index];
 73             GUILayout.BeginHorizontal();
 74             GUILayout.Label($"{index + 1}", GUILayout.ExpandWidth(false));
 75             _tempContent.tooltip = style.name;
 76             _tempContent.text = $"Sample Text";
 77             GUILayout.Label(_tempContent, style, GUILayout.ExpandWidth(true));
 78 
 79             _tempContent.text = $"Copy";
 80             if (GUILayout.Button(_tempContent, GUILayout.Width(60)))
 81             {
 82                 EditorGUIUtility.systemCopyBuffer = style.name;
 83                 Debug.Log("已复制 " + style.name);
 84             }
 85 
 86             GUILayout.EndHorizontal();
 87             GUILayout.Space(15);
 88         }
 89         GUILayout.EndScrollView();
 90 
 91         if (_pageCount > 1)
 92         {
 93             EditorGUILayout.BeginHorizontal();
 94 
 95             EditorGUILayout.LabelField($"{_pageIndex + 1}/{_pageCount}", GUILayout.MaxWidth(50));
 96             if (GUILayout.Button("上一页"))
 97             {
 98                 if (_pageIndex > 0)
 99                 {
100                     _pageIndex--;
101                     _scrollPosition.Set(0, 0);
102                 }
103             }
104             if (GUILayout.Button("下一页"))
105             {
106                 if (_pageIndex < _pageCount - 1)
107                 {
108                     _pageIndex++;
109                     _scrollPosition.Set(0, 0);
110                 }
111             }
112 
113             EditorGUILayout.EndHorizontal();
114             GUILayout.Space(5);
115         }
116     }
117 
118     void Filter()
119     {
120         _list.Clear();
121         if ("" == _search)
122         {
123             _list.AddRange(_reflectStyles);
124             _list.AddRange(GUI.skin.customStyles);
125         }
126         else
127         {
128             var searchLower = _search.ToLower();
129             foreach (var style in _reflectStyles)
130             {
131                 if (style.name.ToLower().Contains(searchLower))
132                 {
133                     _list.Add(style);
134                 }
135             }
136 
137             foreach (var style in GUI.skin.customStyles)
138             {
139                 if (style.name.ToLower().Contains(searchLower))
140                 {
141                     _list.Add(style);
142                 }
143             }
144         }
145 
146         _pageCount = Mathf.CeilToInt(_list.Count / 20.0f);
147         _pageIndex = Mathf.Max(0, Mathf.Min(_pageIndex, _pageCount - 1));
148     }
149 }
150 #endif

 

截图:

 

posted @ 2022-03-11 00:18  yanghui01  阅读(41)  评论(0编辑  收藏  举报