码迷,mamicode.com
首页 > 编程语言 > 详细

unity3d拓展编辑器MenuItem的使用

时间:2016-03-11 18:49:53      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

 

MenuItem是自定义菜单栏显示

比如:[MenuItem("new/My Window")] 这样就会显示菜单new/My Window

把这个放在一个静态方法上就可以了。记住。方法一定要是静态的。

Unity圣典上的解释:

技术分享

 

也就是说类必须继承:EditorWindow。EditorWindow在命名空间using UnityEditor;中

 

跟着Unity圣典上的列子和网上的列子敲了一次,这样才能深入理解

  1 using UnityEngine;
  2 using System.Collections;
  3 using UnityEditor;
  4 
  5 /*
  6  拓展自定义编辑器窗口
  7  */
  8 public class GetWindows : EditorWindow
  9 {
 10 
 11     /*
 12      从这EditorWindow个类来创建编辑器窗口。
 13      * 注意:这是一个编辑器类,如果想使用它你需要把它放到工程目录下的Assets/Editor文件夹下。
 14      * 编辑器类在UnityEditor命名空间下。所以当使用C#脚本时,你需要在脚本前面加上 "using UnityEditor"引用。
 15 
 16      * 
 17      */
 18 
 19     string myString = "Hello World";
 20     bool groupEnabled;
 21     bool myBool = true;
 22     float myFloat = 1.23f;
 23 
 24     string notification = "This is a Notification";
 25 
 26 
 27     Texture texture;
 28 
 29     //添加菜单项My Window到Window菜单
 30     [MenuItem("new/My Window")]
 31     static void Init()
 32     {
 33         ////获取现有的打开窗口或如果没有,创建一个新的
 34         /*
 35          参数一:窗口的类型,必须源于编辑器窗口
 36          * 参数二:设置为false,来创建一个浮动窗口;为true,创建一个标准窗口(标准窗口不能停靠)。 默认是false
 37          * 参数三:如果GetWindow创建一个新的窗口,它将获取这个标题。如果这个值为null,使用类名作为标题。
 38          * 
 39          * GetWindows类必须继承EditorWindow
 40          * 
 41          */
 42         //GetWindows window = (GetWindows)EditorWindow.GetWindow(typeof(GetWindows), false, "窗口名称");
 43         
 44         //跟上面是同样的效果
 45         GetWindows window = GetWindow<GetWindows>("窗口名称");
 46         //设置窗口在屏幕中的位置
 47         window.position = new Rect(Screen.width / 2, Screen.height / 2, 500, 500);
 48         //显示窗口
 49         //window.Show();
 50 
 51         
 52     }
 53 
 54     /// <summary>
 55     /// 在这里实现自己的编辑器界面。
 56     /// </summary>
 57     void OnGUI()
 58     {
 59 
 60         /*
 61          EditorGUILayout.BeginToggleGroup 开始开关组
 62          */
 63 
 64         //label
 65         GUILayout.Label("Base Settings", EditorStyles.boldLabel);
 66         //一个输入框
 67         myString = EditorGUILayout.TextField("Text Field", myString);
 68 
 69         //groupEnabled为true,则打开组。false则关闭组
 70         groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
 71         myBool = EditorGUILayout.Toggle("Toggle", myBool);
 72         myFloat = EditorGUILayout.Slider("Slider", myFloat, -3, 3);
 73         EditorGUILayout.EndToggleGroup();
 74 
 75 
 76         texture = EditorGUILayout.ObjectField("添加贴图", texture, typeof(Texture), true) as Texture;
 77 
 78         if (GUILayout.Button("Show Notification"))
 79         {
 80             //显示一个具有用户类型的通知消息 显示一个消息提示,过会 会慢慢消失
 81             this.ShowNotification(new GUIContent(notification));
 82         }
 83         if (GUILayout.Button("Remove Notification"))
 84         {
 85             //立即移除消息
 86             this.RemoveNotification();
 87         }
 88         if (GUILayout.Button("Close Windows"))
 89         {
 90             //关闭窗口
 91             this.Close();
 92         }
 93     }
 94 
 95     void OnFocus()
 96     {
 97         Debug.Log("窗口获得焦点");
 98     }
 99     /// <summary>
100     /// 只要选择发生改变时调用。 
101     /// </summary>
102     void OnSelectionChange()
103     {
104         Transform tr = Selection.transforms[0];
105         Debug.Log("当前选择的是:" + tr.name);
106     }
107     /// <summary>
108     /// 在给定检视面板每秒10帧更新。 
109     /// </summary>
110     void OnInspectorUpdate()
111     {
112         //Debug.Log("窗口面板的更新");
113         //这里开启窗口的重绘,不然窗口信息不会刷新
114         this.Repaint();
115     }
116 }

最终的效果是:

技术分享

unity3d拓展编辑器MenuItem的使用

标签:

原文地址:http://www.cnblogs.com/nsky/p/5266618.html

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