【问题标题】:Unity GUI Not able to Display Text at the right PositionUnity GUI 无法在正确的位置显示文本
【发布时间】: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


    【解决方案1】:

    标签的 Rect 中的 x 和 y 是左上角的位置,与 GameObjects 的中心对齐。

    要将它们居中,从 x 中减去一半宽度,从 y 中减去一半高度,并确保标签的样式已将对齐设置为“中间居中”

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多