-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEditorAudioUtility.cs
More file actions
218 lines (196 loc) · 6.83 KB
/
EditorAudioUtility.cs
File metadata and controls
218 lines (196 loc) · 6.83 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
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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EditorAudioUtility.cs">
// Copyright (c) 2021 Johannes Deml. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
// public@deml.io
// </author>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Reflection;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;
namespace JD.EditorAudioUtils
{
/// <summary>
/// Interface to play AudioClips and Notification sounds in the editor
/// See also https://forum.unity.com/threads/way-to-play-audio-in-editor-using-an-editor-script.132042/
/// </summary>
public static class EditorAudioUtility
{
public static AudioClip LastPlayedPreviewClip { get; private set; }
private static bool _initialized;
private static AudioUtilMethodWrapper _playPreviewClip;
private static AudioUtilMethodWrapper _pausePreviewClip;
private static AudioUtilMethodWrapper _resumePreviewClip;
private static AudioUtilMethodWrapper _stopPreviewClip;
private static AudioUtilMethodWrapper _stopAllPreviewClips;
private static AudioUtilMethodWrapper _isPreviewClipPlaying;
/// <summary>
/// Play a predefined notification sound, if the user enabled notification sounds
/// </summary>
/// <param name="type">Type of the sound to play</param>
public static void PlayNotificationSound(EditorNotificationSound type)
{
if (EditorNotificationSettings.NotificationSoundsEnabled)
{
AudioClip clip = EditorNotificationSettings.Instance.GetAudioClip(type);
// Don't play a clip, if it is not set
if (clip != null)
{
PlayPreviewClip(clip);
}
}
}
/// <summary>
/// Play a predefined notification sound after a predefined time
/// </summary>
/// <param name="type">Type of the sound to play</param>
/// <param name="secondsDelay">Seconds until the sound should be played</param>
public static void PlayNotificationSoundDelayed(EditorNotificationSound type, float secondsDelay)
{
IEnumerator DelayedCoroutine()
{
yield return new EditorWaitForSeconds(secondsDelay);
PlayNotificationSound(type);
}
object obj = new object();
EditorCoroutineUtility.StartCoroutine(DelayedCoroutine(), obj);
}
/// <summary>
/// Play an audio clip in the editor once
/// </summary>
/// <param name="audioClip">Audio clip to play</param>
public static void PlayPreviewClip(AudioClip audioClip)
{
PlayPreviewClip(audioClip, 0, false);
}
/// <summary>
/// Play an audio clip in the editor
/// </summary>
/// <param name="audioClip">Clip to play</param>
/// <param name="startSample">The sample to start playing the clip</param>
/// <param name="loop"></param>
public static void PlayPreviewClip(AudioClip audioClip, int startSample, bool loop)
{
if (!_initialized)
{
Initialize();
}
LastPlayedPreviewClip = audioClip;
_playPreviewClip.Invoke(audioClip, startSample, loop);
}
/// <summary>
/// Pause the currently playing preview clips (Unity 2020+) or the defined clip (Unity 2019)
/// </summary>
/// <param name="audioClip">Clip to pause (Unity 2019) - Ignored in Unity 2020+</param>
public static void PausePreviewClip(AudioClip audioClip)
{
if (!_initialized)
{
Initialize();
}
#if UNITY_2020_1_OR_NEWER
_pausePreviewClip.Invoke();
#else
_pausePreviewClip.Invoke(audioClip);
#endif
}
/// <summary>
/// Resume the paused preview clips (Unity 2020+) or the defined clip (Unity 2019)
/// </summary>
/// <param name="audioClip">Clip to resume (Unity 2019) - Ignored in Unity 2020+</param>
public static void ResumePreviewClip(AudioClip audioClip)
{
if (!_initialized)
{
Initialize();
}
#if UNITY_2020_1_OR_NEWER
_resumePreviewClip.Invoke();
#else
_resumePreviewClip.Invoke(audioClip);
#endif
}
/// <summary>
/// Stop all preview clips (Unity 2020+) or the defined clip (Unity 2019)
/// </summary>
/// <param name="audioClip">Clip to stop (Unity 2019) - Ignored in Unity 2020+</param>
public static void StopPreviewClip(AudioClip audioClip)
{
if (!_initialized)
{
Initialize();
}
#if UNITY_2020_1_OR_NEWER
_stopPreviewClip.Invoke();
#else
_stopPreviewClip.Invoke(audioClip);
#endif
}
/// <summary>
/// Stop all playing preview clips
/// </summary>
public static void StopAllPreviewClips()
{
if (!_initialized)
{
Initialize();
}
_stopAllPreviewClips.Invoke();
}
/// <summary>
/// Is any preview clip playing (Unity 2020+) or the defined clip playing (Unity 2019)
/// </summary>
/// <param name="audioClip">Clip to check if it is playing (Unity 2019) - Ignored in Unity 2020+</param>
/// <returns>True if audioClip is playing (Unity 2019) or any clip is playing (Unity 2020+=</returns>
public static bool IsPreviewClipPlaying(AudioClip audioClip)
{
if (!_initialized)
{
Initialize();
}
#if UNITY_2020_1_OR_NEWER
return (bool)_isPreviewClipPlaying.Invoke();
#else
return (bool)_isPreviewClipPlaying.Invoke(audioClip);
#endif
}
private static void Initialize()
{
Assembly unityEditorAssembly = typeof(AudioImporter).Assembly;
Type audioUtilClass = unityEditorAssembly.GetType("UnityEditor.AudioUtil");
#if UNITY_2020_1_OR_NEWER
_playPreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "PlayPreviewClip",
new[] {typeof(AudioClip), typeof(int), typeof(bool)} );
_pausePreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "PausePreviewClip",
Array.Empty<Type>());
_resumePreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "ResumePreviewClip",
Array.Empty<Type>());
_stopPreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "StopAllPreviewClips",
Array.Empty<Type>());
_stopAllPreviewClips = new AudioUtilMethodWrapper(audioUtilClass, "StopAllPreviewClips",
Array.Empty<Type>());
_isPreviewClipPlaying = new AudioUtilMethodWrapper(audioUtilClass, "IsPreviewClipPlaying",
Array.Empty<Type>());
#else
_playPreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "PlayClip",
new[] {typeof(AudioClip), typeof(int), typeof(bool)} );
_pausePreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "PauseClip",
new[] {typeof(AudioClip)});
_resumePreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "ResumeClip",
new[] {typeof(AudioClip)});
_isPreviewClipPlaying = new AudioUtilMethodWrapper(audioUtilClass, "IsClipPlaying",
new[] {typeof(AudioClip)});
_stopPreviewClip = new AudioUtilMethodWrapper(audioUtilClass, "StopClip",
new[] {typeof(AudioClip)});
_stopAllPreviewClips = new AudioUtilMethodWrapper(audioUtilClass, "StopAllClips",
Array.Empty<Type>());
#endif
_initialized = true;
}
}
}