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個。我在編輯器中檢查了它們的變換,並且它們精確地對準了精靈的中心。我該如何糾正?