MIF_E31230101/Assets/Script Manager/LevelMenuController.cs

86 lines
2.8 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LevelMenuController : MonoBehaviour
{
[Header("Pengaturan Menu")]
public string namaSceneGameplay;
public string kunciProgres;
[Header("Daftar Tombol")]
public Button[] levelButtons;
void Start()
{
// PENGAMAN 1: Cek apakah kunci progres sudah diisi
if (string.IsNullOrEmpty(kunciProgres))
{
Debug.LogError("Riz, 'Kunci Progres' di objek manager masih KOSONG! Isi di Inspector (contoh: ProgresDrag)");
return;
}
// PENGAMAN 2: Cek apakah array tombol sudah diisi
if (levelButtons == null || levelButtons.Length == 0)
{
Debug.LogError("Riz, daftar 'Level Buttons' masih KOSONG! Masukkan tombol-tombol level ke Inspector.");
return;
}
int levelUnlocked = PlayerPrefs.GetInt(kunciProgres, 1);
for (int i = 0; i < levelButtons.Length; i++)
{
int index = i;
// PENGAMAN 3: Cek apakah ada slot tombol yang isinya 'None'
if (levelButtons[i] == null)
{
Debug.LogError("Riz, ada slot tombol yang 'None' di Element " + i + ". Tarik tombol dari Hierarchy!");
continue;
}
if (i + 1 > levelUnlocked)
{
levelButtons[i].interactable = false;
// PENGAMAN 4: Cek apakah tombol punya komponen Image sebelum ganti warna
Image img = levelButtons[i].GetComponent<Image>();
if (img != null) img.color = new Color(0.3f, 0.3f, 0.3f, 0.8f);
}
else
{
levelButtons[i].interactable = true;
Image img = levelButtons[i].GetComponent<Image>();
if (img != null) img.color = Color.white;
// Bersihkan listener lama dulu supaya tidak double
levelButtons[i].onClick.RemoveAllListeners();
levelButtons[i].onClick.AddListener(() => PilihLevel(index));
}
}
}
void PilihLevel(int index)
{
// PENGAMAN 5: Cek nama scene sebelum pindah
if (string.IsNullOrEmpty(namaSceneGameplay))
{
Debug.LogError("Riz, 'Nama Scene Gameplay' masih kosong! Isi di Inspector (contoh: Gameplay_DragDrop)");
return;
}
LevelData.SelectedLevel = index;
SceneManager.LoadScene(namaSceneGameplay);
}
public void HapusDataGameIni()
{
if (!string.IsNullOrEmpty(kunciProgres))
{
PlayerPrefs.DeleteKey(kunciProgres);
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}