【发布时间】:2013-06-30 17:52:02
【问题描述】:
我正在使用 Unity3D 的标准 GUI。
如何获取 GUI 元素的屏幕位置?
【问题讨论】:
我正在使用 Unity3D 的标准 GUI。
如何获取 GUI 元素的屏幕位置?
【问题讨论】:
基本上你不能。使用更好的词,正如您已经注意到的那样,GUI.Button 只返回一个布尔值,指示按钮是否被按下。
因为您实际上是在每一帧重新创建按钮(您的 GUI.Button 代码在回调中,例如 Update、FixedUpdate、OnGUI、...),并且当您调用 @987654326 @ 你自己传递了Rect 边界,实际上不需要查询任何对象来检索实际坐标。只需将它们存放在某个地方即可。
Rect buttonBounds = new Rect (50,60,100,20);
bool buttonPressed = GUI.Button (buttonBounds, "get postion");
if (buttonPressed)
{
//you know the bounds, because buttonBounds button has been pressed
}
【讨论】:
试试这个:
var positionGui : Vector2;
positionGui = Vector2 (guiElement.transform.position.x * Screen.width, guiElement.transform.position.y * Screen.height);
【讨论】:
你可以这样做
public static Rect screenRect
(float tx,
float ty,
float tw,
float th)
{
float x1 = tx * Screen.width;
float y1 = ty * Screen.height;
float sw = tw * Screen.width;
float sh = th * Screen.height;
return new Rect(x1,y1,sw,sh);
}
public void OnGUI()
{
if (GUI.Button(screenRect(0.4f, 0.6f, 0.2f, 0.1f), "TRY AGAIN"))
{
Application.LoadLevel(0);
}
print ("button position + framesize"+screenRect(0.4f, 0.6f, 0.2f, 0.1f));
}
【讨论】: