Skip to content

Instantly share code, notes, and snippets.

@sunny352
Created April 8, 2018 10:56
Show Gist options
  • Save sunny352/96ddbdd5fc5ebf9ad7a2267d74119109 to your computer and use it in GitHub Desktop.
Save sunny352/96ddbdd5fc5ebf9ad7a2267d74119109 to your computer and use it in GitHub Desktop.
Show unity built-in images
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class BuiltInImagesWindow : EditorWindow
{
[MenuItem("Window/Built-in Images")]
public static void ShowWindow()
{
GetWindow<BuiltInImagesWindow>().Show();
}
private string[] resourcePaths;
private void OnEnable()
{
var type = typeof(EditorGUIUtility);
var methodInfo = type.GetMethod("GetEditorAssetBundle", BindingFlags.Static | BindingFlags.NonPublic);
var result = methodInfo.Invoke(methodInfo, null) as AssetBundle;
//Only png & jpg
resourcePaths = result.GetAllAssetNames()
.Where(item => Path.GetExtension(item) == ".png"
|| Path.GetExtension(item) == ".jpg").ToArray();
}
private float _scrollbarPos;
private const float GridWidth = 64.0f;
private const float GridHeight = 64.0f;
private const float ScrollbarWidth = 16.0f;
private void OnGUI()
{
var windowRect = position;
GUILayout.BeginVertical();
var height = 0.0f;
var realHeight = 0.0f;
var widthCount = (int) (windowRect.width / GridWidth);
var start = resourcePaths.Length / widthCount * GridHeight * _scrollbarPos / 100.0f;
for (var index = 0; index < resourcePaths.Length;)
{
realHeight += GridHeight;
if (realHeight < start)
{
index += widthCount;
continue;
}
if (height + GridHeight > windowRect.height)
{
break;
}
GUILayout.BeginHorizontal();
var width = 0.0f;
for (var innerIndex = 0; index < resourcePaths.Length && innerIndex < widthCount; index++, innerIndex++)
{
if (width + GridWidth > windowRect.width - ScrollbarWidth)
{
break;
}
var resourcePath = resourcePaths[index];
var texture = EditorGUIUtility.Load(resourcePath) as Texture2D;
if (GUI.Button(new Rect(width, height, GridWidth, GridHeight), texture))
{
Debug.Log(resourcePath);
GUIUtility.systemCopyBuffer = resourcePath;
}
width += GridWidth;
}
GUILayout.EndHorizontal();
height += GridHeight;
}
GUILayout.EndVertical();
_scrollbarPos = GUI.VerticalScrollbar(
new Rect(windowRect.width - ScrollbarWidth, 0, ScrollbarWidth, windowRect.height), _scrollbarPos,
1.0f, 0.0f, 100.0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment