2013-07-03 270 views

回答

4

基本上你不能。使用更好的單詞,正如你已經注意到的那樣,GUI.Button只是返回一個指示按鈕是否被按下的布爾值。

因爲實際上重新創建按鈕,每幀(你GUI.Button代碼是一個回調中,如UpdateFixedUpdateOnGUI,...),當你調用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 
} 
0

試試這個:

var positionGui : Vector2; 
positionGui = Vector2 (guiElement.transform.position.x * Screen.width, guiElement.transform.position.y * Screen.height); 
0

你可以做這樣的事情

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)); 

}