Unity腳本修改游戲?qū)ο蟮膱D標(biāo)SelectIcon的方法

最近在做一個(gè)編輯器。需要修改這個(gè)。方便美術(shù)人員觀看。和編輯更方便。但是一直找不到API。在今天無聊翻翻谷歌。竟然翻到了。之前百度怎么都找不到
這里就分享一下了。


11

好了。不多說了。直接上代碼

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
 
/// <summary>
/// 對象Icon管理設(shè)置
/// </summary>
public class IconManager
{
    #region 數(shù)據(jù)定義
    private static GUIContent[] labelIcons;
    private static GUIContent[] largeIcons;
 
    /// <summary>
    /// Label類型icon 顯示文字的
    /// </summary>
    public enum LabelIcon
    {
        Gray = 0,
        Blue,
        Teal,
        Green,
        Yellow,
        Orange,
        Red,
        Purple
    }
    /// <summary>
    /// 其他icon不顯示文字
    /// </summary>
    public enum Icon
    {
        CircleGray = 0,
        CircleBlue,
        CircleTeal,
        CircleGreen,
        CircleYellow,
        CircleOrange,
        CircleRed,
        CirclePurple,
        DiamondGray,
        DiamondBlue,
        DiamondTeal,
        DiamondGreen,
        DiamondYellow,
        DiamondOrange,
        DiamondRed,
        DiamondPurple
    }
    #endregion
 
    #region 外部接口
    public static void SetIcon(GameObject gObj, LabelIcon icon)
    {
        if (labelIcons == null)
        {
            labelIcons = GetTextures("sv_label_", string.Empty, 0, 8);
        }
 
        SetIcon(gObj, labelIcons[(int)icon].image as Texture2D);
    }
 
    public static void SetIcon(GameObject gObj, Icon icon)
    {
        if (largeIcons == null)
        {
            largeIcons = GetTextures("sv_icon_dot", "_pix16_gizmo", 0, 16);
        }
 
        SetIcon(gObj, largeIcons[(int)icon].image as Texture2D);
    }
    
    private static void SetIcon(GameObject gObj, Texture2D texture)
    {
        var ty = typeof(EditorGUIUtility);
        var mi = ty.GetMethod("SetIconForObject", BindingFlags.NonPublic | BindingFlags.Static);
        mi.Invoke(null, new object[] { gObj, texture });
    }
    #endregion
 
    #region 內(nèi)部
 
    private static GUIContent[] GetTextures(string baseName, string postFix, int startIndex, int count)
    {
        GUIContent[] guiContentArray = new GUIContent[count];
 
        var t = typeof(EditorGUIUtility);
        var mi = t.GetMethod("IconContent", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(string) }, null);
 
        for (int index = 0; index < count; ++index)
        {
            guiContentArray[index] = mi.Invoke(null, new object[] { baseName + (object)(startIndex + index) + postFix }) as GUIContent;
        }
 
        return guiContentArray;
    }
    
    #endregion
}

調(diào)用方法

IconManager.SetIcon(obj, IconManager.LabelIcon.Blue);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容