码迷,mamicode.com
首页 > 其他好文 > 详细

Editor Script to set Icons

时间:2015-03-20 12:31:24      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

 有很多时候我们需要在编辑器查看一个Gameobject的移动,有些人采用Gizoms类,可是如果不想用,可以使用U3D内置的Icon类。

 大神还是很多的,http://forum.unity3d.com/threads/editor-script-to-set-icons-impossible.187975/

 

public enum LabelIcon
{
    Gray = 0,
    Blue,
    Teal,
    Green,
    Yellow,
    Orange,
    Red,
    Purple
}

public enum Icon
{
    CircleGray = 0,
    CircleBlue,
    CircleTeal,
    CircleGreen,
    CircleYellow,
    CircleOrange,
    CircleRed,
    CirclePurple,
    DiamondGray,
    DiamondBlue,
    DiamondTeal,
    DiamondGreen,
    DiamondYellow,
    DiamondOrange,
    DiamondRed,
    DiamondPurple
}

private static GUIContent[] labelIcons;
private static GUIContent[] largeIcons;


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 });
}

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;
}

 

 

使用方法也很简单,只需要

 1     void Test()
 2     {
 3         GameObject go = new GameObject("1");
 4         Util.SetIcon(go, LabelIcon.Red);
 5 
 6     }
 7 
 8     void OnGUI()
 9     {
10         if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button")) Test();
11     }

技术分享

 

Editor Script to set Icons

标签:

原文地址:http://www.cnblogs.com/chongxin/p/4353225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!