2016. 2. 22. 05:54

유니티에서 사운드 재생 하기 위해 만든 매니저입니다.


간단한 사운드부터 거리설정 및 포지션 설정 사운드까지 지원합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class SoundManager : MonoBehaviour
{
 
    public AudioClip[] BGMSounds;
    public AudioClip[] EffectSounds;
 
    public bool isSfxMute = false;
    [Range(0.0f, 1.0f)]
    public float sfxVolume = 1.0f;
 
    public Vector3 SoundPlayPosition;
 
    float DefaultMinDistance = 10.0f;
    float DefaultMaxDistance = 30.0f;
    public void SimplePlayBGM(int Index)
    {
        PlaySfx(BGMSounds[Index]);
    }
    public void SimplePlayEffect(int Index)
    {
        PlaySfx(EffectSounds[Index]);
    }
    public void PlaySfx(AudioClip sfx, Vector3 _pos, float _MinDistance, float _MaxDistance, float _Volume)
    {
        if (isSfxMute) return;
 
        GameObject SoundPlayObject = new GameObject("Sfx_" + sfx.name);
        SoundPlayObject.transform.position = _pos;
 
        AudioSource _AudioSource = SoundPlayObject.AddComponent<AudioSource>();
        _AudioSource.clip = sfx;
        _AudioSource.minDistance = _MinDistance;
        _AudioSource.maxDistance = _MaxDistance;
        _AudioSource.volume = sfxVolume;
 
        _AudioSource.Play();
 
        Destroy(SoundPlayObject, sfx.length);
    }
    public void PlaySfx(AudioClip sfx)
    {
        PlaySfx(sfx,SoundPlayPosition, DefaultMinDistance, DefaultMaxDistance, sfxVolume);
    }
    public void PlaySfx(AudioClip sfx, float _MinDistance, float _MaxDistance)
    {
        PlaySfx(sfx,SoundPlayPosition, _MinDistance, _MaxDistance, sfxVolume);
    }
    public void PlaySfx(AudioClip sfx, Vector3 _pos)
    {
        PlaySfx(sfx, _pos, DefaultMinDistance, DefaultMaxDistance, sfxVolume);
    }
    public void PlaySfx(AudioClip sfx, Vector3 _pos, float _MinDistance, float _MaxDistance)
    {
        PlaySfx(sfx, _pos, _MinDistance, _MaxDistance, sfxVolume);
    }
    public void PlaySfx(AudioClip sfx, Vector3 _pos, float sfxVolume)
    {
        PlaySfx(sfx, _pos, DefaultMinDistance, DefaultMaxDistance, sfxVolume);
    }
}
cs


Posted by 시리시안