unity 擴展編輯器二 新建窗體


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("當窗口關閉時調用");
	}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM