-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeEditor.cs
352 lines (297 loc) · 12.8 KB
/
TimeEditor.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;
//******************************************* Time Editor Unity Window **********************************************//
//
// Author: Andres De La Fuente Duran
//
// Use: This script can simply be included in your Editor file. It adds a new editor window
// accessible from the Windows drop down list.
//
// The point of this is to allow for the generation of timing information with respect
// to some corresponding audio file, like a piece of music. The window is intended to make it
// easy for you to mark specific times in the audio as you listen to it.
// Editing functions are built in to make this process more productive as well.
//****************************************** Class: Time Editor ******************************************************//
//
// Use: The editor window extension for the above functionality.
//
// Fields:
// Audio Playback ..............................................................................
//
// musicFile: The filepath for the audio to be loaded
//
// musicFileInProject: The filename with extension for use with FileUtil
//
// musicFileNoExtension: The filename for use with Resources.load()
//
// musicPlayer: The game object to playback the audio selected
//
// Playback Visuals ...........................................................................
//
// musicPreview: The image preview of the selected audio generated by Unity
//
// previewImageLength: Width of the music preview
//
// playbackMarker: The object that represents current playback time
//
// playbackMarkerColor: The color for the playback marker in the GUI
//
// oldColor: Color to return to after rendering GUI elements with unique colors
//
// Timing Marks ..............................................................................
//
// marks: List of current timing marks in this Time Editor window
//
// selectedMarks: List of selected timing marks
//
// loadedDoc: The timing marks loaded from JSON
//
// timingDoc: The timing marks of the current Time Editor window to save
//
// Control Flow ..............................................................................
//
// musicLoaded: Whether there is an audio file loaded
//
// moveMarks: Whether the movement of timing marks is enabled
//
// externalMusic: Whether the music to be loaded is not in the project
//
// Other .....................................................................................
//
// indexPosition: The current position of the playback marker
//
// yPosition: The y coordinate at which to render the audio visualization and marks
//
// musicLength: Duration in seconds of the loaded audio
//
// buttonHeight: The height of all the buttons in the window
//
// Methods:
// ShowWindow(): Show this window in the Windows menu
//
// Update(): Keep track of current playback timing information
//
// OnGUI(): Render the contents of the Time Editor
//
// HandleEvents(Event): Have each object in the Time Editor handle user events
public class TimeEditor : EditorWindow
{
string musicFile = "";
string musicFileInProject = "/TE_music.mp3";
string musicFileNoExtension = "TE_music";
GameObject musicPlayer;
Texture musicPreview;
float previewImageLength = 1000;
EditorPlaybackMarker playbackMarker;
Color playbackMarkerColor = new Color(0, 0, 100, 50f);
Color oldColor;
bool musicLoaded = false;
bool moveMarks = false;
bool externalMusic = false;
float indexPosition = 0;
float yPosition = 20;
float musicLength = 0;
float buttonHeight = 30;
List<EditorTimeMarker> marks = new List<EditorTimeMarker>();
List<EditorTimeMarker> selectedMarks = new List<EditorTimeMarker>();
TimingDocument loadedDoc;
TimingDocument timingDoc;
// Add menu item named "My Window" to the Window menu
[MenuItem("Window/Time Editor")]
public static void ShowWindow()
{
// Show existing window instance. If one doesn't exist, make one.
EditorWindow.GetWindow(typeof(TimeEditor));
AssetDatabase.Refresh();
}
private void Update()
{
if (musicLoaded && playbackMarker != null)
{
// Update timing information according to playback
float musicTime = musicPlayer.GetComponent<AudioSource>().time;
indexPosition = (musicTime / musicLength) * previewImageLength;
playbackMarker.box.x = indexPosition;
playbackMarker.time = musicTime;
Repaint();
}
}
void OnGUI()
{
playbackMarkerColor = new Color(0, 1, 1, 0.5f);
oldColor = GUI.color;
//**************** MUSIC VISUALIZATION ****************************
GUILayout.BeginVertical(GUILayout.Height(150));
GUILayout.Label("");
if (musicLoaded)
{
// Audio Preview
GUI.DrawTexture(new Rect(0, yPosition, previewImageLength, 100), musicPreview);
// Time Markers
if (marks.Count > 0)
{
foreach (EditorTimeMarker m in marks)
{
if (m.selected)
{
GUI.color = Color.red;
GUI.Box(m.box, "");
GUI.color = oldColor;
}
else
{
GUI.Box(m.box, "");
}
}
}
if (playbackMarker == null)
{
playbackMarker = new EditorPlaybackMarker(indexPosition, yPosition, 0);
}
else
{
GUI.color = playbackMarkerColor;
GUI.Box(playbackMarker.box, "");
GUI.color = oldColor;
}
}
GUILayout.EndVertical();
//***************** PLAYBACK CONTROLS *****************************
GUILayout.Label("Audio Controls", EditorStyles.largeLabel);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Play", GUILayout.Height(buttonHeight)))
{
musicPlayer.GetComponent<AudioSource>().Play();
}
if (GUILayout.Button("Pause", GUILayout.Height(buttonHeight)))
{
musicPlayer.GetComponent<AudioSource>().Pause();
}
if (GUILayout.Button("Stop", GUILayout.Height(buttonHeight)))
{
musicPlayer.GetComponent<AudioSource>().Pause();
musicPlayer.GetComponent<AudioSource>().time = 0;
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(20);
//****************** MARKERS CONTROLS *****************************
GUILayout.Label("Marker Controls", EditorStyles.largeLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.BeginVertical();
if (GUILayout.Button("Add Marker", GUILayout.Height(buttonHeight)))
{
marks.Add(new EditorTimeMarker(indexPosition, yPosition, musicPlayer.GetComponent<AudioSource>().time));
}
if (GUILayout.Button("Delete Selection", GUILayout.Height(buttonHeight)))
{
foreach (EditorTimeMarker m in selectedMarks)
{
if (m.selected)
{
marks.Remove(m);
}
}
selectedMarks.Clear();
}
EditorGUILayout.EndVertical();
EditorGUILayout.BeginVertical();
if (GUILayout.Button("Save", GUILayout.Height(buttonHeight)))
{
timingDoc = new TimingDocument(marks, musicLength);
Debug.Log("timing doc array length: " + timingDoc.timeMarks.Count);
string timingDocJson = JsonUtility.ToJson(timingDoc);
Debug.Log(timingDocJson);
string path = EditorUtility.SaveFilePanel(
"Save timing information as txt",
Application.dataPath + "/Resources",
musicFileNoExtension + "Timing.txt",
"txt");
File.WriteAllText(path, timingDocJson);
AssetDatabase.Refresh();
}
if (GUILayout.Button("Load Time Marks", GUILayout.Height(buttonHeight)))
{
string path = EditorUtility.OpenFilePanel(
"Load timing marks file",
Application.dataPath,
"txt");
string json = File.ReadAllText(path);
loadedDoc = JsonUtility.FromJson<TimingDocument>(json);
marks = loadedDoc.timeMarks;
}
EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
GUILayout.Space(10);
moveMarks = GUILayout.Toggle(moveMarks, "Move Marks");
GUILayout.Space(20);
//*************** AUDIO SOURCE SETUP ******************************
GUILayout.Label("Setup", EditorStyles.largeLabel);
if (GUILayout.Button("Spawn Music Player", GUILayout.Height(buttonHeight)))
{
musicPlayer = new GameObject("Time Editor Music Player");
}
EditorGUILayout.BeginHorizontal();
externalMusic = GUILayout.Toggle(externalMusic, "Load External Music File");
if (GUILayout.Button("Select Music", GUILayout.Height(buttonHeight)))
{
if (externalMusic)
{
musicFile = EditorUtility.OpenFilePanel("Load mp3 file", "", "mp3");
if (musicFile != "")
{
FileUtil.CopyFileOrDirectory(musicFile, Application.dataPath + "/Resources" + musicFileInProject);
AssetDatabase.Refresh();
}
} else
{
musicFile = EditorUtility.OpenFilePanel("Choose mp3 file", Application.dataPath + "/Resources", "mp3");
}
}
if(GUILayout.Button("Add to Player", GUILayout.Height(buttonHeight)))
{
if (musicPlayer.GetComponent<AudioSource>() == null)
{
musicPlayer.AddComponent<AudioSource>();
}
musicPlayer.GetComponent<AudioSource>().clip = Resources.Load(musicFileNoExtension) as AudioClip;
musicLength = musicPlayer.GetComponent<AudioSource>().clip.length;
musicPreview = AssetPreview.GetAssetPreview(musicPlayer.GetComponent<AudioSource>().clip);
musicLoaded = true;
}
EditorGUILayout.EndHorizontal();
GUILayout.Label(musicFile, EditorStyles.boldLabel);
GUILayout.Space(20);
//************************* CLEANUP *****************************
if (GUILayout.Button("Clean Up", GUILayout.Height(buttonHeight)))
{
musicLoaded = false;
FileUtil.DeleteFileOrDirectory(Application.dataPath + "/Resources/" + musicFileInProject);
GameObject.DestroyImmediate(musicPlayer);
marks.Clear();
AssetDatabase.Refresh();
}
if (musicLoaded && playbackMarker != null)
{
HandleEvents(Event.current);
}
}
private void HandleEvents(Event e)
{
foreach (EditorTimeMarker m in marks)
{
m.HandleEvents(e, previewImageLength, musicLength, moveMarks, selectedMarks);
}
playbackMarker.HandleEvents(e, previewImageLength);
if (e.type == EventType.mouseDrag)
{
if (playbackMarker.moved)
{
musicPlayer.GetComponent<AudioSource>().time = (playbackMarker.box.x / previewImageLength) * musicLength;
playbackMarker.moved = false;
}
}
}
}