1+ using System . Reflection ;
2+ using UnityEditor ;
3+ using UnityEngine ;
4+
5+ namespace CompiledSound . Editor {
6+ [ InitializeOnLoad ]
7+ internal static class CompiledSoundSource {
8+ private const string START_COMPILATION_AUDIO_FILE_NAME = "StartCompilationSound" ;
9+ private const string END_COMPILATION_AUDIO_FILE_NAME = "EndCompilationSound" ;
10+ private const string COMPILE_STATE_PREFS_KEY = "CompileIndicator.WasCompiling" ;
11+ private const string UNITY_EDITOR_AUDIO_UTIL_ASSEMBLY_NAME = "UnityEditor.AudioUtil" ;
12+ private const string PLAY_PREVIEW_CLIP_METHOD_NAME = "PlayPreviewClip" ;
13+
14+ private const string WELCOME_INIT_MESSAGE = "Compilation sounds are not set. "
15+ + "Put '<color=#FF7F00>" + START_COMPILATION_AUDIO_FILE_NAME
16+ + "</color>' or/and '<color=#FF7F00>" + END_COMPILATION_AUDIO_FILE_NAME
17+ + "</color>' audio clips under '<color=#FF7F00>Resources</color>' folder." ;
18+
19+ private const string ERROR_FAILED_PLAY_PREVIEW = "Failed to play sound on compilation. "
20+ + "Can't find <color=#FF7F00>" + PLAY_PREVIEW_CLIP_METHOD_NAME
21+ + "</color> method in <color=#FF7F00>" + UNITY_EDITOR_AUDIO_UTIL_ASSEMBLY_NAME
22+ + "</color> assembly." ;
23+
24+ private static readonly AudioClip startCompilationClip ;
25+ private static readonly AudioClip endCompilationClip ;
26+
27+ static CompiledSoundSource ( ) {
28+ EditorApplication . update += OnUpdate ;
29+ startCompilationClip = Resources . Load < AudioClip > ( START_COMPILATION_AUDIO_FILE_NAME ) ;
30+ endCompilationClip = Resources . Load < AudioClip > ( END_COMPILATION_AUDIO_FILE_NAME ) ;
31+ }
32+
33+ private static void OnUpdate ( ) {
34+ var wasCompiling = EditorPrefs . GetBool ( COMPILE_STATE_PREFS_KEY ) ;
35+ var isCompiling = EditorApplication . isCompiling ;
36+
37+ if ( wasCompiling == isCompiling ) {
38+ return ;
39+ }
40+
41+ if ( isCompiling ) {
42+ OnStartCompiling ( ) ;
43+ } else {
44+ OnEndCompiling ( ) ;
45+ }
46+
47+ EditorPrefs . SetBool ( COMPILE_STATE_PREFS_KEY , isCompiling ) ;
48+ }
49+
50+ private static void OnStartCompiling ( ) {
51+ PlayStartCompilationSound ( ) ;
52+ }
53+
54+ private static void OnEndCompiling ( ) {
55+ PlayEndCompilationSound ( ) ;
56+ PlayWelcomeInitLogMessage ( ) ;
57+ }
58+
59+ private static void PlayStartCompilationSound ( ) {
60+ if ( startCompilationClip == null ) {
61+ return ;
62+ }
63+
64+ PlayPreviewClip ( startCompilationClip ) ;
65+ }
66+
67+ private static void PlayEndCompilationSound ( ) {
68+ if ( endCompilationClip == null ) {
69+ return ;
70+ }
71+
72+ PlayPreviewClip ( endCompilationClip ) ;
73+ }
74+
75+ private static void PlayWelcomeInitLogMessage ( ) {
76+ if ( startCompilationClip != null || endCompilationClip != null ) {
77+ return ;
78+ }
79+
80+ Debug . Log ( WELCOME_INIT_MESSAGE ) ;
81+ }
82+
83+ private static void PlayPreviewClip ( AudioClip clip ) {
84+ const int START_SAMPLE = 0 ;
85+ const bool IS_LOOP = false ;
86+
87+ var unityEditorAssembly = typeof ( AudioImporter ) . Assembly ;
88+ var audioUtilClass = unityEditorAssembly . GetType ( UNITY_EDITOR_AUDIO_UTIL_ASSEMBLY_NAME ) ;
89+ var method = audioUtilClass . GetMethod ( PLAY_PREVIEW_CLIP_METHOD_NAME ,
90+ BindingFlags . Static | BindingFlags . Public ,
91+ null ,
92+ new [ ] { typeof ( AudioClip ) , typeof ( int ) , typeof ( bool ) } ,
93+ null ) ;
94+
95+ if ( method == null ) {
96+ Debug . LogError ( ERROR_FAILED_PLAY_PREVIEW ) ;
97+ return ;
98+ }
99+
100+ method . Invoke ( null ,
101+ new object [ ] { clip , START_SAMPLE , IS_LOOP } ) ;
102+ }
103+ }
104+ }
0 commit comments