44 lines
775 B
C#
44 lines
775 B
C#
using UnityEngine;
|
|
|
|
[ExecuteInEditMode]
|
|
public class FadeUGUI : MonoBehaviour
|
|
{
|
|
public float invisibleDistance = 100f;
|
|
|
|
public float fadeDistance = 70f;
|
|
|
|
public Transform target;
|
|
|
|
private EnergyBarBase energyBarBase;
|
|
|
|
private void OnEnable()
|
|
{
|
|
energyBarBase = GetComponent<EnergyBarBase>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!(target == null))
|
|
{
|
|
float magnitude = (Camera.main.transform.position - target.position).magnitude;
|
|
if (magnitude < fadeDistance)
|
|
{
|
|
SetAlpha(1f);
|
|
}
|
|
else if (magnitude > invisibleDistance)
|
|
{
|
|
SetAlpha(0f);
|
|
}
|
|
else
|
|
{
|
|
SetAlpha((magnitude - fadeDistance) / (invisibleDistance - fadeDistance));
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetAlpha(float alpha)
|
|
{
|
|
energyBarBase.tint = new Color(1f, 1f, 1f, alpha);
|
|
}
|
|
}
|