There is not much secret about using audio in Unity. The tool is very nice and makes it enormously easy. The whole point is that you must necessarily have an audio source (that is, an object that contains the component Audio Source) and a "listener" (an object containing the component Audio Listener).
When you create a new project, the camera (Main Camera) already contains by default a Audio Listener (because it makes sense for the player to be able to hear the sounds of the game while on the main camera). As the music of the game is something that accompanies the player constantly, you can also add a Audio Source on the camera itself (select "Main Camera" in the window Hierarchy and click "Add Component" in the *Inspector" window, choosing "Audio -> Audio Source"), just to play the songs. Then import the song as a Asset new ("Assets -> Import New Asset..." menu) and select it from the component Audio Source in the field Audio Clip (in the example, I am using music "Volatile Reaction" by Kevin Macleod (Incompetech)):
Important: You can leave marked (come by default) option Play on
Awake that causes the music to be started together with the object (in
example case, the camera that is started along with the game). I
I unchecked only to illustrate.
Once having an attached object with a component Audio Source and a Audio Clip, just access the attribute audio
of the object to execute the methods Play()
, Stop()
or Pause
. The method PlayOneShot()
that you used requires you to inform the audio clip (and is another possible approach), but it is usually much easier and practical to leave this definition through the clip attributed to the audio source (if you want to change the music, you better change the audio clip in the object, and so the music start and pause menu need not get more complex than I exemplify below.
To illustrate the code I created two buttons, Button Play
and Button Pause
(are actually GUI Textures created from the Gameobject menu -> Create Other -> Gui Texture. The texture images are from the package Soft Scraps Icons on Iconarchive, created by Jojo Mendoza) each with an attached script component (PlayScript
and PauseScript
):
These are the codes (in C#):
Playscript
using UnityEngine;
using System.Collections;
public class PlayScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseUp() {
Camera.main.audio.Play ();
}
}
Pausescript
using UnityEngine;
using System.Collections;
public class PauseScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseUp() {
Camera.main.audio.Pause ();
}
}
Note that in both scripts the audio source is the main camera (accessible from Camera.main
) for illustration only, but could be the very object in which the script is attached (the this
). They also execute (the play) or interrupt (the pause) the audio regardless of which is the audio clip used. And that’s a good approach, because you can change the music in another location (by updating the property Camera.main.audio.clip
) without having to modify the code of these buttons.
The sample project can be downloaded from 4shared.