GUILayoutUtility.GetRect 获取矩形

static function GetRect (content : GUIContent, style : GUIStyle) : Rect
static function GetRect (content : GUIContent, style : GUIStyle, params options : GUILayoutOption[]) : Rect

Parameters参数

Returns

Rect - A rectangle that is large enough to contain content when rendered in style.

返回Rect类型,一个足够大矩形包含style渲染的content内容。

Description描述

Reserve layout space for a rectangle for displaying some contents with a specific style.

为显示某些对于一个指定风格的内容的矩形,保留布局空间。也就是说,获取一个自动大小矩形。

using UnityEngine;

using System.Collections;



public class example : MonoBehaviour {

	public GUIContent buttonText = new GUIContent("some button");

	public GUIStyle buttonStyle = GUIStyle.none;

	void OnGUI() {

		Rect rt = GUILayoutUtility.GetRect(buttonText, buttonStyle);

		if (rt.Contains(Event.current.mousePosition))

			GUI.Label(new Rect(0, 20, 200, 70), "PosX: " + rt.x + "\nPosY: " + rt.y + "\nWidth: " + rt.width + "\nHeight: " + rt.height);



		GUI.Button(rt, buttonText, buttonStyle);

		}

}
// Shows the button rect properties in a label when the mouse is over it

//当鼠标经过时,在标签显示按钮的矩形属性

var buttonText : GUIContent = new GUIContent("some dasdfgwbutton");

var buttonStyle : GUIStyle = GUIStyle.none;



function OnGUI() {

	var rt : Rect = GUILayoutUtility.GetRect(buttonText, buttonStyle);

	if (rt.Contains(Event.current.mousePosition)) {

		GUI.Label(Rect(0,20,200,70), "PosX: " + rt.x + "\nPosY: " + rt.y +

		"\nWidth: " + rt.width + "\nHeight: " + rt.height);

	}

	GUI.Button(rt, buttonText, buttonStyle);

}