94 lines
2.0 KiB
C#
94 lines
2.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class AudioGame : MonoBehaviour
|
|
{
|
|
public static AudioGame Instance;
|
|
|
|
[Header("Audio Source")]
|
|
[SerializeField] AudioSource musicSource;
|
|
[SerializeField] AudioSource SFXSourse;
|
|
|
|
[Header("Audio Clip")]
|
|
public AudioClip bgMusic1;
|
|
public AudioClip bgMusic2;
|
|
public AudioClip bgMusic3;
|
|
public AudioClip correct;
|
|
public AudioClip incorrect;
|
|
public AudioClip gameOver;
|
|
public AudioClip popUp;
|
|
public AudioClip click;
|
|
|
|
[Header("Audio Materi")]
|
|
public AudioClip[] audioMateri;
|
|
|
|
private string currentSceneName;
|
|
|
|
void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
}
|
|
|
|
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
string sceneName = scene.name;
|
|
if (sceneName == currentSceneName) return; // skip jika scene tidak berubah
|
|
currentSceneName = sceneName;
|
|
|
|
if (sceneName == "Screen Menu" && sceneName == "InputUser")
|
|
{
|
|
PlayMusic(bgMusic1);
|
|
}
|
|
else if (sceneName.StartsWith("Level"))
|
|
{
|
|
PlayMusic(bgMusic2);
|
|
}
|
|
else
|
|
{
|
|
PlayMusic(bgMusic3);
|
|
}
|
|
}
|
|
|
|
void PlayMusic(AudioClip clip)
|
|
{
|
|
if (musicSource.clip == clip) return; // skip jika lagu sama
|
|
musicSource.clip = clip;
|
|
musicSource.Play();
|
|
}
|
|
|
|
public void PlaySFX(AudioClip clip)
|
|
{
|
|
if (clip != null)
|
|
{
|
|
SFXSourse.PlayOneShot(clip);
|
|
}
|
|
}
|
|
|
|
public void PlayAksara(int id)
|
|
{
|
|
if (id >= 0 && id < audioMateri.Length )
|
|
{
|
|
SFXSourse.PlayOneShot(audioMateri[id]);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("id suara materi tidak ditemaukan:" + id);
|
|
}
|
|
}
|
|
|
|
|
|
}
|