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,"widow name");
window.Show();
}
}
EditorWindow.GetWindowWithRect() 和 EditorWindow.GetWindow()都可以创建一个窗口。前者可以规定窗口的区域,后者可通过鼠标动态的延伸窗口。参数1表示窗口的对象,参数2表示窗口的区域,参数3表示窗口类型true表示窗口不会被别的窗口覆盖,参数4表示窗口的名称。
using System.Collections;
using System.Collections.Generic;
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,"window name");
window.Show();
}
//输入文字到内容
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("关闭窗口"))
{
//关闭窗口
this.Close();
}
}
void OnFocus()
{
Debug.Log("当窗口获得焦点时调用一次");
}
void OnLostFocus()
{
Debug.Log("当窗口失去焦点时调用一次");
}
void OnHierarchyChange()
{
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用");
}
void OnInspectorUpdate()
{
//重新绘制
this.Repaint();
}
void OnSelectionChange()
{
//当窗口出去开启壮体啊,并且在Hierarchy视图中选择某个游戏对象时调用
foreach(Transform t in Selection.transforms)
{
Debug.Log("OnSelectionChange"+t.name);
}
}
void OnDestroy()
{
Debug.Log("当窗口关闭时调用");
}
}