拓展Unity3D编辑器

时间:2023-03-10 06:22:57
拓展Unity3D编辑器
/***
*
* 编辑器创建新窗口,并设置窗口布局
*
*
*
*
*
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; public class EditorWindowTest : EditorWindow { [MenuItem("MyMenu/OpenWindow")]
static void OpenWindow()
{
Rect wr = new Rect(0, 0, 500, 500); //创建窗口 EditorWindowTest window = (EditorWindowTest)EditorWindow.GetWindowWithRect(typeof(EditorWindowTest), wr, true, "测试创建窗口");
window.Show();
} private string text; //输入文字的内容
private Texture texture; //选择贴图的对象 public void Awake()
{
texture = Resources.Load("timg (3)") as Texture; //在资源中读取一张贴图
} //绘制窗口时调用
void OnGUI()
{
text = EditorGUILayout.TextField("输入文字:", text); //输入框控件 if (GUILayout.Button("打开通知", GUILayout.Width(200)))
{
//打开一个通知栏
this.ShowNotification(new GUIContent("我是菜菜"));
} 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 Update()
{ } void OnFocus()
{
Debug.Log("当窗口获得焦点是调用一次");
} void OnLostFocus()
{
Debug.Log("当窗口丢失焦点时调用一次");
} void OnHierarchyChange()
{
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
} void OnProjectChange()
{
Debug.Log("当Project视图中的资源发生改变时调用一次");
} void OnInspectorUpdate()
{
this.Repaint();
} void OnSelectionChange()
{
//当窗口处于开启状态,并且在Hierarchy视图中选择某游戏对象时调用
foreach (Transform t in Selection.transforms)
{
//有可能是多选,这里开启一个循环打印选中游戏对象的名称
Debug.Log("OnSelectionChange" + t.name);
}
} void OnDestroy()
{
Debug.Log("当窗口关闭时调用");
}
}