unity 编辑器扩展 创建一个窗口


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 windows=(MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor),wr,true,"window name");
		windows.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("关闭窗口",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("当窗口关闭时调用");
	}




}



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM