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

但是如果想在脚本中设置而不是通过手动选择呢?

Google之,大神在民间

http://forum.unity3d.com/threads/editor-script-to-set-icons-impossible.187975/

一、代码

public enum LabelIcon
{
Gray = ,
Blue,
Teal,
Green,
Yellow,
Orange,
Red,
Purple
} public enum Icon
{
CircleGray = ,
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, , );
} 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", , );
} 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 = ; index < count; ++index)
{
guiContentArray[index] = mi.Invoke(null, new object[] { baseName + (object)(startIndex + index) + postFix }) as GUIContent;
} return guiContentArray;
}

二、使用

     void Test()
{
GameObject go = new GameObject("");
Util.SetIcon(go, LabelIcon.Red); } void OnGUI()
{
if (GUI.Button(new Rect(, , , ), "I am a button")) Test();
}

最新文章

  1. ACCESS中计算日均值
  2. iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
  3. [Xamarin] 客製化的ListView之章 (转帖)
  4. QT5.0.1在Windows下 出现QApplication: No such file or directory 问题的解决办法
  5. BZOJ2464: 中山市选[2009]小明的游戏
  6. IE浏览器下读取客户端上传的文件大小
  7. UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
  8. BZOJ1537: [POI2005]Aut- The Bus
  9. Swift - 操作SQLite数据库(引用SQLite3库)
  10. PHP基础入门(五)---PHP面向对象
  11. Java并发编程笔记——技术点汇总
  12. 自己动手修改Robotium代码(上)
  13. 品阿里 Java 开发手册有感
  14. 今日、本周、本月时间列表 python方法
  15. tiny6410采用sd卡烧写的问题
  16. div展现与收起效果(鼠标移入移出)
  17. json与xml数据输出类
  18. javascript变量浅析
  19. UIButton 设置圆角 边框颜色 点击回调方法
  20. Python基础总结(字符串常用,数字类型转换,基本运算符与流程控制)

热门文章

  1. 第十四章 调试及安全性(In .net4.5) 之 对称及非对称加密
  2. 关于EasyUI与富文本编辑器结合使用的问题(kindueditor与uueditor)
  3. 实战Django:官方实例Part3
  4. Android无法连接adb的解决方法
  5. Power of Four
  6. Android--获取App应用程序的大小
  7. golang初试:坑爷的
  8. linux eclipse
  9. locate 命令
  10. django-pagination的使用