Advertisement
Muk99

StylesViewer

Jul 31st, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.99 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using UnityEditor;
  5. using UnityEditorInternal;
  6. using UnityEngine;
  7.  
  8. internal sealed class EditorStyleViewer : EditorWindow {
  9.  
  10.     private string search = string.Empty;
  11.     private Vector2 scroll = Vector2.zero;
  12.     private GUIStyle selectedStyle = null;
  13.  
  14.     private Color32 selectedColor {
  15.         get {
  16.             if(EditorGUIUtility.isProSkin)
  17.                 return new Color32(62, 95, 150, 255);
  18.             else
  19.                 return new Color32(62, 125, 231, 255);
  20.         }
  21.     }
  22.  
  23.     [MenuItem("Window/Editor Styles Viewer")]
  24.     private static void Init() {
  25.         var window = GetWindow<EditorStyleViewer>();
  26.  
  27. #if UNITY_5_0
  28.         window.title = "Styles Viewer";
  29. #else
  30.         window.titleContent = new GUIContent("Styles Viewer");
  31. #endif
  32.         window.Show();
  33.     }
  34.  
  35.     private void OnGUI() {
  36.         EditorGUILayout.BeginHorizontal("toolbar");
  37.  
  38.         //GUI.enabled = InternalEditorUtility.HasPro();
  39.         if(GUILayout.Button(EditorGUIUtility.isProSkin ? "White Skin" : "Dark Skin", "toolbarbutton"))
  40.             InternalEditorUtility.SwitchSkinAndRepaintAllViews();
  41.         GUI.enabled = true;
  42.  
  43.         if(GUILayout.Button("Generate Styles Class", "toolbarbutton"))
  44.             GenerateClassFile();
  45.  
  46.         GUILayout.FlexibleSpace();
  47.  
  48.         search = SearchBar(search);
  49.  
  50.         EditorGUILayout.EndHorizontal();
  51.  
  52.         scroll = EditorGUILayout.BeginScrollView(scroll, false, false);
  53.  
  54.         for(int i = 0; i < GUI.skin.customStyles.Length; i++) {
  55.             var style = GUI.skin.customStyles[i];
  56.  
  57.             if(style.name.ToLower().Contains(search.ToLower()))
  58.                 DrawStyle(style);
  59.         }
  60.         EditorGUILayout.EndScrollView();
  61.     }
  62.  
  63.     private string GetHighlightedName(GUIStyle style) {
  64.         var name = style.name;
  65.  
  66.         if(string.IsNullOrEmpty(search) || !name.ToLower().Contains(search.ToLower()))
  67.             return name;
  68.  
  69.         var startIndex = name.ToLower().IndexOf(search.ToLower());
  70.         var endIndex = startIndex + search.Length;
  71.  
  72.         return name.Insert(endIndex, "</b>").Insert(startIndex, "<b>");
  73.     }
  74.  
  75.     private void DrawStyle(GUIStyle style) {
  76.         EditorGUILayout.BeginHorizontal("EyedropperHorizontalLine");
  77.         if(GUILayout.Button(style.name, style))
  78.             EditorGUIUtility.systemCopyBuffer = style.name;
  79.         GUILayout.FlexibleSpace();
  80.         EditorGUILayout.SelectableLabel(GetHighlightedName(style), "IN Label");
  81.         EditorGUILayout.EndHorizontal();
  82.     }
  83.  
  84.     private bool SelectedStyle(GUIStyle style, Rect rect) {
  85.  
  86.         var evt = Event.current;
  87.         if(evt.type == EventType.MouseDown && rect.Contains(evt.mousePosition)) {
  88.             selectedStyle = style;
  89.             evt.Use();
  90.         }
  91.  
  92.         var value = style == selectedStyle;
  93.  
  94.         if(value)
  95.             EditorGUI.DrawRect(rect, selectedColor);
  96.  
  97.         return value;
  98.     }
  99.  
  100.     private static string SearchBar(string search, string[] searchModes, ref int searchMode, params GUILayoutOption[] options) {
  101.         var paramsTypes = new Type[] { typeof(string), typeof(string[]), typeof(int), typeof(GUILayoutOption[]) };
  102.         var methodName = "ToolbarSearchField";
  103.         var flags = BindingFlags.Static | BindingFlags.NonPublic;
  104.         var type = typeof(EditorGUILayout);
  105.         var parameters = new object[] { search, searchModes, searchMode, options };
  106.         var method = type.GetMethod(methodName, flags, null, paramsTypes, null);
  107.         var result = method.Invoke(null, parameters);
  108.         return (string)result;
  109.     }
  110.  
  111.     private static string SearchBar(string search, params GUILayoutOption[] options) {
  112.         var paramsTypes = new Type[] { typeof(string), typeof(GUILayoutOption[]) };
  113.         var methodName = "ToolbarSearchField";
  114.         var flags = BindingFlags.Static | BindingFlags.NonPublic;
  115.         var type = typeof(EditorGUILayout);
  116.         var parameters = new object[] { search, options };
  117.         var method = type.GetMethod(methodName, flags, null, paramsTypes, null);
  118.         var result = method.Invoke(null, parameters);
  119.         return (string)result;
  120.     }
  121.  
  122.     public static void GenerateClassFile() {
  123.         var path = EditorUtility.SaveFilePanel("Save Styles Class", "Assets", "Styles", "cs");
  124.  
  125.         if(path.Length == 0)
  126.             return;
  127.  
  128.         if(File.Exists(path))
  129.             File.Delete(path);
  130.  
  131.         try {
  132.             using(var file = new StreamWriter(path)) {
  133.                 file.WriteLine("using UnityEngine;");
  134.                 file.WriteLine("");
  135.                 file.WriteLine("//Automatic Generated;");
  136.                 file.WriteLine("#if UNITY_EDITOR");
  137.                 file.WriteLine("public static class CustomStyles {");
  138.                 file.WriteLine("\tpublic static GUIStyle[] allStyles { get { return GUI.skin.customStyles; } }");
  139.                 file.WriteLine("");
  140.  
  141.                 var line = "\tpublic static GUIStyle {0} {{ get {{ return \"{1}\"; }} }}";
  142.                 //var line = "\tpublic static GUIStyle {0} {{ get {{ return new GUIStyle(\"{1}\"); }} }}";
  143.                 //var line = "\tpublic static GUIStyle {0} = \"{1}\";";
  144.  
  145.                 foreach(var style in GUI.skin.customStyles) {
  146.                     var chars = style.name.Replace(".", "")
  147.                                           .Replace(",", "")
  148.                                           .Replace(" ", "")
  149.                                           .Replace("_", "").ToCharArray();
  150.  
  151.                     if(char.ToUpper(chars[1]) != chars[1])
  152.                         chars[0] = char.ToLower(chars[0]);
  153.  
  154.                     file.WriteLine(line, new string(chars), style.name);
  155.                 }
  156.  
  157.                 file.WriteLine("}");
  158.                 file.WriteLine("#endif");
  159.             }
  160.         }
  161.         catch(Exception e) {
  162.             if(File.Exists(path))
  163.                 File.Delete(path);
  164.  
  165.             Debug.LogException(e);
  166.         }
  167.  
  168.         AssetDatabase.Refresh();
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement