|
7 | 7 | using UnityEngine; |
8 | 8 | using UnityEngine.Networking; |
9 | 9 |
|
10 | | -namespace Overlayer.Scripting |
11 | | -{ |
12 | | - public static class AudioPlayer |
13 | | - { |
14 | | - static List<AudioSource> sources = new List<AudioSource>(); |
15 | | - static Dictionary<string, AudioClip> clips = new Dictionary<string, AudioClip>(); |
16 | | - public static void Play(Sound sound) |
17 | | - { |
18 | | - if (sound?.sound == null) return; |
19 | | - if (clips.TryGetValue(sound.sound, out var clip)) |
20 | | - { |
21 | | - if (sound.offset > 0) |
22 | | - StaticCoroutine.Run(PlayCo(sound.SetClip(clip))); |
23 | | - else |
24 | | - { |
25 | | - AudioSource source = EnsureSource(); |
26 | | - sound.SetClip(clip); |
27 | | - source.clip = sound.clip; |
28 | | - source.volume = sound.volume; |
29 | | - source.pitch = sound.pitch; |
30 | | - source.Play(); |
31 | | - } |
| 10 | +namespace Overlayer.Scripting; |
| 11 | + |
| 12 | +public static class AudioPlayer { |
| 13 | + static List<AudioSource> sources = new(); |
| 14 | + static Dictionary<string, AudioClip> clips = new(); |
| 15 | + public static void Play(Sound sound) { |
| 16 | + if(sound?.sound == null) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + if(clips.TryGetValue(sound.sound, out var clip)) { |
| 21 | + if(sound.offset > 0) { |
| 22 | + StaticCoroutine.Run(PlayCo(sound.SetClip(clip))); |
| 23 | + } else { |
| 24 | + AudioSource source = EnsureSource(); |
| 25 | + sound.SetClip(clip); |
| 26 | + source.clip = sound.clip; |
| 27 | + source.volume = sound.volume; |
| 28 | + source.pitch = sound.pitch; |
| 29 | + source.Play(); |
32 | 30 | } |
33 | | - else StaticCoroutine.Run(LoadClip(sound.sound, clip => StaticCoroutine.Run(PlayCo(sound.SetClip(clip))))); |
| 31 | + } else { |
| 32 | + StaticCoroutine.Run(LoadClip(sound.sound, clip => StaticCoroutine.Run(PlayCo(sound.SetClip(clip))))); |
34 | 33 | } |
35 | | - public static void Stop(Sound sound) |
36 | | - { |
37 | | - sources.Find(a => a.clip == sound.clip)?.Stop(); |
| 34 | + } |
| 35 | + public static void Stop(Sound sound) => sources.Find(a => a.clip == sound.clip)?.Stop(); |
| 36 | + public static void StopAll() => sources.ForEach(a => a.Stop()); |
| 37 | + public static void LoadAudio(string path, Action<AudioClip> callback) => StaticCoroutine.Run(LoadClip(path, callback)); |
| 38 | + static IEnumerator PlayCo(Sound sound) { |
| 39 | + float counted = 0f; |
| 40 | + while(counted < sound.offset) { |
| 41 | + counted += UnityEngine.Time.deltaTime * 1000f; |
| 42 | + yield return null; |
38 | 43 | } |
39 | | - public static void StopAll() |
40 | | - { |
41 | | - sources.ForEach(a => a.Stop()); |
| 44 | + AudioSource source = EnsureSource(); |
| 45 | + source.clip = sound.clip; |
| 46 | + source.volume = sound.volume; |
| 47 | + source.pitch = sound.pitch; |
| 48 | + source.Play(); |
| 49 | + } |
| 50 | + static IEnumerator LoadClip(string sound, Action<AudioClip> callback) { |
| 51 | + if(callback == null) { |
| 52 | + yield break; |
42 | 53 | } |
43 | | - public static void LoadAudio(string path, Action<AudioClip> callback) |
44 | | - { |
45 | | - StaticCoroutine.Run(LoadClip(path, callback)); |
| 54 | + |
| 55 | + if(clips.TryGetValue(sound, out var c)) { |
| 56 | + callback(c); |
| 57 | + yield break; |
46 | 58 | } |
47 | | - static IEnumerator PlayCo(Sound sound) |
48 | | - { |
49 | | - float counted = 0f; |
50 | | - while (counted < sound.offset) |
51 | | - { |
52 | | - counted += UnityEngine.Time.deltaTime * 1000f; |
53 | | - yield return null; |
54 | | - } |
55 | | - AudioSource source = EnsureSource(); |
56 | | - source.clip = sound.clip; |
57 | | - source.volume = sound.volume; |
58 | | - source.pitch = sound.pitch; |
59 | | - source.Play(); |
| 59 | + if(!File.Exists(sound)) { |
| 60 | + Main.Logger.Log($"{sound} Is Not Exist!!"); |
| 61 | + yield break; |
60 | 62 | } |
61 | | - static IEnumerator LoadClip(string sound, Action<AudioClip> callback) |
62 | | - { |
63 | | - if (callback == null) yield break; |
64 | | - if (clips.TryGetValue(sound, out var c)) |
65 | | - { |
66 | | - callback(c); |
67 | | - yield break; |
68 | | - } |
69 | | - if (!File.Exists(sound)) |
70 | | - { |
71 | | - Main.Logger.Log($"{sound} Is Not Exist!!"); |
72 | | - yield break; |
73 | | - } |
74 | | - Uri.TryCreate(sound, UriKind.RelativeOrAbsolute, out Uri uri); |
75 | | - if (uri == null) yield break; |
76 | | - var at = Path.GetExtension(sound) switch |
77 | | - { |
78 | | - ".ogg" => AudioType.OGGVORBIS, |
79 | | - ".mp3" => AudioType.MPEG, |
80 | | - ".aiff" => AudioType.AIFF, |
81 | | - ".wav" => AudioType.WAV, |
82 | | - _ => AudioType.UNKNOWN |
83 | | - }; |
84 | | - if (at == AudioType.UNKNOWN) yield break; |
85 | | - var clipReq = UnityWebRequestMultimedia.GetAudioClip(uri, at); |
86 | | - yield return clipReq.SendWebRequest(); |
87 | | - var clip = DownloadHandlerAudioClip.GetContent(clipReq); |
88 | | - UnityEngine.Object.DontDestroyOnLoad(clip); |
89 | | - callback(clips[sound] = clip); |
| 63 | + Uri.TryCreate(sound, UriKind.RelativeOrAbsolute, out Uri uri); |
| 64 | + if(uri == null) { |
| 65 | + yield break; |
| 66 | + } |
| 67 | + |
| 68 | + var at = Path.GetExtension(sound) switch { |
| 69 | + ".ogg" => AudioType.OGGVORBIS, |
| 70 | + ".mp3" => AudioType.MPEG, |
| 71 | + ".aiff" => AudioType.AIFF, |
| 72 | + ".wav" => AudioType.WAV, |
| 73 | + _ => AudioType.UNKNOWN |
| 74 | + }; |
| 75 | + if(at == AudioType.UNKNOWN) { |
| 76 | + yield break; |
90 | 77 | } |
91 | | - static AudioSource EnsureSource() |
92 | | - { |
93 | | - var source = sources.FirstOrDefault(a => !a.isPlaying); |
94 | | - if (source != null) return source; |
95 | | - GameObject sourceObject = new GameObject(); |
96 | | - source = sourceObject.AddComponent<AudioSource>(); |
97 | | - source.playOnAwake = false; |
98 | | - source.ignoreListenerPause = true; |
99 | | - source.ignoreListenerVolume = true; |
100 | | - UnityEngine.Object.DontDestroyOnLoad(sourceObject); |
101 | | - sources.Add(source); |
| 78 | + |
| 79 | + var clipReq = UnityWebRequestMultimedia.GetAudioClip(uri, at); |
| 80 | + yield return clipReq.SendWebRequest(); |
| 81 | + var clip = DownloadHandlerAudioClip.GetContent(clipReq); |
| 82 | + UnityEngine.Object.DontDestroyOnLoad(clip); |
| 83 | + callback(clips[sound] = clip); |
| 84 | + } |
| 85 | + static AudioSource EnsureSource() { |
| 86 | + var source = sources.FirstOrDefault(a => !a.isPlaying); |
| 87 | + if(source != null) { |
102 | 88 | return source; |
103 | 89 | } |
| 90 | + |
| 91 | + GameObject sourceObject = new(); |
| 92 | + source = sourceObject.AddComponent<AudioSource>(); |
| 93 | + source.playOnAwake = false; |
| 94 | + source.ignoreListenerPause = true; |
| 95 | + source.ignoreListenerVolume = true; |
| 96 | + UnityEngine.Object.DontDestroyOnLoad(sourceObject); |
| 97 | + sources.Add(source); |
| 98 | + return source; |
104 | 99 | } |
105 | 100 | } |
0 commit comments