Unity脚本修改游戏对象的图标SelectIcon的方法

前言

最近在做一个编辑器。需要修改这个。方便美术人员观看。和编辑更方便。但是一直找不到API。在今天无聊翻翻谷歌。竟然翻到了。之前百度怎么都找不到

这里就分享一下了。

11

 

好了。不多说了。直接上代码

代码

Manager脚本

using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 对象Icon管理设置
/// </summary>
public class IconManager
{
    #region 数据定义
    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 内部

    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
}

调用

IconManager.SetIcon(obj, IconManager.LabelIcon.Blue);

传入要修改的GameObject以及样式枚举即可

效果如下

11

本文链接:

https://bobsong.cn/1067.html
1 + 6 =
快来做第一个评论的人吧~