Advertisement
Mikilo

Built-In Resources

May 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.24 KB | None | 0 0
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5.  
  6. public class BuiltInResourcesWindow : EditorWindow
  7. {
  8.     [MenuItem("Window/Built-in styles and icons")]
  9.     public static void ShowWindow()
  10.     {
  11.         BuiltInResourcesWindow w = (BuiltInResourcesWindow)EditorWindow.GetWindow<BuiltInResourcesWindow>();
  12.         w.Show();
  13.     }
  14.  
  15.     private struct Drawing
  16.     {
  17.         public Rect Rect;
  18.         public Action Draw;
  19.     }
  20.  
  21.     private List<Drawing> Drawings;
  22.  
  23.     private List<UnityEngine.Object> _objects;
  24.     private float _scrollPos;
  25.     private float _maxY;
  26.     private Rect _oldPosition;
  27.  
  28.     private bool _showingStyles = true;
  29.     private bool _showingIcons = false;
  30.  
  31.     private string _search = "";
  32.  
  33.     void OnGUI()
  34.     {
  35.         if (position.width != _oldPosition.width && Event.current.type == EventType.Layout)
  36.         {
  37.             Drawings = null;
  38.             _oldPosition = position;
  39.         }
  40.  
  41.         GUILayout.BeginHorizontal();
  42.  
  43.         if (GUILayout.Toggle(_showingStyles, "Styles", EditorStyles.toolbarButton) != _showingStyles)
  44.         {
  45.             _showingStyles = !_showingStyles;
  46.             _showingIcons = !_showingStyles;
  47.             Drawings = null;
  48.         }
  49.  
  50.         if (GUILayout.Toggle(_showingIcons, "Icons", EditorStyles.toolbarButton) != _showingIcons)
  51.         {
  52.             _showingIcons = !_showingIcons;
  53.             _showingStyles = !_showingIcons;
  54.             Drawings = null;
  55.         }
  56.  
  57.         GUILayout.EndHorizontal();
  58.  
  59.         string newSearch = GUILayout.TextField(_search);
  60.         if (newSearch != _search)
  61.         {
  62.             _search = newSearch;
  63.             Drawings = null;
  64.         }
  65.  
  66.         float top = 36;
  67.  
  68.         if (Drawings == null)
  69.         {
  70.             string lowerSearch = _search.ToLower();
  71.  
  72.             Drawings = new List<Drawing>();
  73.  
  74.             GUIContent inactiveText = new GUIContent("inactive");
  75.             GUIContent activeText = new GUIContent("active");
  76.  
  77.             float x = 5.0f;
  78.             float y = 5.0f;
  79.  
  80.             if (_showingStyles)
  81.             {
  82.                 foreach (GUIStyle ss in GUI.skin.customStyles)
  83.                 {
  84.                     if (lowerSearch != "" && !ss.name.ToLower().Contains(lowerSearch))
  85.                         continue;
  86.  
  87.                     GUIStyle thisStyle = ss;
  88.  
  89.                     Drawing draw = new Drawing();
  90.  
  91.                     float width = Mathf.Max(
  92.                         100.0f,
  93.                         GUI.skin.button.CalcSize(new GUIContent(ss.name)).x,
  94.                         ss.CalcSize(inactiveText).x + ss.CalcSize(activeText).x
  95.                                       ) + 16.0f;
  96.  
  97.                     float height = 60.0f;
  98.  
  99.                     if (x + width > position.width - 32 && x > 5.0f)
  100.                     {
  101.                         x = 5.0f;
  102.                         y += height + 10.0f;
  103.                     }
  104.  
  105.                     draw.Rect = new Rect(x, y, width, height);
  106.  
  107.                     width -= 8.0f;
  108.  
  109.                     draw.Draw = () =>
  110.                     {
  111.                         if (GUILayout.Button(thisStyle.name, GUILayout.Width(width)))
  112.                             CopyText("(GUIStyle)\"" + thisStyle.name + "\"");
  113.  
  114.                         GUILayout.BeginHorizontal();
  115.                         GUILayout.Toggle(false, inactiveText, thisStyle, GUILayout.Width(width / 2));
  116.                         GUILayout.Toggle(false, activeText, thisStyle, GUILayout.Width(width / 2));
  117.                         GUILayout.EndHorizontal();
  118.                     };
  119.  
  120.                     x += width + 18.0f;
  121.  
  122.                     Drawings.Add(draw);
  123.                 }
  124.             }
  125.             else if (_showingIcons)
  126.             {
  127.                 if (_objects == null)
  128.                 {
  129.                     _objects = new List<UnityEngine.Object>(Resources.FindObjectsOfTypeAll(typeof(Texture2D)));
  130.                     _objects.Sort((pA, pB) => System.String.Compare(pA.name, pB.name, System.StringComparison.OrdinalIgnoreCase));
  131.                 }
  132.  
  133.                 float rowHeight = 0.0f;
  134.  
  135.                 foreach (UnityEngine.Object oo in _objects)
  136.                 {
  137.                     Texture2D texture = (Texture2D)oo;
  138.  
  139.                     if (texture.name == "")
  140.                         continue;
  141.  
  142.                     if (lowerSearch != "" && !texture.name.ToLower().Contains(lowerSearch))
  143.                         continue;
  144.  
  145.                     Drawing draw = new Drawing();
  146.  
  147.                     float width = Mathf.Max(
  148.                         GUI.skin.button.CalcSize(new GUIContent(texture.name)).x,
  149.                         texture.width
  150.                     ) + 8.0f;
  151.  
  152.                     float height = texture.height + GUI.skin.button.CalcSize(new GUIContent(texture.name)).y + 8.0f;
  153.  
  154.                     if (x + width > position.width - 32.0f)
  155.                     {
  156.                         x = 5.0f;
  157.                         y += rowHeight + 8.0f;
  158.                         rowHeight = 0.0f;
  159.                     }
  160.  
  161.                     draw.Rect = new Rect(x, y, width, height);
  162.  
  163.                     rowHeight = Mathf.Max(rowHeight, height);
  164.  
  165.                     width -= 8.0f;
  166.  
  167.                     draw.Draw = () =>
  168.                     {
  169.                         if (GUILayout.Button(texture.name, GUILayout.Width(width)))
  170.                             CopyText("EditorGUIUtility.FindTexture( \"" + texture.name + "\" )");
  171.  
  172.                         Rect textureRect = GUILayoutUtility.GetRect(texture.width, texture.width, texture.height, texture.height, GUILayout.ExpandHeight(false), GUILayout.ExpandWidth(false));
  173.                         EditorGUI.DrawTextureTransparent(textureRect, texture);
  174.                     };
  175.  
  176.                     x += width + 8.0f;
  177.  
  178.                     Drawings.Add(draw);
  179.                 }
  180.             }
  181.  
  182.             _maxY = y;
  183.         }
  184.  
  185.         Rect r = position;
  186.         r.y = top;
  187.         r.height -= r.y;
  188.         r.x = r.width - 16;
  189.         r.width = 16;
  190.  
  191.         float areaHeight = position.height - top;
  192.         _scrollPos = GUI.VerticalScrollbar(r, _scrollPos, areaHeight, 0.0f, _maxY);
  193.  
  194.         Rect area = new Rect(0, top, position.width - 16.0f, areaHeight);
  195.         GUILayout.BeginArea(area);
  196.  
  197.         int count = 0;
  198.         foreach (Drawing draw in Drawings)
  199.         {
  200.             Rect newRect = draw.Rect;
  201.             newRect.y -= _scrollPos;
  202.  
  203.             if (newRect.y + newRect.height > 0 && newRect.y < areaHeight)
  204.             {
  205.                 GUILayout.BeginArea(newRect, GUI.skin.textField);
  206.                 draw.Draw();
  207.                 GUILayout.EndArea();
  208.  
  209.                 count++;
  210.             }
  211.         }
  212.  
  213.         GUILayout.EndArea();
  214.     }
  215.  
  216.     void CopyText(string pText)
  217.     {
  218.         TextEditor editor = new TextEditor();
  219.         editor.text = pText;
  220.         editor.SelectAll();
  221.         editor.Copy();
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement