MIF_E31221357/Assets/Scripts/LevelSelect.cs

58 lines
1.6 KiB
C#

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Firebase.Database;
using Firebase.Extensions;
public class LevelSelect : MonoBehaviour
{
public Button[] buttons;
public string currentMateriName;
private void Start()
{
// Nonaktifkan semua tombol dulu
foreach (var btn in buttons)
btn.interactable = false;
LoadUnlockedLevelFromFirebase();
}
void LoadUnlockedLevelFromFirebase()
{
string kodeLogin = TestManager.Instance?.kodeLogin ?? "default";
string firebasePath = $"users/{kodeLogin}/progress/{currentMateriName}";
FirebaseDatabase.DefaultInstance.GetReference(firebasePath)
.GetValueAsync().ContinueWithOnMainThread(task =>
{
int unlockedLevel = 1;
if (task.IsCompleted && task.Result.Exists)
{
unlockedLevel = int.Parse(task.Result.Value.ToString());
}
// Aktifkan tombol sampai level yang terbuka
for (int i = 0; i < unlockedLevel && i < buttons.Length; i++)
{
buttons[i].interactable = true;
}
Debug.Log($"Unlocked level untuk {kodeLogin} - {currentMateriName}: {unlockedLevel}");
});
}
public void OpenLevel(int levelId)
{
string levelName = "Level " + levelId;
if (LevelControl.Instance != null)
{
LevelControl.Instance.currentMateriName = currentMateriName;
}
SceneManager.LoadScene(levelName);
}
}