OctoCore 0.0.1
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace OctoCore.Navigation
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles fade in/out transitions for scene loading and teleportation.
|
||||
/// </summary>
|
||||
public class OctoSceneFade : MonoBehaviour
|
||||
{
|
||||
private Canvas _canvas;
|
||||
private Image _fadeImage;
|
||||
|
||||
private IEnumerator fadeOutCoroutine;
|
||||
private IEnumerator fadeInCoroutine;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the fade canvas and image.
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
// Create canvas
|
||||
_canvas = gameObject.AddComponent<Canvas>();
|
||||
_canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
_canvas.sortingOrder = 9999;
|
||||
|
||||
gameObject.AddComponent<CanvasScaler>();
|
||||
gameObject.AddComponent<GraphicRaycaster>();
|
||||
|
||||
// Create fade image
|
||||
GameObject imageObj = new GameObject("FadeImage");
|
||||
imageObj.transform.SetParent(transform);
|
||||
|
||||
_fadeImage = imageObj.AddComponent<Image>();
|
||||
_fadeImage.color = new Color(0, 0, 0, 0);
|
||||
|
||||
RectTransform rt = _fadeImage.GetComponent<RectTransform>();
|
||||
rt.anchorMin = Vector2.zero;
|
||||
rt.anchorMax = Vector2.one;
|
||||
rt.sizeDelta = rt.anchoredPosition = Vector2.zero;
|
||||
|
||||
_canvas.enabled = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fade out to specified color over duration.
|
||||
/// </summary>
|
||||
public void FadeOut(float duration, Color color)
|
||||
{
|
||||
if (fadeOutCoroutine == null)
|
||||
{
|
||||
if (fadeInCoroutine != null)
|
||||
StopCoroutine(fadeInCoroutine);
|
||||
|
||||
StartCoroutine(fadeOutCoroutine = FadeOutCoroutine(duration, color));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fade in from current color to transparent over duration.
|
||||
/// </summary>
|
||||
public void FadeIn(float duration)
|
||||
{
|
||||
if (fadeInCoroutine == null)
|
||||
{
|
||||
if (fadeOutCoroutine != null)
|
||||
StopCoroutine(fadeOutCoroutine);
|
||||
|
||||
StartCoroutine(fadeInCoroutine = FadeInCoroutine(duration));
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator FadeOutCoroutine(float duration, Color color)
|
||||
{
|
||||
_canvas.enabled = true;
|
||||
|
||||
Color startColor = new Color(color.r, color.g, color.b, 0);
|
||||
Color endColor = new Color(color.r, color.g, color.b, 1);
|
||||
|
||||
for (float t = 0; t < duration; t += Time.unscaledDeltaTime)
|
||||
{
|
||||
float normalizedTime = Mathf.Clamp01(t / duration);
|
||||
_fadeImage.color = Color.Lerp(startColor, endColor, normalizedTime);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
_fadeImage.color = endColor;
|
||||
|
||||
fadeOutCoroutine = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fade in from current color to transparent over duration.
|
||||
/// </summary>
|
||||
private IEnumerator FadeInCoroutine(float duration)
|
||||
{
|
||||
Color startColor = _fadeImage.color;
|
||||
|
||||
if (startColor != Color.clear)
|
||||
{
|
||||
Color endColor = new Color(startColor.r, startColor.g, startColor.b, 0);
|
||||
|
||||
for (float t = 0; t < duration; t += Time.unscaledDeltaTime)
|
||||
{
|
||||
float normalizedTime = Mathf.Clamp01(t / duration);
|
||||
_fadeImage.color = Color.Lerp(startColor, endColor, normalizedTime);
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
_fadeImage.color = endColor;
|
||||
_canvas.enabled = false;
|
||||
}
|
||||
|
||||
fadeInCoroutine = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user