Files
beyond/Assets/Scripts/Tutorial/CanvasGroupFadeIn.cs
2024-11-20 15:21:28 +01:00

43 lines
1.1 KiB
C#

using UnityEngine;
public class CanvasGroupFadeIn : MonoBehaviour
{
private CanvasGroup canvasGroup; // Automatically finds the CanvasGroup
public float fadeSpeed = 1f; // Speed of the fade animation
private void Awake()
{
// Automatically find the CanvasGroup component on this GameObject
canvasGroup = GetComponent<CanvasGroup>();
if (canvasGroup == null)
{
Debug.LogError("No CanvasGroup found on the object. Please attach a CanvasGroup component.");
return;
}
// Set alpha to 0 at the start
canvasGroup.alpha = 0f;
canvasGroup.blocksRaycasts = false;
}
private void Start()
{
StartCoroutine(FadeIn());
}
private System.Collections.IEnumerator FadeIn()
{
while (canvasGroup.alpha < 1f)
{
// Smoothly increase alpha based on fadeSpeed
canvasGroup.alpha += Time.deltaTime * fadeSpeed;
yield return null;
}
// Ensure alpha is exactly 1 at the end
canvasGroup.alpha = 1f;
canvasGroup.blocksRaycasts = true; // Enable interaction after fade in
}
}