74 lines
2.4 KiB
C#
74 lines
2.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class LevelMenuRPG : MonoBehaviour
|
|
{
|
|
[Header("Pengaturan Menu RPG")]
|
|
public string kunciProgres = "ProgresRPG";
|
|
|
|
[Header("Daftar Nama Scene (Isi urut Level 1-8)")]
|
|
// Kita pakai array string agar bisa menampung 8 nama scene berbeda
|
|
public string[] namaSceneLevels;
|
|
|
|
[Header("Daftar Tombol")]
|
|
public Button[] levelButtons;
|
|
|
|
void Start()
|
|
{
|
|
// PENGAMAN 1: Pastikan jumlah nama scene sama dengan jumlah tombol
|
|
if (namaSceneLevels.Length != levelButtons.Length)
|
|
{
|
|
Debug.LogError("Riz, jumlah 'Nama Scene Levels' harus sama dengan jumlah 'Level Buttons' (8)!");
|
|
return;
|
|
}
|
|
|
|
// Mengambil data level yang sudah terbuka (Default 1)
|
|
int levelUnlocked = PlayerPrefs.GetInt(kunciProgres, 1);
|
|
|
|
for (int i = 0; i < levelButtons.Length; i++)
|
|
{
|
|
int index = i;
|
|
|
|
if (levelButtons[i] == null) continue;
|
|
|
|
if (i + 1 > levelUnlocked)
|
|
{
|
|
// Kunci tombol jika level belum terbuka
|
|
levelButtons[i].interactable = false;
|
|
Image img = levelButtons[i].GetComponent<Image>();
|
|
if (img != null) img.color = new Color(0.3f, 0.3f, 0.3f, 0.8f);
|
|
}
|
|
else
|
|
{
|
|
// Buka tombol jika level sudah terbuka
|
|
levelButtons[i].interactable = true;
|
|
Image img = levelButtons[i].GetComponent<Image>();
|
|
if (img != null) img.color = Color.white;
|
|
|
|
levelButtons[i].onClick.RemoveAllListeners();
|
|
levelButtons[i].onClick.AddListener(() => PilihLevel(index));
|
|
}
|
|
}
|
|
}
|
|
|
|
void PilihLevel(int index)
|
|
{
|
|
// Membuka scene yang sesuai dengan urutan tombol yang diklik
|
|
string sceneTujuan = namaSceneLevels[index];
|
|
|
|
if (string.IsNullOrEmpty(sceneTujuan))
|
|
{
|
|
Debug.LogError("Riz, nama scene di urutan ke-" + index + " masih kosong di Inspector!");
|
|
return;
|
|
}
|
|
|
|
SceneManager.LoadScene(sceneTujuan);
|
|
}
|
|
|
|
public void HapusDataGameIni()
|
|
{
|
|
PlayerPrefs.DeleteKey(kunciProgres);
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
|
}
|
|
} |