简介: 最近写工具,想用一下 unity 内置的图标,在网上搜到的一段生成代码,修改了一下,做成了 UIElement 显示,记录一下作为备忘
unity版本:20222.3.0fc1
运行效果如下图,点即对应的图标即可复制icon名称到剪切板

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
public class ShowMeUnityIconsWindow : EditorWindow {
[MenuItem(("Window/ShowMeUnityIcons"))]
static void Init(){
EditorWindow.GetWindow<ShowMeUnityIconsWindow>("UnityTextureWindow");
}
Vector2 m_Scroll;
List<string> m_Icons = null;
void Awake(){
m_Icons = new List<string>();
Texture2D[] t = Resources.FindObjectsOfTypeAll<Texture2D>();
foreach(Texture2D x in t){
Debug.unityLogger.logEnabled = false;
GUIContent gc = EditorGUIUtility.IconContent(x.name);
Debug.unityLogger.logEnabled = true;
if(gc != null && gc.image != null){
m_Icons.Add(x.name);
}
}
Debug.Log(m_Icons.Count);
var scroll = new ScrollView(ScrollViewMode.Vertical);
rootVisualElement.Add(scroll);
scroll.StretchToParentSize();
var container = scroll.Q<VisualElement>("unity-content-container");
container.style.flexDirection = FlexDirection.Row;
container.style.flexWrap = Wrap.Wrap;
foreach (var icon in m_Icons)
{
var viewEle = new VisualElement();
viewEle.style.width = 200;
viewEle.style.flexDirection = FlexDirection.Row;
var btn = new Button();
btn.style.height = 30;
btn.style.width = 30;
btn.clicked += () =>
{
GUIUtility.systemCopyBuffer =icon;
ShowNotification(new GUIContent(icon+" copyed to clipboard"),1f);
};
var iconObj = EditorGUIUtility.IconContent(icon);
btn.style.backgroundImage = new StyleBackground(iconObj.image as Texture2D);
var label = new Label(icon);
label.style.width = new StyleLength(StyleKeyword.Auto);
label.style.height = 30;
viewEle.Add(btn);
viewEle.Add(label);
scroll.Add(viewEle);
}
}
}
网友评论