Files
GeoSusGame/Assets/Scripts/ArrowSequence.cs
2026-05-17 10:50:17 +02:00

84 lines
2.2 KiB
C#

using System.Collections;
using UnityEngine;
public class ArrowSequence : MonoBehaviour
{
[Header("Šipky")]
public GameObject[] arrows;
[Header("Časování")]
public float fadeInDuration = 0.3f;
public float visibleDuration = 0.5f;
public float fadeOutDuration = 0.3f;
public float delayBetween = 0.1f;
void Start()
{
foreach (var arrow in arrows)
SetAlpha(arrow, 0f);
StartCoroutine(PlayLoop());
}
IEnumerator PlayLoop()
{
float singleArrow = fadeInDuration + visibleDuration + fadeOutDuration;
float fullCycle = arrows.Length * delayBetween + singleArrow;
float restartAt = fullCycle / 2f;
while (true)
{
StartCoroutine(PlaySequence());
yield return new WaitForSeconds(restartAt);
}
}
IEnumerator PlaySequence()
{
foreach (var arrow in arrows)
{
StartCoroutine(AnimateArrow(arrow));
yield return new WaitForSeconds(delayBetween);
}
}
IEnumerator AnimateArrow(GameObject arrow)
{
yield return StartCoroutine(Fade(arrow, 0f, 1f, fadeInDuration));
yield return new WaitForSeconds(visibleDuration);
yield return StartCoroutine(Fade(arrow, 1f, 0f, fadeOutDuration));
}
IEnumerator Fade(GameObject obj, float from, float to, float duration)
{
float elapsed = 0f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
float t = Mathf.SmoothStep(0f, 1f, elapsed / duration);
SetAlpha(obj, Mathf.Lerp(from, to, t));
yield return null;
}
SetAlpha(obj, to);
}
void SetAlpha(GameObject obj, float alpha)
{
var images = obj.GetComponentsInChildren<UnityEngine.UI.Image>(true);
foreach (var img in images)
{
Color c = img.color;
c.a = alpha;
img.color = c;
}
var renderers = obj.GetComponentsInChildren<SpriteRenderer>(true);
foreach (var sr in renderers)
{
Color c = sr.color;
c.a = alpha;
sr.color = c;
}
}
}