Unity 编辑器扩展自定义窗体

这次看见Unity还可以自定义弹出窗体,让我很好奇.于是就去网上找文章看了看. 如果想自定义窗体需要把类放入Editor文件夹下面.

代码如下:

using UnityEngine;

        using UnityEditor;



        public class MyEditor : EditorWindow

        {

            [MenuItem("GameObject/window")]

            static void AddWindow()

            {

                Rect wr = new Rect(0, 0, 500, 500);

                MyEditor window = (MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor), wr, true, "盘子脸");

            }



            private string text;

            private Texture texture;



            public void Awake()

            {

                texture = Resources.Load("1") as Texture;

            }



            void OnGUI()

            {

                text = EditorGUILayout.TextField("输入文字", text);



                if (GUILayout.Button("打开通知", GUILayout.Width(200)))

                {

                    this.ShowNotification(new GUIContent("This is a Notification"));

                }



                if (GUILayout.Button("关闭通知", GUILayout.Width(200)))

                {

                    //关闭通知栏

                    this.RemoveNotification();

                }



                EditorGUILayout.LabelField("鼠标在窗口的位置", Event.current.mousePosition.ToString());

                texture = EditorGUILayout.ObjectField("添加贴图", texture, typeof(Texture), true) as Texture;

                if (GUILayout.Button("关闭窗口", GUILayout.Width(200)))

                {

                    //关闭窗口

                    this.Close();

                }

            }





            void OnFocus()

            {

                Debug.Log("当窗口获得焦点调用一次");

            }



            void OnLostFocus()

            {

                Debug.Log("当窗口丢失焦点调用一次");

            }



            void OnHierarchyChange()

            {

                Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");

            }



            void OnProjectChange()

            {

                Debug.Log("当Project视图中的资源发生改变时调用一次");

            }



            void OnInspectorUpdate()

            {

                this.Repaint();

            }



            void OnSelectionChange()

            {

                foreach (Transform t in Selection.transforms)

                {

                    Debug.Log("OnSelectionChange" + t.name);

                }

            }



            void OnDestroy()

            {

                Debug.Log("当窗口关闭时候调用");

            }

        }

 

1. 暂时没有找到自定义窗体和组件之间是如何传递值的!

 

本文固定链接: http://www.xuanyusong.com/archives/2211

转载请注明: 雨松MOMO 2013年04月15日 于 雨松MOMO程序研究院 发表

你可能感兴趣的:(unity)