set gameobject Icons by Script
2024-08-30 07:52:49
有很多时候我们需要在编辑器查看一个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();
}
最新文章
- ACCESS中计算日均值
- iOS 页面跳转传值,属性传值,代理传值,代码块传值,单例传值,通知传值
- [Xamarin] 客製化的ListView之章 (转帖)
- QT5.0.1在Windows下 出现QApplication: No such file or directory 问题的解决办法
- BZOJ2464: 中山市选[2009]小明的游戏
- IE浏览器下读取客户端上传的文件大小
- UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
- BZOJ1537: [POI2005]Aut- The Bus
- Swift - 操作SQLite数据库(引用SQLite3库)
- PHP基础入门(五)---PHP面向对象
- Java并发编程笔记——技术点汇总
- 自己动手修改Robotium代码(上)
- 品阿里 Java 开发手册有感
- 今日、本周、本月时间列表 python方法
- tiny6410采用sd卡烧写的问题
- div展现与收起效果(鼠标移入移出)
- json与xml数据输出类
- javascript变量浅析
- UIButton 设置圆角 边框颜色 点击回调方法
- Python基础总结(字符串常用,数字类型转换,基本运算符与流程控制)