69 lines
1.5 KiB
C#
69 lines
1.5 KiB
C#
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<CanvasGroup>();
|
|
if (canvas == null)
|
|
{
|
|
canvas = gameObject.AddComponent<CanvasGroup>();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|