如图,在GUIIcon窗口列出所有Unity内置的图标:
实现:
首先,加载以上所有图片,将它们的名称存在一个.txt的文本中
代码语言:javascript复制[MenuItem("SKFramework/Editor/GUI/GUI Icon/Load GUIIcon")]
private static void LoadGUIIcon()
{
List<Texture> icons = new List<Texture>(Resources.FindObjectsOfTypeAll<Texture>());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < icons.Count; i )
{
sb.Append(icons[i].name);
if (i != icons.Count - 1)
{
sb.Append("r");
}
}
//找到GUIIcon脚本的路径 为了将txt文本存到相同的目录 也可以指定其他路径
string path = AssetDatabase.FindAssets(typeof(GUIIcon).Name)[0];
path = AssetDatabase.GUIDToAssetPath(path).Replace(".cs", ".txt");
File.WriteAllText(path, sb.ToString());
AssetDatabase.Refresh();
}
生成的文本:
需要注意的是,图标是通过Resources方式加载所有的图片,需要在一个空的工程中进行加载并生成文本,或者工程的Resources文件夹下没有其他贴图资源。
有了该文本后,即可通过这些图标名称进行预览。
最终实现:
代码语言:javascript复制using System.IO;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace SK.Framework
{
public sealed class GUIIcon : EditorWindow
{
[MenuItem("SKFramework/Editor/GUI/GUI Icon/Open GUIIcon")]
private static void OpenGUIIcon()
{
GetWindow<GUIIcon>().Show();
}
[MenuItem("SKFramework/Editor/GUI/GUI Icon/Load GUIIcon")]
private static void LoadGUIIcon()
{
List<Texture> icons = new List<Texture>(Resources.FindObjectsOfTypeAll<Texture>());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < icons.Count; i )
{
sb.Append(icons[i].name);
if (i != icons.Count - 1)
{
sb.Append("r");
}
}
//找到GUIIcon脚本 为了将txt文本存储到相同路径 也可以指定其他路径
string path = AssetDatabase.FindAssets(typeof(GUIIcon).Name)[0];
path = AssetDatabase.GUIDToAssetPath(path).Replace(".cs", ".txt");
File.WriteAllText(path, sb.ToString());
AssetDatabase.Refresh();
}
private Vector2 scrollPosition;
private string[] names;
private string searchContent = "";
private const float width = 50f;
private void OnGUI()
{
if (names == null)
{
//获取存储txt文本的路径
string path = AssetDatabase.FindAssets(typeof(GUIIcon).Name)[0];
path = AssetDatabase.GUIDToAssetPath(path).Replace(".cs", ".txt");
if (File.Exists(path))
{
string content = File.ReadAllText(path);
names = content.Split('r');
}
EditorGUILayout.HelpBox($"File {path} not exists.", MessageType.Error);
}
else
{
GUILayout.BeginHorizontal("Toolbar");
{
GUILayout.Label("Search:", GUILayout.Width(50));
searchContent = GUILayout.TextField(searchContent, "SearchTextField");
}
GUILayout.EndHorizontal();
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
{
List<string> matchNames = new List<string>();
for (int i = 0; i < names.Length; i )
{
if (names[i].ToLower().Contains(searchContent.ToLower()))
{
matchNames.Add(names[i]);
}
}
int count = Mathf.RoundToInt(position.width / (width 3f));
for (int i = 0; i < matchNames.Count; i = count)
{
GUILayout.BeginHorizontal();
for (int j = 0; j < count; j )
{
int index = i j;
if (index < matchNames.Count)
{
if (GUILayout.Button(EditorGUIUtility.IconContent(matchNames[index]), GUILayout.Width(width), GUILayout.Height(30)))
{
EditorGUIUtility.systemCopyBuffer = matchNames[index];
Debug.Log(matchNames[index]);
}
}
}
GUILayout.EndHorizontal();
}
}
GUILayout.EndScrollView();
}
}
}
}