using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIFollowObjFuLi : MonoBehaviour
{
[Header(“跟随的物体”)]
public Transform FollowTran;
[Header("偏移值")]
public Vector2 Offset;
RectTransform ParentTran, Rtran;
void Start()
{
Rtran = transform.GetComponent<RectTransform>();
ParentTran = transform.GetComponentInParent<Canvas>().GetComponent<RectTransform>();
}
private void Update()
{
if (FollowTran != null)
{
Vector2 mScreenPos = Camera.main.WorldToScreenPoint(FollowTran.transform.position);
Vector2 mRectPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(ParentTran, mScreenPos, null, out mRectPos);
Rtran.localPosition = mRectPos + Offset;
}
}
}