82 lines
2.1 KiB
C#
82 lines
2.1 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public enum LoadingLogoType
|
|
{
|
|
Fire,
|
|
Ice,
|
|
Grass,
|
|
Sand,
|
|
Water,
|
|
Count
|
|
}
|
|
|
|
public class FadeTransition : MonoBehaviour
|
|
{
|
|
public Image fadeImage;
|
|
public List<Image> logoTypes = new List<Image>();
|
|
public float fadeDuration = 1f;
|
|
|
|
private Coroutine currentFade;
|
|
|
|
int currentLogoIndex;
|
|
|
|
public void FadeToLogo(LoadingLogoType logoType)
|
|
{
|
|
if (currentFade != null) StopCoroutine(currentFade);
|
|
|
|
currentLogoIndex = (int)logoType;
|
|
currentFade = StartCoroutine(FadeToAlpha(1f));
|
|
}
|
|
|
|
public void FadeToScene()
|
|
{
|
|
if (currentFade != null) StopCoroutine(currentFade);
|
|
currentFade = StartCoroutine(FadeToAlpha(0f));
|
|
}
|
|
|
|
private IEnumerator FadeToAlpha(float targetAlpha)
|
|
{
|
|
// Get current state
|
|
Color currentColor = fadeImage.color;
|
|
float startAlpha = currentColor.a;
|
|
|
|
// Enable image if fading in
|
|
if (targetAlpha > 0) fadeImage.enabled = true;
|
|
|
|
// Fade over time
|
|
float timer = 0f;
|
|
while (timer < fadeDuration)
|
|
{
|
|
timer += Time.deltaTime;
|
|
float progress = timer / fadeDuration;
|
|
float newAlpha = Mathf.Lerp(startAlpha, targetAlpha, progress);
|
|
|
|
fadeImage.color = new Color(currentColor.r, currentColor.g, currentColor.b, newAlpha);
|
|
HandleLogos(newAlpha);
|
|
yield return null;
|
|
}
|
|
|
|
// Ensure final value
|
|
fadeImage.color = new Color(currentColor.r, currentColor.g, currentColor.b, targetAlpha);
|
|
HandleLogos(targetAlpha);
|
|
|
|
// Disable image if faded out
|
|
if (targetAlpha == 0) fadeImage.enabled = false;
|
|
|
|
currentFade = null;
|
|
}
|
|
|
|
private void HandleLogos(float alpha)
|
|
{
|
|
for (int i = 0; i < logoTypes.Count; i++)
|
|
{
|
|
if (i == currentLogoIndex)
|
|
logoTypes[i].color = new Color(1f, 1f, 1f, alpha);
|
|
else
|
|
logoTypes[i].color = new Color(1f, 1f, 1f, 0f);
|
|
}
|
|
}
|
|
} |