#unity/日常积累
坐标转换,绝对坐标设定,逻辑参考
using UnityEngine;
public class UITextFollowSprite : MonoBehaviour
{
public Transform targetSprite;
public Camera mainCamera;
public Vector2 screenOffset;
void Update()
{
if (targetSprite != null && mainCamera != null)
{
// 将3D世界坐标转换为屏幕坐标
Vector3 screenPos = mainCamera.WorldToScreenPoint(targetSprite.position);
// 设置UI Text的屏幕位置
GetComponent<RectTransform>().position = screenPos + (Vector3)screenOffset;
}
}
}
这是ui里面的text,跟随3D物体的表现效果 参考:
![[Pasted image 20250711084311.png]]