This is a custom editor window for manually generating timing information for triggers related to a piece of music.
I developed this for use in my project Maestro, where the player must make conducting gestures at specific points in time throughout a piece of music.
This could be used for any such Guitar-Hero-like mechanic.
The motivation was the need to efficiently produce this timing information and the desire to do it within Unity.
Like the Gesture Recognition tool I have published on this GitHub account, I made this as easy as possible to start using. All you need to do is:
- Put the TimeEditor.cs file into your Assets/Editor folder
- Put the EditorPlaybackMarker.cs, EditorTimeMarker.cs, TimingDocument.cs files into your Assets/Scripts folder
Open the Time Editor window in Unity. Next, perform the following steps in this order:
- Spawn Music Player
- Check Load External Music File
- Select Music
- Add to Player
This will spawn a GameObject to hold the audioclip of the music being 'marked up' so that it can be played and heard. Then, the mp3 file outside of Unity is copied into your Assets/Resources folder. Finally, this asset is turned into an AudioClip and attached to the GameObject.
You will see the visualization of the mp3 file displayed at the top, the blue playback marker will be at the left.
These operate like you would expect, press play to play the music wherever the playback marker is. Press pause to pause in place. Press stop to reset.
The playback marker can be scrubbed back and forth on the audio file, so you can visually select where to play the music or rewind wherever you need to. You don't need to pause to use the scrubbing feature, just move it around. It is partially clear so that you can see any timing marks underneath it.
The focus is on ease of navigation so that you can more efficiently create the timing marks.
This button will create a marker at the current position of the blue playback marker. This is so you can click as you listen to the music and mark quickly by ear. Remember you can move around the playback marker!
This button will delete any markers you have selected. Selection is easy, just right click on each marker, it will turn red so you can easily distinguish selected ones. Right clicking actually toggles selection, so you can deselect easily as well. You can have as many markers selected at a time as you want.
This button will prompt you to choose a file location at which a text file with the JSON representation of the timing information is created.
The data is stored in the TimingDocument object, which has a list of the EditorTimeMarker objects, which correspond to each mark you made in the editor. To use this in another script, generate the TimingDocument object from the text file using JsonUtility, then access the timeMarks field, which is the list of time markers. The time markers contain the time they represent along with any other information you want to add to that class.
This button will prompt you to select a text file which has ben generated by this editor tool. The information will be loaded into the window, and you will see all of the timing marks that were saved in that file. Now you can edit these marks, and then save as a new file or overwrite the old one.
This toggle will allow you to move the time marks around freely just like the playback marker. Just click and drag. This allows for efficient editing and fine tuning of your work in a visual way.
This is a toggle because moving the playback marker across any of your timing marks will drag them along too (the movement functionality is implemented by checking whether your mouse is in the box of the marker). Moving any marks across each other will do the same thing, though there would be no reason to do this unless you had different kinds of markers, meaning that interchanging places would actually be meaningful.
The cleanup button will delete the mp3 file in your /Resources folder, the game object used to play this audio, and the current set of marks in the editor.
Make sure to save before using the cleanup button, or your work will be lost!
This allows you to simply open up the editor window, do what you need, and then be done, not having to worry about cleaning up any of the peripheral things generated by this tool. It also is a way to 'clean the slate' so you can load a new audio file and create/edit a different set of marks.
Possible improvements to this tool include:
- Allowing for movement of the playback marker and timing markers over each other.
- Using a nicer mp3 visualization than the one generated by Unity.
- Adding a group selection feature for the marks (currently you toggle selection one by one).
- Add more documentation for how to make use of this in a game.
- Add performance documentation.