fix:1
This commit is contained in:
68
Assets/Scripts/Tools/IconFollow2D.cs
Normal file
68
Assets/Scripts/Tools/IconFollow2D.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user