MIF_E31230837/Assets/Script/SettingsDropdown.cs

116 lines
3.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SettingsDropdown : MonoBehaviour
{
public GameObject btnSoundOn;
public GameObject btnSoundOff;
public GameObject btnInfo;
public GameObject panelInfo;
public float slideDistance = 90f;
public float duration = 0.3f;
private bool isOpen = false;
private Vector3 soundHide, infoHide;
private Vector3 soundShow, infoShow;
void Start()
{
soundHide = btnSoundOn.GetComponent<RectTransform>().localPosition;
infoHide = btnInfo.GetComponent<RectTransform>().localPosition;
soundShow = soundHide + new Vector3(0, -slideDistance, 0);
infoShow = infoHide + new Vector3(0, -slideDistance * 2, 0);
btnSoundOn.SetActive(false);
btnSoundOff.SetActive(false);
btnInfo.SetActive(false);
panelInfo.SetActive(false);
}
// Ambil status musik dari MusicManager
bool IsSoundOn()
{
if (MusicManager.Instance != null)
return MusicManager.Instance.musikAktif;
return true;
}
public void OnSettingsClick()
{
isOpen = !isOpen;
if (isOpen)
{
// Tampilkan tombol sesuai status musik saat ini
bool soundOn = IsSoundOn();
btnSoundOn.SetActive(soundOn);
btnSoundOff.SetActive(!soundOn);
btnInfo.SetActive(true);
GameObject activeSound = soundOn ? btnSoundOn : btnSoundOff;
StartCoroutine(SlideButton(activeSound, soundHide, soundShow));
StartCoroutine(SlideButton(btnInfo, infoHide, infoShow));
}
else
{
bool soundOn = IsSoundOn();
GameObject activeSound = soundOn ? btnSoundOn : btnSoundOff;
StartCoroutine(SlideAndHide(activeSound, soundShow, soundHide));
StartCoroutine(SlideAndHide(btnInfo, infoShow, infoHide));
}
}
public void OnSoundClick()
{
// Delegasikan ke MusicManager
if (MusicManager.Instance != null)
MusicManager.Instance.KlikToggleMusik();
bool soundOn = IsSoundOn();
// Tukar tampilan tombol
if (soundOn)
{
btnSoundOff.SetActive(false);
btnSoundOn.SetActive(true);
btnSoundOn.GetComponent<RectTransform>().localPosition = soundShow;
}
else
{
btnSoundOn.SetActive(false);
btnSoundOff.SetActive(true);
btnSoundOff.GetComponent<RectTransform>().localPosition = soundShow;
}
}
public void OnInfoClick()
{
panelInfo.SetActive(true);
}
public void TutupPanel()
{
panelInfo.SetActive(false);
}
IEnumerator SlideButton(GameObject obj, Vector3 from, Vector3 to)
{
RectTransform rt = obj.GetComponent<RectTransform>();
float elapsed = 0f;
while (elapsed < duration)
{
elapsed += Time.deltaTime;
rt.localPosition = Vector3.Lerp(from, to, elapsed / duration);
yield return null;
}
rt.localPosition = to;
}
IEnumerator SlideAndHide(GameObject obj, Vector3 from, Vector3 to)
{
yield return StartCoroutine(SlideButton(obj, from, to));
obj.SetActive(false);
}
}