Unity の組み込みアイコン

Unity ツールを作成する場合、機能性も重要ですが、見た目の美しさも重要です。したがって、Unity の組み込みアイコンを使用する方が快適です。

そこで問題は、Unity の組み込みアイコンをどうやって入手するかということです。

Yusong MOMO さんがブログを書き、エディタ DLL を逆コンパイルし、正規表現を使用してアイコン情報を抽出しました。しかし、逆コンパイルを見たことがない場合はどうすればよいでしょうか? [ユソンMOMO:彼のアイコン]

実は他の方法もあります。Resources の FindObjectsOfTypeAll を直接使用して、最初に確認することができます。

/*
 * 查看所有 Textures
 * 
 * 
 * 
 */


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class UnityIconsWindow : EditorWindow
{
    
    

    [MenuItem(("JJJ/Unity Textures Window"))]
    static void Init()
    {
    
    
        UnityIconsWindow window = EditorWindow.GetWindow<UnityIconsWindow>("Unity Textures Window");
        //加载所有Texture2D
        window.textures = Resources.FindObjectsOfTypeAll<Texture2D>();
        window.minSize = new Vector2(200, 400);

        //用于GUI
        window.fullRect = new Rect();
        foreach (var texture in window.textures)
        {
    
    
            window.fullRect.height += texture.height + 15 + 5 + 10;
        }
    }

    Vector2 scrollPos;

    Texture2D[] textures;

    Rect fullRect;

    void OnGUI()
    {
    
    
        Rect scrollrect = new Rect();
        scrollrect.width = position.width;
        scrollrect.height = position.height;

        scrollPos = GUI.BeginScrollView(scrollrect, scrollPos, fullRect);

        Rect r = new Rect();
        r.x = 10;

        Rect box = new Rect();
        box.x = 7;
        box.width = position.width - 30;

        foreach (Texture2D texture in textures)
        {
    
    
            box.y = r.y;
            box.height = 15 + 5 + texture.height + 2;
            GUI.Box(box, "");

            r.height = 15;
            r.width = position.width;
            //绘制名字
            GUI.Label(r, texture.name);
            r.y += r.height;

            r.y += 5;

            r.height = texture.height;
            r.width = texture.width;
            //绘制图标
            GUI.DrawTexture(r, texture);
            r.y += texture.height;

            r.y += 10;
        }
        GUI.EndScrollView();
    }
}

次に、これまでに見たことのあるアイコンがたくさん表示されます。
プロジェクト独自のテクスチャも読み込まれるため、新しい空のプロジェクトを作成することをお勧めします。

ここに画像の説明を挿入します

しかし、いくつかの問題が見つかりました。一部のテクスチャ名のフィールドが空ですか?

ここに画像の説明を挿入します
これで大丈夫でしょうか?でも大丈夫、大きな問題ではありません、私たちはそれらを使用しません。

さて、これらの画像が表示されたので、ツールでどのように使用すればよいでしょうか? 使用するたびにロードして、それを横断して同じ名前のものを選択することはできませんよね?

実際、Unity には名前で特定のアイコンを取得する API があります。

	public static GUIContent IconContent (string name, string text= null);
	
	//name	所需图标的名称。
	//text	悬停在图标上的工具提示。

	//从具有给定名称的 Unity 内置资源中获取 GUIContent。
	//EditorGUIUtility.IconContent 用于为 GUI 元素创建 GUIContent。 
	//只会加载图标.通常情况下,将使用第一个参数获取 Assets/Editor Default 	Resources/Icons 中的图标。 
	//只需要图标的名称,而不需要 png 扩展名。 \ 第二个参数为悬停工具提示提供文本。
	//此字符串 需要以竖线“|”字符开头以将其标记为工具提示。
	//注意:目前无法悬停定位在工具提示上方。

GUIContent を作成するには、この API を使用するだけです。テクスチャだけが必要な場合はどうすればよいでしょうか? それもとても簡単なことなのですが、

     Texture texture = EditorGUIUtility.IconContent("IconName").image;

しかし実際には、この関数ですべてのアイコンを取得できるわけではなく、アセット/エディタのデフォルト リソース/アイコンにあるアイコンのみがこの関数で取得できます。

上記のスクリプトを変更してみましょう。

  /*
 * 查看所有 Textures
 * 
 * 
 * 
 */


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class UnityIconsWindow : EditorWindow
{
    
    

    [MenuItem(("JJJ/Unity Textures Window"))]
    static void Init()
    {
    
    
        UnityIconsWindow window = EditorWindow.GetWindow<UnityIconsWindow>("Unity Textures Window");
        //加载所有Texture2D
        window.textures = Resources.FindObjectsOfTypeAll<Texture2D>();
        window.minSize = new Vector2(200, 400);


        //可见
        window.textures2 = new Texture2D[window.textures.Length];
        for (int i = 0; i < window.textures2.Length; i++)
        {
    
    
            window.textures2[i] = EditorGUIUtility.IconContent(window.textures[i].name).image as Texture2D;
        }


        //用于GUI
        window.fullRect = new Rect();
        //foreach (var texture in window.textures)
        //{
    
    
        //    window.fullRect.height += texture.height + 15 + 5 + 10;
        //}
        for (int i = 0; i < window.textures2.Length; i++)
        {
    
    
            if (window.textures2[i] != null)
            {
    
    
                window.fullRect.height += window.textures2[i].height + 15 + 5 + 10;
            }
        }
    }

    Vector2 scrollPos;

    Texture2D[] textures;

    Texture2D[] textures2;

    Rect fullRect;

    void OnGUI()
    {
    
    
        Rect scrollrect = new Rect();
        scrollrect.width = position.width;
        scrollrect.height = position.height;

        scrollPos = GUI.BeginScrollView(scrollrect, scrollPos, fullRect);

        Rect r = new Rect();
        r.x = 10;

        Rect box = new Rect();
        box.x = 7;
        box.width = position.width - 30;

        foreach (Texture2D texture in textures2)
        {
    
    
            if (texture == null) continue;

            box.y = r.y;
            box.height = 15 + 5 + texture.height + 2;
            GUI.Box(box, "");

            r.height = 15;
            r.width = position.width;
            //绘制名字
            GUI.Label(r, texture.name);
            r.y += r.height;

            r.y += 5;

            r.height = texture.height;
            r.width = texture.width;
            //绘制图标
            GUI.DrawTexture(r, texture);
            r.y += texture.height;

            r.y += 10;
        }
        GUI.EndScrollView();
    }

    private void OnDisable()
    {
    
    
        textures = null;
    }
}

ウィンドウを開く:

ここに画像の説明を挿入します

エラーレポートからも明らかですが、画像のプロジェクトへのフルパス(拡張子を含む)を指定するか、 Assets/Editor Default Resources/Iconsのアイコン名(拡張子なし)を指定する必要があります

次に、まずエラーを非表示にしてから、完全なプロジェクト パスを指定してみます。

/*
 * 查看所有 Textures
 * 
 * 
 * 
 */


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class UnityIconsWindow : EditorWindow
{
    
    

    [MenuItem(("JJJ/Unity Textures Window"))]
    static void Init()
    {
    
    
        UnityIconsWindow window = EditorWindow.GetWindow<UnityIconsWindow>("Unity Textures Window");
        //加载所有Texture2D
        window.textures = Resources.FindObjectsOfTypeAll<Texture2D>();
        window.minSize = new Vector2(200, 400);


        //可见
        window.textures2 = new Texture2D[window.textures.Length];
        Debug.unityLogger.logEnabled = false;//关掉那个报错,不用管它
        for (int i = 0; i < window.textures2.Length; i++)
        {
    
    
            if (window.textures[i].name == "微信图片_20210808203939")//这样写很愚蠢,只是为了作为一个例子才这样写
                window.textures2[i] = EditorGUIUtility.IconContent(AssetDatabase.GetAssetPath(window.textures[i])).image as Texture2D;
            else
                window.textures2[i] = EditorGUIUtility.IconContent(window.textures[i].name).image as Texture2D;
        }
        Debug.unityLogger.logEnabled = true;

        //用于GUI
        window.fullRect = new Rect();
        //foreach (var texture in window.textures)
        //{
    
    
        //    window.fullRect.height += texture.height + 15 + 5 + 10;
        //}
        for (int i = 0; i < window.textures2.Length; i++)
        {
    
    
            if (window.textures2[i] != null)
            {
    
    
                window.fullRect.height += window.textures2[i].height + 15 + 5 + 10;
            }
        }
    }

    Vector2 scrollPos;

    Texture2D[] textures;

    Texture2D[] textures2;

    Rect fullRect;

    void OnGUI()
    {
    
    
        Rect scrollrect = new Rect();
        scrollrect.width = position.width;
        scrollrect.height = position.height;

        scrollPos = GUI.BeginScrollView(scrollrect, scrollPos, fullRect);

        Rect r = new Rect();
        r.x = 10;

        Rect box = new Rect();
        box.x = 7;
        box.width = position.width - 30;

        foreach (Texture2D texture in textures2)
        {
    
    
            if (texture == null) continue;

            box.y = r.y;
            box.height = 15 + 5 + texture.height + 2;
            GUI.Box(box, "");

            r.height = 15;
            r.width = position.width;
            //绘制名字
            GUI.Label(r, texture.name);
            r.y += r.height;

            r.y += 5;

            r.height = texture.height;
            r.width = texture.width;
            //绘制图标
            GUI.DrawTexture(r, texture);
            r.y += texture.height;

            r.y += 10;
        }
        GUI.EndScrollView();
    }

    private void OnDisable()
    {
    
    
        textures = null;
    }
}

もう一度ウィンドウを開きます。

ここに画像の説明を挿入します
完璧です、最初のものが見えます。

次に、そのようなツール クラスを用意することを検討し、アイコンが必要な場合はこのスクリプトを使用するだけで済みます。

using UnityEngine;
using UnityEditor;

public static class JIconUtility
{
    
    
    //比如需要一个文件夹图标,就可以这样写
    static Texture2D directoryIcon;
    public static Texture2D DirectoryIcon {
    
     get {
    
     if (directoryIcon == null) directoryIcon = EditorGUIUtility.IconContent("Folder Icon").image as Texture2D; return directoryIcon; } }

    //其他的你自定义的icon其实也可以放在这里,只不过不是用 EditorGUIUtility.IconContent() ,而是要直接 Resources.Load 加载
    static Texture2D myIcon;
    public static Texture2D MyIcon {
    
     get {
    
     if (myIcon == null) myIcon = Resources.Load<Texture2D>("MyIcon"/*自定义icon路径*/); return myIcon; } }
}

または、EditorGUIUtility.Load()またはEditorGUIUtility.LoadRequired()を使用して、組み込みリソースをロードすることもできます。

おすすめ

転載: blog.csdn.net/weixin_44293055/article/details/120443635