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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System.Security.Cryptography.X509Certificates;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public enum AudioEnum
{
    BGM1,
    BGM2,
    UI,//UI按键
    UIDisable,//UI不可点击_错误
    BuffAppear,//翻书
    ChooseBuff,//选择buff后
    End,//游戏结束
    BossDie,//BOSS死亡
    AttackTower,//敌人攻击塔
    DragTower,//拖动宝石时
    PutTower,//放下宝石
    FireTAttack,//火攻击
    WaterTAttack,//水攻击
    WoodTAttack,//木攻击
    LightningSkill,//电技能
    FireSkill,//火技能
    FinalScore,//结算分数滚动效果
    BossWaterSkill,//气泡
    WaterTowerSkill,//冰淇淋技能
    WoodSkill,//玉米狙击子弹
    Upgrade,//合成
    Gold,//金币
    GuideOpenBox,//新手引导开宝箱
    OpenTowerPlace,//开启塔位
 
}
public class AudioSourceManager : MonoBehaviour
{
    private static AudioSourceManager _ins;
 
    public static AudioSourceManager Ins
    {
        get { return _ins; }
    }
 
    private void Awake()
    {
        _ins = this;
 
        clipDic = new Dictionary<string, AudioClip>();
 
        audioSourceList = new List<AudioSource>();
        for (int i = 0; i < 5; i++)
        {
            audioSourceList.Add(gameObject.AddComponent<AudioSource>());
        }
    }
 
    private AudioSource audioSource;
    private List<AudioSource> audioSourceList = new List<AudioSource>();
 
    /// <summary>
    /// 跳过第一个,第一个用来播放背景音 获取一个空的组件
    /// </summary>
    /// <returns></returns>
    private AudioSource GetAudioSource()
    {
        for (int i = 1; i < audioSourceList.Count; i++)
        {
            if (!audioSourceList[i].isPlaying)
            {
                return audioSourceList[i];
            }
        }
 
        AudioSource tmpAudioSource = gameObject.AddComponent<AudioSource>();
        audioSourceList.Add(tmpAudioSource);
        return tmpAudioSource;
    }
 
    public AudioClip LoadAudioClip(string musicName)
    {
        if (clipDic.ContainsKey(musicName))
            return clipDic[musicName];
        else
        {
            AudioClip au = Resources.Load<AudioClip>("Music/" + musicName);
            clipDic.Add(musicName, au);
            return au;
        }
    }
 
    public void Play(AudioEnum en)
    {
        // Debug.Log("播放了音效:"+en);
        switch (en)
        {
            case AudioEnum.BGM1:
                Play("BGM1", false);
                break;
            case AudioEnum.BGM2:
                Play("BGM2", false);
                break;
            case AudioEnum.UI:
                Play("UI按键", true);
                break;
            case AudioEnum.UIDisable:
                Play("不可点击_错误", true);
                break;
            case AudioEnum.BuffAppear:
                Play("翻书", true);
                break;
            case AudioEnum.ChooseBuff:
                StopOneShot("选择buff后", true);
                //Play("选择buff后", true);
                break;
            case AudioEnum.End:
                Play("游戏结束", true);
                break;
            case AudioEnum.BossDie:
                Play("BOSS死亡", true);
                break;
            case AudioEnum.AttackTower:
                Play("敌人攻击塔", true);
                break;
            case AudioEnum.DragTower:
                Play("拖动宝石时", true);
                break;
            case AudioEnum.PutTower:
                Play("放下宝石", true);
                break;
            case AudioEnum.FireTAttack:
                Play("火攻击", true, 0.5f);
                break;
            case AudioEnum.WaterTAttack:
                Play("水攻击", true, 0.5f);
                break;
            case AudioEnum.WoodTAttack:
                Play("木攻击", true, 0.5f);
                break;
            case AudioEnum.LightningSkill:
                Play("电技能", true);
                break;
            case AudioEnum.FireSkill:
                Play("火焰喷射", true);
                break;
            case AudioEnum.FinalScore:
                Play("结算分数", true);
                break;
            case AudioEnum.BossWaterSkill:
                //Play("气泡", true);
                PlayBossWaterSkill("气泡");
                break;
            case AudioEnum.WaterTowerSkill:
                Play("冰淇淋技能", true);
                break;
            case AudioEnum.WoodSkill:
                Play("玉米狙击子弹", true);
                break;
            case AudioEnum.Upgrade:
                Play("合成", true);
                break;
            case AudioEnum.Gold:
                Play("金币", true);
                break;
            case AudioEnum.GuideOpenBox:
                Play("新手引导开宝箱", true);
                break;
            case AudioEnum.OpenTowerPlace:
                Play("开启塔位", true);
                break;
 
        }
    }
 
 
    Dictionary<string, AudioClip> clipDic;
 
    private void Play(string musicName, bool isOneShot, float volume = 1.0f)
    {
        AudioClip au = LoadAudioClip(musicName);
        AudioSource tmpAudioSource;
        //tmpAudioSource.volume = PlayerPrefs.GetFloat("BgmVal");
 
        if (isOneShot)
        {
            tmpAudioSource = GetAudioSource();
            tmpAudioSource.clip = au;
            tmpAudioSource.loop = false;
 
            //tmpAudioSource.PlayOneShot(au);//无法立刻暂停
            StartCoroutine(RemoAudio(tmpAudioSource, au));
        }
        else
        {
            tmpAudioSource = audioSourceList[0];
            if (tmpAudioSource.clip == au)
            {
                return;
            }
            tmpAudioSource.clip = null;
            tmpAudioSource.clip = au;
            tmpAudioSource.loop = true;
        }
        tmpAudioSource.Play();
        tmpAudioSource.volume = volume;
    }
 
    AudioSource bossWaterSkillAS;
    /// <summary>
    /// BOSS气泡技能 38秒钟,所以不用循环播放
    /// </summary>
    /// <param name="musicName"></param>
    /// <param name="volume"></param>
    private void PlayBossWaterSkill(string musicName, float volume = 1.0f)
    {
        Debug.Log("BOSS气泡技能 38秒钟,所以不用循环播放");
 
        AudioClip au = LoadAudioClip(musicName);
        bossWaterSkillAS = GetAudioSource();
        bossWaterSkillAS.clip = au;
        bossWaterSkillAS.loop = false;
 
        bossWaterSkillAS.Play();
        bossWaterSkillAS.volume = volume;
    }
 
    /// <summary>
    /// BOSS气泡技能结束
    /// </summary>
    public void StopBossWaterSkill()
    {
        Debug.Log("BOSS气泡技能结束");
        if (bossWaterSkillAS != null)
        {
            bossWaterSkillAS.Stop();
            bossWaterSkillAS.clip = null;
            if (audioSourceList.Count > 5)
            {
                audioSourceList.Remove(bossWaterSkillAS);
                Destroy(bossWaterSkillAS);
                bossWaterSkillAS = null;
            }
        }
    }
 
    /// <summary>
    /// 暂停背景音乐
    /// </summary>
    public void StopBGAudio()
    {
        if (audioSourceList[0].isPlaying)
        {
            audioSourceList[0].Stop();
        }
    }
 
    /// <summary>
    /// 继续背景音乐
    /// </summary>
    public void RestartBGAudio()
    {
        if (!audioSourceList[0].isPlaying)
            audioSourceList[0].Play();
    }
 
 
    /// <summary>
    /// 销毁音效
    /// </summary>
    /// <param name="audioClip"></param>
    private IEnumerator RemoAudio(AudioSource tmpAudioSource, AudioClip au)
    {
        if (audioSourceList.Count > 5)
        {
            yield return new WaitForSeconds(au.length);
            audioSourceList.Remove(tmpAudioSource);
            Destroy(tmpAudioSource);
        }
        yield break;
    }
 
 
    /// <summary>
    /// 调整音效时更新音量
    /// </summary>
    public void UpdateAudioVal()
    {
        foreach (AudioSource val in audioSourceList)
        {
            //便利所有的Bgm ,设置音量
            if (val)
            {
                val.volume = PlayerPrefs.GetFloat("BgmVal");
            }
 
        }
    }
 
    /// <summary>
    /// 删除指定音效
    /// </summary>
    /// <param name="audioName">被删除的音乐</param>
    public void DeleteAudio(string audioName)
    {
        foreach (AudioSource val in audioSourceList)
        {
            if (val && val.clip.name == audioName)
            {
                audioSourceList.Remove(val);
                Destroy(val.gameObject);
                break;
            }
        }
    }
 
    /// <summary>
    /// 音乐暂停
    /// </summary>
    /// <param name="isStop">是否暂停</param>
    public void StopAudio(bool isStop)
    {
        foreach (AudioSource val in audioSourceList)
        {
            if (isStop && val)
            {
                val.Pause();
            }
            else if (val)
            {
                val.Play();
            }
        }
    }
 
    /// <summary>
    /// 判断当前音乐是否存在
    /// </summary>
    /// <param name="audioName">音乐名字</param>
    /// <returns></returns>
    private bool AudioIsTrue(string audioName)
    {
        foreach (AudioSource val in audioSourceList)
        {
            if (val && val.clip.name == audioName)
            {
                return true;
            }
 
        }
        return false;
    }
 
    /// <summary>
    /// 停止播放某一个音效
    /// </summary>
    /// <param name="audioName"></param>
    private void StopOneShot(string musicName, bool isOneShot)
    {
        foreach (AudioSource val in audioSourceList)
        {
            if (val && val.clip && val.clip.name == musicName)
            {
                val.Stop();
                val.clip = null;
                break;
            }
        }
 
        Play(musicName, isOneShot);
    }
 
}