【发布时间】:2014-06-23 13:12:49
【问题描述】:
我的场景中有一定数量的精灵。这是一个2D游戏。我想在精灵上显示一些文本,每个精灵的游戏对象都附有一个脚本。在脚本中我有以下 OnGUI() 方法
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。我在编辑器中检查了它们的变换,它们在精灵的中心完全对齐。我该如何纠正?
【问题讨论】:
标签: c# user-interface unity3d 2d