🏂

GUILayout 用于定义 GUIOption 类型的静态方法

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace SceneViewExtension.GUILayouts {
	[CustomEditor (typeof (GUIOptions))]
	public class HeightWidth : Editor {

		bool _scaling;
		Rect _window = new Rect (5, 20, 200, 250);
		Rect _scaleArea;

		bool _useWidth, _useHeight;
		bool _expandWidth, _expandHeight;
		bool _useMaxWidth, _useMaxHeight;
		bool _useMinWidth, _useMinHeight;

		bool _reset;

		private void OnSceneGUI () {
			Handles.BeginGUI ();

			_window = GUILayout.Window (0, _window, OnWindowGUI, "Window");

			Handles.EndGUI ();
		}

		private void OnWindowGUI (int id) {
			_useWidth = GUILayout.Toggle (_useWidth, "Width: 200");
			_useHeight = GUILayout.Toggle (_useHeight, "Height: 100");

			_expandWidth = GUILayout.Toggle (_expandWidth, "Expanded Width");
			_expandHeight = GUILayout.Toggle (_expandHeight, "Expanded Height");

			_useMaxWidth = GUILayout.Toggle (_useMaxWidth, "Max Width: 300");
			_useMaxHeight = GUILayout.Toggle (_useMaxHeight, "Max Height: 150");

			_useMinWidth = GUILayout.Toggle (_useMinWidth, "Min Width: 100");
			_useMinHeight = GUILayout.Toggle (_useMinHeight, "Min Height: 50");

			List<GUILayoutOption> options = new List<GUILayoutOption> ();
			if (_useWidth) options.Add (GUILayout.Width (200));
			if (_useHeight) options.Add (GUILayout.Height (100));

			if (_expandWidth)
				options.Add (GUILayout.ExpandWidth (true));
			else
				options.Add (GUILayout.ExpandWidth (false));

			if (_expandHeight)
				options.Add (GUILayout.ExpandHeight (true));
			else
				options.Add (GUILayout.ExpandHeight (false));

			if (_useMaxWidth) options.Add (GUILayout.MaxWidth (300));
			if (_useMaxHeight) options.Add (GUILayout.MaxHeight (150));
			if (_useMinWidth) options.Add (GUILayout.MinWidth (100));
			if (_useMinHeight) options.Add (GUILayout.MinHeight (50));

			GUILayout.BeginArea (new Rect (5, 165, _window.width - 10, _window.height - 190), "", "box");
			_reset = GUILayout.Button ("Click to Reset", options.ToArray ());
			GUILayout.EndArea ();
			if (_reset) {
				_useWidth = _useHeight = _expandWidth = _expandHeight = _useMaxWidth = _useMaxHeight = _useMinWidth = _useMinHeight = false;
				_window = new Rect (5, 20, 200, 250);
			}

			_scaleArea = new Rect (_window.width - 20, _window.height - 20, 15, 15);
			GUI.Box (_scaleArea, "");

			if (Event.current.type == EventType.MouseUp) {
				_scaling = false;
			} else if (Event.current.type == EventType.MouseDown && _scaleArea.Contains (Event.current.mousePosition)) {
				_scaling = true;
			}

			if (_scaling) {
				_window = new Rect (
					_window.x, _window.y,
					_window.width + Event.current.delta.x / 2, _window.height + Event.current.delta.y / 2
				);
			}

			GUI.DragWindow (new Rect(0, 0, _window.width, 15));
		}
	}
}

Width, Height

使用 GUILayout.HeightGUILayout.Width 定义的控件以固定的高或宽显示:

ExpandWidth, ExpandHeight

使用 GUILayout.ExpandHeightGUILayout.ExpandWidth 定义的控件则始终在竖直或者水平方向上填满整个父区域:

MinWidth, MaxWidth, MinHeight, MaxHeight

使用 GUILayout.MinWidth GUILayout.MaxWidth GUILayout.MinHeight GUILayout.MaxHeight 定义的控件具备自动延展的特性,同时设置有宽度或者高度的最值: