2014-06-23 51 views
1

我的場景中有一定數量的精靈。這是一款2D遊戲。我想在Sprite上顯示一些文本,每個Sprite的GameObject都附帶一個腳本。在腳本我有以下OnGUI()方法Unity GUI無法在右側顯示文本位置

void OnGUI() 
{ 
    GUI.contentColor = Color.black; 
     Rect label = new Rect(); 
     label.x = worldToRect (transform.position).x; 
     label.y = worldToRect (transform.position).y; 
     label.width = 100; 
     label.height = 100; 
     GUI.Label (label, number.ToString()); 

} 
Vector2 worldToRect (Vector3 WorldPosition) 
    { 
      Vector2 v = Camera.main.WorldToScreenPoint (WorldPosition); 
      return GUIUtility.ScreenToGUIPoint (v); 
    } 

但位置並不完全正確。這兩個白色精靈應該在中心顯示3個。我在編輯器中檢查了它們的變換,並且它們精確地對準了精靈的中心。我該如何糾正? The Two white Sprites should display 3 at the centre.I checked their transform in the editor and they align exactly at the centre of the sprite

回答

0

標籤Rect中的x和y是左上角的位置,它與GameObjects的中心對齊。

要將它們居中,從x減去一半寬度並從y減去一半高度,並確保標籤的樣式將「路線」設置爲「中間中心」

相關問題