Game Audio Development with Unity 5.X
上QQ阅读APP看书,第一时间看更新

Importing and playing notes

Of course, in order to use our new Keyboard script, we are going to need some notes recorded for at least one instrument. Let's get back into Unity and import some tunes by following the following exercise:

  1. From the menu, select Assets | Import Package | Custom Package.... A file explorer dialog will open. Use the dialog to find the book's downloaded source code for Chapter_2_Assets and a file inside called Chapter_2_Keyboard_Tunes.unitypackage. Select the file and click on the Open button to begin importing the asset.
  2. A progress dialog will quickly flash showing the asset being decompressed and then the Import Unity Package dialog will be shown as follows:
Import Unity Package importing keyboard tunes
  1. Click on the Import button on the dialog to import all the tunes into the project. Again, you will see another progress dialog quickly flash and the assets will be imported.
  2. Select the VirtualKeyboard object in the Hierarchy window. In the Inspector window, expand the Audio Source component we added earlier. Set the component's AudioClip source to the ukulele clip by clicking on the target icon beside the setting and then selecting the item from Select AudioClip dialog, as shown in the screenshot here:
Setting the AudioClip source
  1. Close the dialog after you make the selection. Keep all the defaults for the Audio Source component. No need to set 3D sound or other fancy audio settings.
  2.  Click on the play button to start the scene running. After the scene starts running, type some notes by typing any of the following keys a,s,d,f,g,h,j,k, or l.
  3. Depending on your musical skills, you may be able to play notes or something that sounds like music. You can change the type of instrument anytime while the scene is running. Just be sure if you change an instrument to click back in the Game view, otherwise the keyboard strokes won't register.
  1. The audio sounds provided in the imported asset will generally be recorded from instruments playing a C4 note or middle C. However, not all the audio is recorded at the middle C, some of it may be other notes. If you have a musical ear you can alter the instrument so it starts from middle C by setting the transpose on the Keyboard component. A transpose value of +12 or -12 will increase/decrease the note by a full octave. While a transpose value of +1/-1 will tune the note up or down the scale to D (+1) or B (-1) for instance. The following chart shows how the notes are arranged and written on sheet music, with corresponding matching keyboard keys shown underneath:
Notes matching keyboard keys
  1. When you are done playing the keyboard, click on the play button to stop the scene. Be sure to save the scene by selecting from the menu File | Save scene as.... This will open a Save Scene dialog, keep the default path, name the scene VirtualKeyboard, and click on Save.

As you can see, with just a small amount of code, we were able to control playing an audio source into a virtual instrument. While the keyboard is not perfect, it certainly demonstrates the power and ease of scripting audio. In the next section, we are going to make some improvements to the Keyboard script in order to demonstrate other scripting and audio concepts.