User interface Unity3D中GUI元素的屏幕位置
我使用的是Unity3D的标准GUIUser interface Unity3D中GUI元素的屏幕位置,user-interface,unity3d,User Interface,Unity3d,我使用的是Unity3D的标准GUI 如何获得GUI元素的屏幕位置?基本上你不能。使用更好的词语,正如您已经注意到的那样,GUI.Button只返回一个bool,指示是否按下了按钮 由于您实际上是在每一帧重新创建按钮(您的GUI.button代码在一个回调中,例如Update,FixedUpdate,OnGUI,…),当您调用GUI.button时,您将自己传递Rect边界,实际上不需要查询任何对象来检索实际坐标。只要把它们放在某个地方就行了 Rect buttonBounds = new Re
如何获得GUI元素的屏幕位置?基本上你不能。使用更好的词语,正如您已经注意到的那样,
GUI.Button
只返回一个bool,指示是否按下了按钮
由于您实际上是在每一帧重新创建按钮(您的GUI.button
代码在一个回调中,例如Update
,FixedUpdate
,OnGUI
,…),当您调用GUI.button
时,您将自己传递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
}
基本上你不能。使用更好的词语,正如您已经注意到的那样,
GUI.Button
只返回一个bool,指示是否按下了按钮
由于您实际上是在每一帧重新创建按钮(您的GUI.button
代码在一个回调中,例如Update
,FixedUpdate
,OnGUI
,…),当您调用GUI.button
时,您将自己传递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);
试试这个:
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));
}
你可以这样做
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));
}