Unity 2D标签跟随3D物体移动

2024-01-03 11:40:09
using UnityEngine;

public class 2DLabelFollow3DObj: MonoBehaviour{
  public Transform targetObj;
  public GameObject labelObj;
  
  private void Update()
  {
    UpdateLabel(targetObj);
  }
  
  //target 标签跟随的物体
  private void UpdateLabel(Transform target)
  {
    RectTransform labelRt=label.Getcomponent<RectTransform()>;
    Vector3 screenPos=Camera.main.WorldToScreenPoint(target.Position);
    RectTransform rect=GameObject.Find("Canvas").GetComponent<RectTransform>;
    Vector2 outVec;
    if(RectTransformUtility.ScreenPointToLocalPointInRectangle(rect,screenPos,null,out outVec))
      outVec+=new Vector2(0,-GameObject.Find("Canvas").transform.Find("Panel").localPosition.y);
      //Panel为Canvas中标签的父节点
  }
  
  labelRt.anchoredPosition=outVec;

}

文章来源:https://blog.csdn.net/qq_42862278/article/details/135358526
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。