using UnityEngine;
using System.Collections;
using UnityEditor ;
public class MyEditorWindow : EditorWindow {
[MenuItem ("Window/MyWindow")]
static void AddWindow(){
//创建窗口
Rect rect = new Rect (0,0,500,500);
MyEditorWindow window = (MyEditorWindow)EditorWindow .GetWindowWithRect (typeof(MyEditorWindow),rect,true,"CustomWindow");
window.Show();
}
//输入文字类容;
private string text;
private Texture texture;
string status="hao";
bool b;
public int toolbarInt = 0;
public Texture icon;
public string[] toolbarStrings = new string[] {"Toolbar1", "Toolbar2", "Toolbar3"};
public void Awake(){
//在资源中读取一张贴图
texture = Resources.Load ("1")as Texture;
}
//绘制窗口时调用
void OnGUI(){
// button 监听按钮
EditorGUILayout .TextField ("name","value");
if(GUILayout .Button ("点我"))
Debug .Log ("点我了");
Debug .Log ("OnGUI");
EditorGUILayout.LabelField ("Status: ", "Idel");
if(b){
EditorGUILayout.Vector3Field("Position", new Vector3 (0f,12f,1f));
}
//输入框控件
text = EditorGUILayout .TextField ("输入文字",text);
if(GUILayout.Button ("打开通知",GUILayout .Width (200))){
//打开通知栏
this.ShowNotification (new GUIContent ("This is a Notifactiion"));
}
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 ();
}
// 工具条
toolbarInt = GUI.Toolbar(new Rect(200, 300, 250, 30), toolbarInt, toolbarStrings);
//
GUI.Button(new Rect(100,200, 100, 20), new GUIContent("Click me", icon));
}
void Update(){}
void OnFocus(){
Debug.Log("当窗口获得焦点时调用一次");
}
void OnLostFocus(){
Debug.Log("当窗口丢失焦点时调用一次");
}
void OnHierarchyChange(){
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
}
void OnProjectChange(){
Debug.Log("当Project视图中的资源发生改变时调用一次");
}
void OnInspectorUpdate(){
//Debug.Log("窗口面板的更新");
//这里开启窗口的重绘,不然窗口信息不会刷新
this.Repaint ();
}
void OnSelectionChange(){
//当窗口出去开启状态,并且在Hierarchy 视图中选择游戏对象时调用
foreach (Transform t in Selection .transforms){
//有可能是多选,这里开启了一个循环打印游戏对戏的名称
Debug .Log ("OnselectionChange"+t.name);
}
}
void OnDestroy(){
Debug.Log("当窗口关闭时调用");
}
}
using System.Collections;
using UnityEditor ;
public class MyEditorWindow : EditorWindow {
[MenuItem ("Window/MyWindow")]
static void AddWindow(){
//创建窗口
Rect rect = new Rect (0,0,500,500);
MyEditorWindow window = (MyEditorWindow)EditorWindow .GetWindowWithRect (typeof(MyEditorWindow),rect,true,"CustomWindow");
window.Show();
}
//输入文字类容;
private string text;
private Texture texture;
string status="hao";
bool b;
public int toolbarInt = 0;
public Texture icon;
public string[] toolbarStrings = new string[] {"Toolbar1", "Toolbar2", "Toolbar3"};
public void Awake(){
//在资源中读取一张贴图
texture = Resources.Load ("1")as Texture;
}
//绘制窗口时调用
void OnGUI(){
// button 监听按钮
EditorGUILayout .TextField ("name","value");
if(GUILayout .Button ("点我"))
Debug .Log ("点我了");
Debug .Log ("OnGUI");
EditorGUILayout.LabelField ("Status: ", "Idel");
EditorGUILayout.LabelField ("Empty");
//下拉隐藏菜单
b =EditorGUILayout .Foldout (b,status);if(b){
EditorGUILayout.Vector3Field("Position", new Vector3 (0f,12f,1f));
}
//输入框控件
text = EditorGUILayout .TextField ("输入文字",text);
if(GUILayout.Button ("打开通知",GUILayout .Width (200))){
//打开通知栏
this.ShowNotification (new GUIContent ("This is a Notifactiion"));
}
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 ();
}
// 工具条
toolbarInt = GUI.Toolbar(new Rect(200, 300, 250, 30), toolbarInt, toolbarStrings);
//
GUI.Button(new Rect(100,200, 100, 20), new GUIContent("Click me", icon));
}
void Update(){}
void OnFocus(){
Debug.Log("当窗口获得焦点时调用一次");
}
void OnLostFocus(){
Debug.Log("当窗口丢失焦点时调用一次");
}
void OnHierarchyChange(){
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
}
void OnProjectChange(){
Debug.Log("当Project视图中的资源发生改变时调用一次");
}
void OnInspectorUpdate(){
//Debug.Log("窗口面板的更新");
//这里开启窗口的重绘,不然窗口信息不会刷新
this.Repaint ();
}
void OnSelectionChange(){
//当窗口出去开启状态,并且在Hierarchy 视图中选择游戏对象时调用
foreach (Transform t in Selection .transforms){
//有可能是多选,这里开启了一个循环打印游戏对戏的名称
Debug .Log ("OnselectionChange"+t.name);
}
}
void OnDestroy(){
Debug.Log("当窗口关闭时调用");
}
}
本文介绍如何在Unity中创建自定义编辑器窗口,包括按钮响应、文本输入、通知栏操作等功能。通过代码示例展示了如何实现这些功能,并介绍了与编辑器窗口交互的各种方法。
852

被折叠的 条评论
为什么被折叠?



