친구가 만들었다고 하길래 나도 만들어본 사운드 매니저.


효과음이나 배경음악을 파일이름으로 재생할 수 있다.

심지어 배경음악 바꿀때 천천히 바꿀 수 도 있음 ㅎ


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class SoundManager : MonoBehaviour {
 
    //싱글톤
    private static SoundManager instance;
    public static SoundManager GetInstance()
    {
        if (!instance)
        {
            instance = GameObject.FindObjectOfType(typeof(SoundManager)) as SoundManager;
            if (!instance)
                Debug.Log("오류");
        }
 
        return instance;
    }
 
 
    public int audioSourceCount = 3;//최대 오디오 소스 개수
 
    [SerializeField]
    [Header("clips"), Tooltip("오디오 클립들")]
    public AudioClip[] BGMs = new AudioClip[2];
    public AudioClip[] SFXs = new AudioClip[3];
 
 
    private AudioSource BGMsource;
    private AudioSource[] SFXsource;
 
    private void OnEnable ()
    {
        BGMsource = gameObject.AddComponent<AudioSource>();
        BGMsource.playOnAwake = false;
        BGMsource.loop = true;
 
        //sfx 소스 초기화
        SFXsource = new AudioSource[audioSourceCount];
 
        for(int i=0;i< SFXsource.Length; i++)
        {
            SFXsource[i] = gameObject.AddComponent<AudioSource>();
            SFXsource[i].playOnAwake = false;
        }
    }
    
    /**********SFX***********/
 
    public void PlaySFX(string name)//효과음 재생
    {
        for(int i=0;i<SFXs.Length;i++)
        {
            if(SFXs[i].name == name)
            {
                GetEmptySource().clip = SFXs[i];
                GetEmptySource().Play();
                return;
            }
        }
    }
 
    private AudioSource GetEmptySource()//비어있는 오디오 소스 반환
    {
        int lageindex = 0;
        float lageProgress = 0;
        for (int i = 0; i < SFXsource.Length; i++)
        {
            if (!SFXsource[i].isPlaying)
            {
                return SFXsource[i];
            }
 
            //만약 비어있는 오디오 소스를 못찿으면 가장 진행도가 높은 오디오 소스 반환
 
            float progress = SFXsource[i].time / SFXsource[i].clip.length;
            if(progress > lageProgress)
            {
                lageindex = i;
                lageProgress = progress;
            }
        }
        return SFXsource[lageindex];
    }
 
    /**********BGM***********/
 
    private AudioClip changeClip;//바뀌는 클립
    private bool isChanging =false;
    private float startTime;
 
 
    [SerializeField]
    [Header("Changing speed"), Tooltip("브금 바꾸는 속도")]
    public float ChangingSpeed;
 
    public void ChangeBGM(string name, bool isSmooth)//브금 변경 (브금이름 , 부드럽게 바꾸기)
    {
        changeClip = null;
        for (int i = 0; i < BGMs.Length; i++)//브금 클립 탐색
        {
            if (BGMs[i].name == name)
            {
                changeClip = BGMs[i];
            }
        }
 
        if (changeClip == null)//없으면 탈주
            return;
 
        if (isSmooth)//스무스 하게 체인지 ~
        {
            startTime = Time.time;
            isChanging = true;
        }
        else
        {
            BGMsource.clip = changeClip;
            BGMsource.Play();
        }
    }
 
    private void Update()
    {
        if (!isChanging) return;
 
        float progress = (Time.time - startTime) * ChangingSpeed;//부드러운 오디오 전환
        BGMsource.volume = Mathf.Lerp(10, progress);
 
        if(progress > 1)
        {
            isChanging = false;
            BGMsource.clip = changeClip;
            BGMsource.Play();
 
            Debug.Log("BGM CHANGE DONE!");
        }
    }
 
}
cs



사실 브금 부분은 아직 제대로 테스트 안해봤음


+ 이 소스 쓰지 마세요 ㅎㅎ

실제로 써보니깐 여러가지 문제가 있었습니다.

문제점들 수정하면 쓸만한데

쓸만하게 만들 자신 있으면 차라리 자기거 만드는게 이득입니다 ㅎ

블로그 이미지

stuban

ㅇ.ㅇ

,