微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何定位世界空间画布文本以在 Unity 中创建尺寸叠加

如何解决如何定位世界空间画布文本以在 Unity 中创建尺寸叠加

我正在尝试在 Unity 中创建一个维度叠加画布。设法制作了一个,但在旋转游戏对象(立方体)时遇到了困难。维度显示文本正在旋转,但它们没有用画布和游戏对象重新定位自己。相反,它们围绕自己的位置旋转。我怎样才能让它们与画布一起旋转,以便它们重新定位?请参阅下面的游戏对象(立方体)的截图和代码

非常感谢 :)

Position of the dimension text and the worldspace canvas

Dimension texts are not changing position while rotating the game

请看下面我的代码

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class dimensionScript : MonoBehavIoUr
{

    //World space Canvas to attach on the GameObject
    public Canvas canvas;

    //Text to display the Length and Width of the Game object
    public Text lt,wt;

    public float l,w;
    public Vector3 angles,pos,sc;



    void Update()
    {

        angles = transform.eulerAngles;
        pos =transform.position;
        sc = transform.localScale;

        Vector3 pos2 = canvas.transform.position;
      

        // Length and Width of the GameObject
        l = transform.localScale.x;
        w = transform.localScale.z;

        // Positioning and scaling the canvas according to the scale of the gameobject
        canvas.transform.position = transform.position;        
        canvas.GetComponent<RectTransform>().sizeDelta = new Vector2(l+.5f,w + .5f);
        canvas.transform.eulerAngles = new Vector3(90f,angles.y,0);

        float rw = canvas.GetComponent<RectTransform>().rect.width;
        float rh = canvas.GetComponent<RectTransform>().rect.height;
      

 lt.transform.position = new Vector3(pos2.x,pos2.y,pos2.z - rh / 2);
  wt.transform.position = new Vector3(pos2.x - rw / 2,pos2.z);

       // lt.transform.eulerAngles = new Vector3(90f,0);
       // wt.transform.eulerAngles = new Vector3(90f,0);


        lt.text = l.ToString();
        wt.text = w.ToString();

    }

}

enter image description here

enter image description here

enter image description here

解决方法

好的,所以您需要做的是将 Canvas 根对象放置在场景层次结构中的立方体下(作为子项),如下所示:

enter image description here

之后,只需删除所有更改画布或文本位置和旋转的代码,您应该就可以了。 在我的示例中,画布放置在立方体的底部(-0.5“y”坐标) enter image description here

之后,您可以根据需要旋转和移动立方体,并且画布将像放置为该立方体的子项的任何其他游戏对象一样粘在它上面 enter image description here

,

谢谢@derHugo。我清理了我的文件并重试了你提到的。它正在工作:) :)

在下面发布代码!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class dimensionScript : MonoBehaviour
{
  

    public Canvas canvas;
    
    public Text lt,wt;
    public float l,w;

    public Vector3 angles,pos,sc;


    void Update()
    {

        angles = transform.eulerAngles;
        pos =transform.position;
        sc = transform.localScale;

        Vector3 pos2 = canvas.transform.position;

        l = transform.localScale.x;
        w = transform.localScale.z;


             canvas.transform.localPosition = transform.position;
    canvas.GetComponent<RectTransform>().sizeDelta = new Vector2(l+.5f,w + .5f);
    canvas.transform.eulerAngles = new Vector3(90f,angles.y,0);

    float rw = canvas.GetComponent<RectTransform>().rect.width;
    float rh = canvas.GetComponent<RectTransform>().rect.height;

    Vector3 pos2 = canvas.transform.position;


  lt.transform.localPosition = new Vector3(0,-rh / 2,0);
 wt.transform.localPosition = new Vector3( - rw / 2,0);
                   

        lt.text = l.ToString();
        wt.text = w.ToString();

    }



}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。