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