98 lines
2.5 KiB
C#
98 lines
2.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
public class LayerInfoPanel : MonoBehaviour
|
|
{
|
|
[Header("UI References")]
|
|
public GameObject panelRoot;
|
|
public TextMeshProUGUI titleText;
|
|
public TextMeshProUGUI descText;
|
|
public Button closeButton;
|
|
|
|
[Header("Audio Narasi")]
|
|
public Button buttonAudio;
|
|
public AudioClip[] narasiClips; // urutan sesuai index layer yang di-pass ke Show()
|
|
|
|
private AudioSource audioSource;
|
|
private float originalBacksoundVolume = 1f;
|
|
|
|
[Range(0f, 1f)]
|
|
public float backsoundVolumeDuringNarasi = 0.2f;
|
|
|
|
void Awake()
|
|
{
|
|
if (panelRoot != null)
|
|
panelRoot.SetActive(false);
|
|
|
|
var go = new GameObject("LayerNarasiSource");
|
|
go.transform.SetParent(transform);
|
|
audioSource = go.AddComponent<AudioSource>();
|
|
audioSource.playOnAwake = false;
|
|
audioSource.loop = false;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (closeButton != null)
|
|
closeButton.onClick.AddListener(HideFromButton);
|
|
|
|
if (buttonAudio != null)
|
|
buttonAudio.onClick.AddListener(ToggleAudio);
|
|
|
|
if (MusicManager.Instance != null)
|
|
originalBacksoundVolume = MusicManager.Instance.GetVolume();
|
|
}
|
|
|
|
public void Show(string title, string description, int clipIndex = -1)
|
|
{
|
|
titleText.text = title;
|
|
descText.text = description;
|
|
panelRoot.SetActive(true);
|
|
|
|
StopAudio();
|
|
|
|
if (clipIndex >= 0 && narasiClips != null && clipIndex < narasiClips.Length)
|
|
audioSource.clip = narasiClips[clipIndex];
|
|
else
|
|
audioSource.clip = null;
|
|
}
|
|
|
|
public void Hide()
|
|
{
|
|
StopAudio();
|
|
panelRoot.SetActive(false);
|
|
}
|
|
|
|
void HideFromButton()
|
|
{
|
|
Hide();
|
|
}
|
|
|
|
void ToggleAudio()
|
|
{
|
|
if (audioSource.isPlaying)
|
|
{
|
|
StopAudio();
|
|
}
|
|
else
|
|
{
|
|
if (audioSource.clip == null) return;
|
|
// Ambil volume terkini sebelum duck
|
|
if (MusicManager.Instance != null)
|
|
originalBacksoundVolume = MusicManager.Instance.GetVolume();
|
|
audioSource.volume = 1f;
|
|
audioSource.Play();
|
|
if (MusicManager.Instance != null)
|
|
MusicManager.Instance.SetVolume(backsoundVolumeDuringNarasi);
|
|
}
|
|
}
|
|
|
|
void StopAudio()
|
|
{
|
|
if (audioSource != null && audioSource.isPlaying)
|
|
audioSource.Stop();
|
|
if (MusicManager.Instance != null)
|
|
MusicManager.Instance.SetVolume(originalBacksoundVolume);
|
|
}
|
|
} |