0
我想在Unity UI中的一个多维数据集上进行文本显示,当我自由移动相机时。画布设置为“屏幕空间 - 相机”模式,并将相机属性设置在检查器中。我还附加了一个鼠标移动脚本到相机。为屏幕空间获取世界位置到画布位置 - 相机画布
我的问题是,文本几乎没有移动,似乎忽略立方体相对于相机的视口的位置。这里是我的代码:
public class UpdateUiMarker : MonoBehaviour {
public RectTransform element;
public Transform cube;
public RectTransform canvas;
// Update is called once per frame
void LateUpdate() {
Vector2 screenPos = Camera.main.WorldToScreenPoint(cube.position);
Vector2 localPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPos, Camera.main, out localPos);
element.localPosition = localPos;
}
}
我没有看到任何明显的错误(除了'WorldToScreenPoint'在Camera类中,而不是RectTransformUtility类)。 – Draco18s
@ Draco18s我也尝试了Camera.main.WorldToScreenPoint方法,我得到了完全相同的结果。 – Angelin
我的意思是,我没有在[RectTransformUtilities文档](https://docs.unity3d.com/ScriptReference/RectTransformUtility.html)中看到WorldToScreenPoint。 – Draco18s