AudioManager.cs
3.52 KB
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
using System;
using UnityEngine;
using System.Collections;
using Unity.VisualScripting;
using UnityEngine.Events;
public class AudioManager : Singleton<AudioManager>
{
/// <summary>
/// BGM音效组件
/// </summary>
private AudioSource _bgmAudioSource;
/// <summary>
/// 音效播放组件
/// </summary>
private AudioSource _effectAudioSource;
/// <summary>
/// 完成回调
/// </summary>
private Action _onComplete;
/// <summary>
/// 音频的时长
/// </summary>
private float _clipLength = 0f;
/// <summary>
/// 当前播放音频的时长
/// </summary>
private float _curClipLength = 0f;
/// <summary>
/// 是否正在播放
/// </summary>
/// <returns></returns>
public bool IsPlaying()
{
return _effectAudioSource.isPlaying;
}
private bool _isPlayComplete = true;
public bool IsPlayComplete()
{
return _isPlayComplete;
}
protected override void Awake()
{
//初始化时 挂载AudioSource组件
if (_bgmAudioSource == null)
{
_bgmAudioSource = gameObject.AddComponent<AudioSource>();
}
if (_effectAudioSource == null)
{
_effectAudioSource = gameObject.AddComponent<AudioSource>();
}
}
protected void Update()
{
if (_effectAudioSource != null && _effectAudioSource.isPlaying)
{
// 手动计时
_curClipLength += Time.deltaTime;
if (_curClipLength >= _clipLength)
{
_curClipLength = 0;
StopEffect();
_isPlayComplete = true;
_onComplete?.Invoke();
}
}
}
/// <summary>
/// 播放Bgm
/// </summary>
/// <param name="clip"></param>
public void PlayBGM(AudioClip clip)
{
_bgmAudioSource.clip = clip;
_bgmAudioSource.loop = true;
_bgmAudioSource.Play();
}
/// <summary>
/// 暂停Bgm
/// </summary>
public void PauseBgm()
{
_bgmAudioSource.Pause();
}
/// <summary>
/// 恢复Bgm
/// </summary>
public void ResumeBgm()
{
_bgmAudioSource.UnPause();
}
/// <summary>
/// 播放声音
/// </summary>
/// <param name="clip">音频文件</param>
/// <param name="callBack">回调参数</param>
/// <param name="isLoop">是否循环播放</param>
public void PlayEffect(AudioClip clip, Action callBack = null, bool isLoop = false)
{
if (clip == null)
{
Debug.LogError("音频报错: ");
return;
}
_curClipLength = 0;
_effectAudioSource.Stop();
_isPlayComplete = false;
_onComplete = callBack;
_effectAudioSource.clip = clip;
_effectAudioSource.loop = isLoop;
_clipLength = clip.length;
_effectAudioSource.Play();
}
/// <summary>
/// 停止播放
/// </summary>
public void StopEffect()
{
_effectAudioSource.Stop();
}
/// <summary>
/// 暂停播放
/// </summary>
public void PauseEffect()
{
_effectAudioSource.Pause();
}
/// <summary>
/// 恢复播放
/// </summary>
public void ResumeEffect()
{
_effectAudioSource.UnPause();
}
/// <summary>
/// 静音
/// </summary>
public void Mute()
{
_effectAudioSource.mute = !_effectAudioSource.mute;
_bgmAudioSource.mute = !_bgmAudioSource.mute;
}
}