오디오
2022. 6. 12. 15:49ㆍUnity/스크립트 기본
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
public AudioSource musicSource; // 인스펙터에서 할당
public AudioSource sfxSource; // 인스펙터에서 할당
public AudioClip[] musicClips;
public AudioClip[] sfxClips;
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
PlayMusic(0);
}
public void PlayMusic(int index)
{
musicSource.clip = musicClips[index];
musicSource.Play();
}
public void PlaySFX(int index)
{
sfxSource.PlayOneShot(sfxClips[index]);
}
public void StopMusic()
{
musicSource.Stop();
}
public void SetMusicVolume(float volume)
{
musicSource.volume = volume;
}
public void SetSFXVolume(float volume)
{
sfxSource.volume = volume;
}
}
728x90