MIF_E31230838/Assets/Scripts/Transition.cs

64 lines
1.6 KiB
C#

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;
public class Transition : MonoBehaviour
{
public static Transition instance;
[Header("Komponen")]
public CanvasGroup blackScreen; // Tarik object BlackScreen ke sini
void Awake()
{
// Singleton (Anti Hancur)
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
// === TIPE 1: FADE BLACK (Layar Gelap Total) ===
public void LoadSceneBlack(string sceneName)
{
StartCoroutine(ProcessFadeBlack(sceneName));
}
IEnumerator ProcessFadeBlack(string sceneName)
{
blackScreen.blocksRaycasts = true; // Blokir klik user
// 1. Gelapkan Layar (Alpha 0 -> 1)
float timer = 0;
while (timer < 0.5f)
{
timer += Time.deltaTime;
blackScreen.alpha = timer / 0.5f;
yield return null;
}
blackScreen.alpha = 1;
// 2. Pindah Scene
SceneManager.LoadScene(sceneName);
// Tunggu dikit biar scene baru loading sempurna
yield return new WaitForSeconds(0.2f);
// 3. Terangkan Layar (Alpha 1 -> 0)
timer = 0;
while (timer < 0.5f)
{
timer += Time.deltaTime;
blackScreen.alpha = 1 - (timer / 0.5f);
yield return null;
}
blackScreen.alpha = 0;
blackScreen.blocksRaycasts = false; // Buka blokir
}
}