-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAudioSourceExtension.cs
96 lines (79 loc) · 2.81 KB
/
AudioSourceExtension.cs
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
using System.Collections;
using UnityEngine;
namespace LCSoundTool
{
public class AudioSourceExtension : MonoBehaviour
{
public AudioSource audioSource;
public bool playOnAwake = false;
public bool loop = false;
private bool updateHasBeenLogged = false;
private bool hasPlayed = false;
void OnEnable()
{
if (audioSource == null)
return;
//if (audioSource.clip == null)
// return;
if (audioSource.isPlaying)
return;
if (playOnAwake)
audioSource.Play();
if (SoundTool.infoDebugging)
SoundTool.Instance.logger.LogDebug($"(AudioSourceExtension) Started playback of {audioSource} with clip {audioSource.clip} in OnEnable function!");
updateHasBeenLogged = false;
hasPlayed = false;
}
void OnDisable()
{
if (audioSource == null)
return;
//if (audioSource.clip == null)
// return;
if (!audioSource.isPlaying)
return;
if (playOnAwake)
audioSource.Stop();
if (SoundTool.infoDebugging)
SoundTool.Instance.logger.LogDebug($"(AudioSourceExtension) Stopped playback of {audioSource} with clip {audioSource.clip} in OnDisable function!");
updateHasBeenLogged = false;
hasPlayed = false;
}
void Update()
{
if (audioSource == null)
return;
if (audioSource.clip == null)
{
hasPlayed = false;
}
if (audioSource.isPlaying)
{
updateHasBeenLogged = false;
return;
}
if (!audioSource.isActiveAndEnabled)
{
hasPlayed = false;
return;
}
if (playOnAwake)
{
if (audioSource.clip != null && !hasPlayed)
{
audioSource.Play();
if (SoundTool.infoDebugging)
SoundTool.Instance.logger.LogDebug($"(AudioSourceExtension) Started playback of {audioSource} with clip {audioSource.clip} in Update function!");
updateHasBeenLogged = false;
hasPlayed = true;
}
else if (audioSource.clip == null && !updateHasBeenLogged)
{
updateHasBeenLogged = true;
if (SoundTool.infoDebugging)
SoundTool.Instance.logger.LogDebug($"(AudioSourceExtension) Can not start playback of {audioSource} with missing clip in Update function!");
}
}
}
}
}