156 lines
4.8 KiB
C#
156 lines
4.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
using UnityEngine.EventSystems; // Wajib ada untuk deteksi tombol
|
|
|
|
public class MenuManager : MonoBehaviour
|
|
{
|
|
[Header("Setup Soft Fade")]
|
|
// Tarik Panel yang membungkus semua tombol di scene ini (harus ada Canvas Group)
|
|
// Kalau dikosongi, transisinya bakal langsung pindah (tanpa pudar)
|
|
public CanvasGroup uiPanelMenu;
|
|
|
|
// --- FUNGSI UTAMA (Pasang di Button OnClick) ---
|
|
public void PindahSceneDenganGaya(string namaScene)
|
|
{
|
|
// 1. Bunyikan SFX Tombol
|
|
if (Audio.instance != null)
|
|
{
|
|
Audio.instance.PlayButtonSound();
|
|
}
|
|
|
|
// 2. Cek Tombol mana yang dipencet
|
|
GameObject tombolYgDipencet = EventSystem.current.currentSelectedGameObject;
|
|
|
|
if (tombolYgDipencet != null)
|
|
{
|
|
// Kalau dipanggil dari tombol -> Animasi dulu
|
|
StartCoroutine(AnimasiSquashStretch(tombolYgDipencet.transform, namaScene));
|
|
}
|
|
else
|
|
{
|
|
// Kalau dipanggil lewat kodingan lain -> Langsung pindah
|
|
ExcecutePindah(namaScene);
|
|
}
|
|
}
|
|
|
|
// --- ANIMASI TOMBOL (Squash & Stretch) ---
|
|
IEnumerator AnimasiSquashStretch(Transform tombol, string sceneTujuan)
|
|
{
|
|
float timer = 0;
|
|
|
|
// FASE 1: SQUASH (Gepeng) - 0.1 detik
|
|
while (timer < 0.1f)
|
|
{
|
|
timer += Time.deltaTime;
|
|
float t = timer / 0.1f;
|
|
float scaleX = Mathf.Lerp(1f, 1.2f, t);
|
|
float scaleY = Mathf.Lerp(1f, 0.8f, t);
|
|
tombol.localScale = new Vector3(scaleX, scaleY, 1f);
|
|
yield return null;
|
|
}
|
|
|
|
// FASE 2: STRETCH (Melar) - 0.15 detik
|
|
timer = 0;
|
|
while (timer < 0.15f)
|
|
{
|
|
timer += Time.deltaTime;
|
|
float t = timer / 0.15f;
|
|
t = Mathf.Sin(t * Mathf.PI * 0.5f); // Efek membal
|
|
float scaleX = Mathf.Lerp(1.2f, 0.9f, t);
|
|
float scaleY = Mathf.Lerp(0.8f, 1.1f, t);
|
|
tombol.localScale = new Vector3(scaleX, scaleY, 1f);
|
|
yield return null;
|
|
}
|
|
|
|
// FASE 3: NORMAL - 0.2 detik
|
|
timer = 0;
|
|
while (timer < 0.2f)
|
|
{
|
|
timer += Time.deltaTime;
|
|
float t = timer / 0.2f;
|
|
float scaleX = Mathf.Lerp(0.9f, 1f, t);
|
|
float scaleY = Mathf.Lerp(1.1f, 1f, t);
|
|
tombol.localScale = new Vector3(scaleX, scaleY, 1f);
|
|
yield return null;
|
|
}
|
|
|
|
tombol.localScale = Vector3.one;
|
|
|
|
// Setelah animasi selesai, baru jalankan pindah scene
|
|
ExcecutePindah(sceneTujuan);
|
|
}
|
|
|
|
// --- LOGIKA PEMILIHAN TRANSISI & AUDIO ---
|
|
void ExcecutePindah(string sceneTujuan)
|
|
{
|
|
// 1. Atur Musik
|
|
CekGantiLagu(sceneTujuan);
|
|
|
|
string currentScene = SceneManager.GetActiveScene().name;
|
|
|
|
// 2. PILIH JENIS TRANSISI
|
|
|
|
// === TIM BLACK FADE (Layar Gelap) ===
|
|
// Masuk ke sini: AR, Splash, dan Game Kuis (KuisUnsur/KuisSenyawa)
|
|
if (sceneTujuan == "ARMenu" || sceneTujuan == "Splash" ||
|
|
sceneTujuan == "KuisUnsur" || sceneTujuan == "KuisSenyawa" ||
|
|
(currentScene == "Splash" && sceneTujuan == "MainMenu"))
|
|
{
|
|
if (Transition.instance != null)
|
|
Transition.instance.LoadSceneBlack(sceneTujuan);
|
|
else
|
|
SceneManager.LoadScene(sceneTujuan);
|
|
}
|
|
|
|
// === TIM SOFT FADE (Pudar Biasa) ===
|
|
// Masuk ke sini: PilihKuis, PilihBelajar, MainMenu, dll.
|
|
else
|
|
{
|
|
StartCoroutine(FadeUIAndLoad(sceneTujuan));
|
|
}
|
|
}
|
|
|
|
// Coroutine untuk Soft Fade (Background Tetap, UI Menghilang)
|
|
IEnumerator FadeUIAndLoad(string sceneTujuan)
|
|
{
|
|
if (uiPanelMenu != null)
|
|
{
|
|
float timer = 0;
|
|
while(timer < 0.3f)
|
|
{
|
|
timer += Time.deltaTime;
|
|
// Alpha turun dari 1 (Jelas) ke 0 (Hilang)
|
|
uiPanelMenu.alpha = 1 - (timer / 0.3f);
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
SceneManager.LoadScene(sceneTujuan);
|
|
}
|
|
|
|
// Logika Ganti Lagu (DJ Otomatis)
|
|
void CekGantiLagu(string sceneTujuan)
|
|
{
|
|
if (Audio.instance == null) return;
|
|
|
|
// Kalau ada kata "Kuis" di nama scenenya -> Musik Tegang
|
|
if (sceneTujuan == "KuisUnsur"|| sceneTujuan == "KuisSenyawa")
|
|
{
|
|
Audio.instance.PlayQuizMusic();
|
|
}
|
|
// Sisanya (Menu, Belajar, AR) -> Musik Utama
|
|
else
|
|
{
|
|
Audio.instance.PlayMainMusic();
|
|
}
|
|
}
|
|
|
|
// Fungsi Keluar Aplikasi
|
|
public void KeluarAplikasi()
|
|
{
|
|
if (Audio.instance != null) Audio.instance.PlayButtonSound();
|
|
Debug.Log("Keluar Game");
|
|
Application.Quit();
|
|
}
|
|
} |