using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.EventSystems; // 添加这一行 public class IconFollow2D : MonoBehaviour { //目标对象 public Transform target; //目标位置 public Vector3 targetPos; private Camera mainCamera; private CanvasGroup canvas; void Awake() { mainCamera = Camera.main; canvas = GetComponent(); if (canvas == null) { canvas = gameObject.AddComponent(); } } void LateUpdate() { if (mainCamera != null) { if (target != null) { Vector3 pos = mainCamera.WorldToScreenPoint(target.position); pos = new Vector3(pos.x, pos.y, pos.z); transform.position = pos; } else if (targetPos != null) { Vector3 pos = mainCamera.WorldToScreenPoint(targetPos); pos = new Vector3(pos.x, pos.y, pos.z); transform.position = pos; } if (Time.frameCount % 5 == 0) { ResetAlpha(); } } } void ResetAlpha() { if (transform.position.z > 0f && canvas.alpha < 0.1f) { canvas.alpha = 1f; } else if (transform.position.z < 0f && canvas.alpha > 0.9f) { canvas.alpha = 0f; } } }