78 lines
1.9 KiB
C#
78 lines
1.9 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
namespace Michsky.UI.Heat
|
|
{
|
|
[AddComponentMenu("Heat UI/Animation/Shiny Image")]
|
|
public class ShinyImage : MonoBehaviour
|
|
{
|
|
[Header("Resources")]
|
|
[SerializeField]
|
|
private RectTransform targetRect;
|
|
|
|
[SerializeField]
|
|
private RectTransform parentRect;
|
|
|
|
[Header("Animation")]
|
|
[SerializeField]
|
|
[Range(0f, 4f)]
|
|
private float delay = 0.25f;
|
|
|
|
[SerializeField]
|
|
[Range(0.25f, 4f)]
|
|
private float animationSpeed = 1f;
|
|
|
|
[SerializeField]
|
|
private AnimationCurve animationCurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(1f, 1f));
|
|
|
|
private void OnEnable()
|
|
{
|
|
StartShiny();
|
|
}
|
|
|
|
public void StartShiny()
|
|
{
|
|
base.gameObject.SetActive(value: true);
|
|
if (targetRect == null)
|
|
{
|
|
targetRect = GetComponent<RectTransform>();
|
|
}
|
|
if (parentRect == null)
|
|
{
|
|
parentRect = base.transform.parent.GetComponent<RectTransform>();
|
|
}
|
|
StopCoroutine("ShinyAnimation");
|
|
StartCoroutine("ShinyAnimation");
|
|
}
|
|
|
|
private IEnumerator ShinyAnimation()
|
|
{
|
|
float elapsedTime = 0f;
|
|
Vector2 startPos = new Vector2(0f - parentRect.sizeDelta.x, targetRect.anchoredPosition.y);
|
|
Vector2 endPos = new Vector2(parentRect.sizeDelta.x, targetRect.anchoredPosition.y);
|
|
targetRect.anchoredPosition = startPos;
|
|
while (targetRect.anchoredPosition.x < endPos.x - 0.1f)
|
|
{
|
|
elapsedTime += Time.unscaledDeltaTime;
|
|
targetRect.anchoredPosition = new Vector2(Mathf.Lerp(startPos.x, endPos.x, animationCurve.Evaluate(elapsedTime * animationSpeed)), targetRect.anchoredPosition.y);
|
|
yield return null;
|
|
}
|
|
targetRect.anchoredPosition = endPos;
|
|
if (delay == 0f)
|
|
{
|
|
StartCoroutine("ShinyAnimation");
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine("ShinyAnimationDelay");
|
|
}
|
|
}
|
|
|
|
private IEnumerator ShinyAnimationDelay()
|
|
{
|
|
yield return new WaitForSecondsRealtime(delay);
|
|
StartCoroutine("ShinyAnimation");
|
|
}
|
|
}
|
|
}
|