Skip to content

Instantly share code, notes, and snippets.

@brunocoimbrar
Last active February 8, 2018 10:30
Show Gist options
  • Save brunocoimbrar/b22496c3894b8ad6e53b4698d184334d to your computer and use it in GitHub Desktop.
Save brunocoimbrar/b22496c3894b8ad6e53b4698d184334d to your computer and use it in GitHub Desktop.
Useful script to display GUI resources available by default.
// Adapted from http://wiki.unity3d.com/index.php/Show_Built_In_Resources
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace PainfulSmile
{
public sealed class GUIResourcesWindow : EditorWindow
{
private enum ResourcesType
{
Styles,
Icons
}
private enum SizeType
{
Original,
Expanded
}
private bool _forceExpand;
private int _iconIndex;
private int _styleIndex;
private string _search = "";
private ResourcesType _resourcesType;
private SizeType _sizeType;
private Vector2 _iconsScrollPosition;
private Vector2 _stylesScrollPosition;
private Texture2D[] _icons;
[MenuItem("Tools/GUI Resources")]
private static void OpenMenuItem()
{
List<Texture2D> __listIcons = new List<Texture2D>(Resources.FindObjectsOfTypeAll(typeof(Texture2D)) as Texture2D[]);
__listIcons.Sort((iconA, iconB) => string.Compare(iconA.name, iconB.name, System.StringComparison.OrdinalIgnoreCase));
GUIResourcesWindow __window = GetWindow<GUIResourcesWindow>(false, "GUI Resources");
__window._icons = __listIcons.ToArray();
__window.minSize = new Vector2(512, 256);
__window.Show();
}
private void OnGUI()
{
GUILayout.Space(4);
using (new GUILayout.HorizontalScope())
{
EditorGUIUtility.labelWidth = 64;
_resourcesType = (ResourcesType)EditorGUILayout.EnumPopup("Resources", _resourcesType);
EditorGUIUtility.labelWidth = 48;
_sizeType = (SizeType)EditorGUILayout.EnumPopup("Size", _sizeType);
}
GUILayout.Space(4);
_search = EditorGUILayout.TextField(_search);
GUILayout.Space(4);
Rect __buttonRect = new Rect();
__buttonRect.x = (position.width / 2) + 8;
__buttonRect.width = (position.width / 2) - 12;
__buttonRect.height = GUI.skin.button.CalcSize(new GUIContent("Copy")).y;
__buttonRect.y = position.height - __buttonRect.height - 4;
switch (_resourcesType)
{
case ResourcesType.Styles:
CheckKeyEvent(ref _styleIndex, ref _stylesScrollPosition, __buttonRect.height, GUI.skin.customStyles.Length);
using (new GUILayout.HorizontalScope())
{
using (new GUILayout.VerticalScope(GUILayout.Width(position.width / 2)))
{
using (var scrollScope = new GUILayout.ScrollViewScope(_stylesScrollPosition, GUI.skin.textField))
{
_stylesScrollPosition = scrollScope.scrollPosition;
for (int i = 0; i < GUI.skin.customStyles.Length; i++)
{
if (_search != "" && !GUI.skin.customStyles[i].name.ToLower().Contains(_search.ToLower()))
{
continue;
}
if (GUILayout.Toggle(_styleIndex == i, GUI.skin.customStyles[i].name, EditorStyles.toolbarButton))
{
_styleIndex = i;
}
}
}
}
Rect __styleRect = new Rect();
__styleRect.x = (position.width / 2) + 48;
__styleRect.y = 48;
__styleRect.width = (position.width / 2) - 52;
switch (_sizeType)
{
case SizeType.Original:
__styleRect.height = GUI.skin.customStyles[_styleIndex].CalcHeight(new GUIContent("Style Label"), __styleRect.width);
break;
case SizeType.Expanded:
__styleRect.height = position.height - __buttonRect.height - __styleRect.y - 8;
break;
}
EditorGUI.LabelField(__styleRect, "Style Label", GUI.skin.customStyles[_styleIndex]);
if (GUI.Button(__buttonRect, "Copy"))
{
CopyText("new GUIStyle(\"" + GUI.skin.customStyles[_styleIndex].name + "\")");
}
}
break;
case ResourcesType.Icons:
CheckKeyEvent(ref _iconIndex, ref _iconsScrollPosition, __buttonRect.height, _icons.Length);
using (new GUILayout.HorizontalScope())
{
using (new GUILayout.VerticalScope(GUILayout.Width(position.width / 2)))
{
using (var scrollScope = new GUILayout.ScrollViewScope(_iconsScrollPosition, GUI.skin.textField))
{
_iconsScrollPosition = scrollScope.scrollPosition;
for (int i = 0; i < _icons.Length; i++)
{
if (string.IsNullOrEmpty(_icons[i].name) || (_search != "" && !_icons[i].name.ToLower().Contains(_search.ToLower())))
{
continue;
}
if (GUILayout.Toggle(_iconIndex == i, new GUIContent(_icons[i].name), EditorStyles.toolbarButton))
{
_iconIndex = i;
}
}
}
}
Rect __textureRect = new Rect();
switch (_sizeType)
{
case SizeType.Original:
__textureRect = GUILayoutUtility.GetRect(_icons[_iconIndex].width, _icons[_iconIndex].width, _icons[_iconIndex].height, _icons[_iconIndex].height, GUILayout.ExpandHeight(false), GUILayout.ExpandWidth(false));
__textureRect.x += (position.width / 4) - (__textureRect.width / 2);
__textureRect.y = 12 + (position.height / 2) - (__textureRect.height / 2);
break;
case SizeType.Expanded:
__textureRect.x = (position.width / 2) + 8;
__textureRect.y = 48;
__textureRect.width = (position.width / 2) - 12;
__textureRect.height = position.height - __buttonRect.height - __textureRect.y - 8;
break;
}
EditorGUI.DrawTextureTransparent(__textureRect, _icons[_iconIndex]);
if (GUI.Button(__buttonRect, "Copy"))
{
CopyText("EditorGUIUtility.FindTexture(\"" + _icons[_iconIndex].name + "\")");
}
}
break;
}
GUILayout.Space(4);
}
private static void CopyText(string text)
{
TextEditor __editor = new TextEditor();
__editor.text = text;
__editor.SelectAll();
__editor.Copy();
}
private void CheckKeyEvent(ref int index, ref Vector2 scrollPosition, float itemHeight, int itemCount)
{
if (Event.current != null && Event.current.type == EventType.KeyDown)
{
switch (Event.current.keyCode)
{
case KeyCode.UpArrow:
index = Mathf.Max(index - 1, 0);
if (index * itemHeight < scrollPosition.y)
{
scrollPosition.y -= itemHeight;
}
Repaint();
break;
case KeyCode.DownArrow:
index = Mathf.Min(index + 1, itemCount - 1);
if (index * itemHeight > scrollPosition.y + position.height - itemHeight - 48)
{
scrollPosition.y += itemHeight;
}
Repaint();
break;
case KeyCode.Home:
index = 0;
scrollPosition.y = 0;
Repaint();
break;
case KeyCode.End:
index = itemCount - 1;
scrollPosition.y = (itemHeight * itemCount) - ((position.height - 48) / itemHeight);
Repaint();
break;
case KeyCode.PageUp:
{
int __previousIndex = index;
index = Mathf.Max(index - Mathf.RoundToInt((position.height - 48) / itemHeight), 0);
scrollPosition.y += (index - __previousIndex) * itemHeight;
}
Repaint();
break;
case KeyCode.PageDown:
{
int __previousIndex = index;
index = Mathf.Min(index + Mathf.RoundToInt((position.height - 48) / itemHeight), itemCount - 1);
scrollPosition.y += (index - __previousIndex) * itemHeight;
}
Repaint();
break;
default:
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment