close
 
  using UnityEngine;
  using UnityEditor;
  public class BuildWindow : EditorWindow {

	string myString = "Hello World";
	bool groupEnabled;
	bool myBool = true;
	float myFloat = 1.23f;

	bool _IsOpen;
	bool _IsOne;
	bool _IsTwo;
 	int _newIdx = 0;
	int _oldIdx = 0;
	string[] ArrayString = new string[3] { "string01", "string02", "string03" };
	Texture texture;
	Transform trans;
	
	// Add menu named "Build Window" to the Window menu
	[MenuItem ("Tools/Build Window")]
	static void Init () {
		// Get existing open window or if none, make a new one:
		BuildWindow window = (BuildWindow)EditorWindow.GetWindow (typeof (BuildWindow));
	}
	
	void OnGUI () {
		// 單純顯示文字
		GUILayout.Label ("Base Settings", EditorStyles.boldLabel);

		// 可輸入文字選項
		myString = EditorGUILayout.TextField ("Text Field", myString);

		// 是否勾 搭配控制內部選項
		groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
		myBool = EditorGUILayout.Toggle ("Toggle", myBool);
		myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
		EditorGUILayout.EndToggleGroup ();

		// 是否勾 搭配直or橫排法 顯示內部選項
		GUILayout.BeginVertical("box");
		_IsOpen = GUILayout.Toggle(_IsOpen, "Is Open");
		if(_IsOpen)
		{
			GUILayout.BeginHorizontal("box");
			GUILayout.BeginVertical("box");
			_IsOne = GUILayout.Toggle(_IsOne, "Is 1");
			GUILayout.EndVertical();
			GUILayout.BeginVertical("box");
			_IsTwo = GUILayout.Toggle(_IsTwo, "Is 2");
			GUILayout.EndVertical();
			GUILayout.EndHorizontal();
		}
		GUILayout.EndVertical();

		// 按鈕
		GUILayout.Button("Button");

		// 下拉式選單
		_newIdx = EditorGUILayout.Popup(_oldIdx, ArrayString);
		if(_newIdx != _oldIdx)
			_oldIdx = _newIdx;

		// 顯示文字 + 自訂變數轉文字顯示
		EditorGUILayout.LabelField ("選擇第N索引值 : ", _oldIdx.ToString());

		// 自訂添加項目
		texture =  EditorGUILayout.ObjectField("選擇 texture :",texture,typeof(Texture),true) as Texture;
		trans =  EditorGUILayout.ObjectField("選擇 Transform :",trans,typeof(Transform),true) as Transform;

		// 訊息通知視窗
		if(GUILayout.Button("顯示通知訊息",GUILayout.Width(300)))
			this.ShowNotification(new GUIContent("ShowNotification"));
		if(GUILayout.Button("關閉通知訊息",GUILayout.Width(300)))
			this.RemoveNotification();
	}
  }

 

以上就是提供自訂視窗,常用的UI編輯程式碼,算是整理官方提供的文件範例

以下是範例的結果 :

005.png  

歡迎光臨 ~ Eg 程式筆記的天堂
當你看完此篇文章,如果你覺得文章不錯
可以留下鼓勵的留言~
將是我的撰寫更多相關文章的動力

arrow
arrow
    文章標籤
    unity Editor 編輯視窗
    全站熱搜

    低調_Eg 發表在 痞客邦 留言(2) 人氣()