Qt+Opengl 模拟unity 引擎时,需要对应很多的显示icon。
一个个去找,比较麻烦,经查资料得知,可以通过如下脚本,获取对应icon
/********************************************************************
Copyright (C) 2020 STUPID DOG STUDIO
类 名:UnityIconWindow.cs
创建时间:2021-04-01 17:51:52
作 者:Birth.Fat
描 述:
版 本:2.0
*********************************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Xml;
using System.IO;
using System;
public class UnityIconsWindow : EditorWindow
{
GUIStyle m_textStyle;
GUIStyle m_saveBtnStyle;
GUIStyle m_iconNameStyle;
string m_search = "";
Vector2 m_scrollPosition = new Vector2(0, 0);
List<Texture2D> m_Icons;
TextEditor m_te = new TextEditor();
string sTagPath = "H:\\UnityIconsEx";
[MenuItem("Tools/UnityIcon")]
static void AddWindow()
{
Rect wr = new Rect(0, 0, 1600, 1000);
UnityIconsWindow window = (UnityIconsWindow)EditorWindow.GetWindowWithRect(typeof(UnityIconsWindow), wr, true, "Unity系统图标");
window.Show();
}
//------------------------------------------------------
void OnGUI()
{
SystemIconViewer();
}
//------------------------------------------------------
private void SystemIconViewer()
{
InitGUIStyle();
GUILayout.BeginHorizontal("HelpBox");
GUILayout.Label("结果如下:" + m_search, m_textStyle);
GUILayout.FlexibleSpace();
sTagPath = GUILayout.TextField(sTagPath);
if (GUILayout.Button("SHIT!", m_saveBtnStyle, GUILayout.Width(80)))
{
InitIconList();
}
if (GUILayout.Button("SaveIcon"))
{
Debug.Log($"Save icon {m_Icons.Count}");
SaveIcon();
}
GUILayout.EndHorizontal();
m_scrollPosition = GUILayout.BeginScrollView(m_scrollPosition);
if (m_Icons != null)
{
for (int i = 0; i < m_Icons.Count; i++)
{
if (i % 15 == 0)
{
GUILayout.BeginHorizontal("PopupCurveSwatchBackground");
for (int j = 0; j < 15; j++)
{
var icon = m_Icons[i];
GUILayout.BeginVertical();
if (GUILayout.Button((Texture)icon, GUILayout.Width(100), GUILayout.Height(100)))
{
m_te.text = icon.name;
m_te.SelectAll();
m_te.Copy();
this.ShowNotification(new GUIContent("Unity 体统不表:" + icon.name + "已经复制到剪切板!"));
}
GUILayout.Label(icon.name, m_iconNameStyle, GUILayout.Width(100));
GUILayout.EndVertical();
i++;
if (i == m_Icons.Count)
break;
}
GUILayout.EndHorizontal();
GUILayout.Space(6);
}
}
}
GUILayout.EndScrollView();
}
/// <summary>
/// 运行模式下Texture转换成Texture2D
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
private Texture2D TextureToTexture2D(Texture2D texture, string sName)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
try
{
byte[] textureData = texture2D.EncodeToPNG();
System.IO.File.WriteAllBytes(sName, textureData);
Debug.Log($"Save icon {sName}");
}
catch (Exception e)
{
Debug.Log($"Save icon {sName} exception {e.Message}");
}
return texture2D;
}
void SaveIcon()
{
string sName;
//for (int i = 0;i<m_szTexture.Length;i++)
for (int i = 0; i < m_Icons.Count; i++)
{
sName = m_Icons[i].name;
sName = $"{sTagPath}/{sName}.png";
TextureToTexture2D(m_Icons[i], sName);
//RenderTexture pTempRt = new RenderTexture()
//Texture2D pTexture = new Texture2D()
//try
//{
// byte[] textureData = m_szTexture[i].EncodeToPNG();
// System.IO.File.WriteAllBytes(sName, textureData);
// Debug.Log($"Save icon {sName}");
//}
//catch(Exception e)
//{
// Debug.Log($"Save icon {sName} exception {e.Message}");
//}
}
}
//------------------------------------------------------
private void InitIconList()
{
if (m_Icons == null || m_Icons.Count == 0)
{
m_Icons = new List<Texture2D>(Resources.FindObjectsOfTypeAll< Texture2D>());
m_Icons.Sort((pA, pB) => System.String.Compare(pA.name, pB.name, System.StringComparison.OrdinalIgnoreCase));
m_search = m_Icons.Count.ToString(); // GUILayout.Label(m_search)
}
}
//------------------------------------------------------
private void InitGUIStyle()
{
if (m_textStyle == null)
{
m_textStyle = new GUIStyle("HeaderLabel");
m_textStyle.fontSize = 20;
//color = textStyle.normal.textColor ;
}
if (m_iconNameStyle == null)
{
m_iconNameStyle = new GUIStyle("WarningOverlay");
m_iconNameStyle.fontSize = 12;
}
if (m_saveBtnStyle == null)
{
m_saveBtnStyle = new GUIStyle("flow node 1");
m_saveBtnStyle.fontSize = 16;
m_saveBtnStyle.fixedHeight = 38;
m_saveBtnStyle.alignment = TextAnchor.MiddleCenter;
m_saveBtnStyle.fontStyle = m_textStyle.fontStyle;
}
}
//------------------------------------------------------
private void Reset()
{
if (m_Icons == null)
return;
if (m_Icons.Count > 0)
m_Icons.Clear();
}
}
需要注意的是,系统Texture2D 直接使用EncodeToPNG 时,会失败,提示需要将贴图设置成可读写状态。但是系统资源无法设置(至少我没找到设置地方)。
参考之前处理过的,相机照射场景,获取初版地图的做法。通过RenderTexture的生成新的自创建的Texture2D 然后再 通过 EncodeToPNG 将数据导出,最后写入文件。
具体参考上述代码中的
TextureToTexture2D 函数。