Skip to content

Commit 12028cb

Browse files
authored
Add files via upload
1 parent 27091c7 commit 12028cb

13 files changed

+222
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changelog
2+
3+
## [1.0.0] - 2024-07-06
4+
5+
### Added
6+
7+
- CompiledSoundSource.
8+
- High-quality sample sound.

CHANGELOG.md.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/CompiledSound.Editor.asmdef

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "CompiledSound.Editor",
3+
"rootNamespace": "",
4+
"references": [],
5+
"includePlatforms": [
6+
"Editor"
7+
],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

Editor/CompiledSound.Editor.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/CompiledSoundSource.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

Editor/CompiledSoundSource.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Samples.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Samples/Resources.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
54.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)