58 lines
1.3 KiB
C#
58 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class BacksoundMusic : MonoBehaviour
|
|
{
|
|
public static BacksoundMusic instance;
|
|
public AudioSource backsoundMusic;
|
|
|
|
// Scene index yang ingin kamu nonaktifkan backsound-nya
|
|
public int[] stopInScenes;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance == null)
|
|
{
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
// Cek apakah scene saat ini termasuk dalam daftar scene yang perlu stop backsound
|
|
if (ShouldStopBacksound(scene.buildIndex))
|
|
{
|
|
if (backsoundMusic.isPlaying)
|
|
backsoundMusic.Stop();
|
|
}
|
|
else
|
|
{
|
|
if (!backsoundMusic.isPlaying)
|
|
backsoundMusic.Play();
|
|
}
|
|
}
|
|
|
|
private bool ShouldStopBacksound(int sceneIndex)
|
|
{
|
|
foreach (int index in stopInScenes)
|
|
{
|
|
if (index == sceneIndex)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
}
|
|
}
|