2014-06-23 45 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减去一半高度,并确保标签的样式将“路线”设置为“中间中心”

相关问题