http://blog.csdn.net/sunshine_1984/article/details/12943979
Today, we studied the content of the next Unity3d audio playback, and sorted out the implementation details.
1, add sound files to the assets/resources directory, I added here to the Assets/resources/audio directory.
2, create a new C # script named audio
3, write the Audio.cs code as follows (the comment is very clear):
Using Unityengine;
Using System.Collections;
public class Audio:monobehaviour {
Music files
public Audiosource Music;
Volume
public float Musicvolume;
Use this for initialization
void Start () {
This.gameObject.AddComponent ("Audiosource");
Music = This.gameObject.AddComponent ("Audiosource") as Audiosource;
Music.clip = Resources.load ("Audio/test") as AudioClip;
Music.playonawake = false;
Music.loop = false;
Set Default volume
Musicvolume = 0.2F;
}
Update is called once per frame
void Update () {
}
void Ongui ()
{
Play Music button
if (GUI. button (new Rect (Ten, ten, +), "Play Music"))
{
No play in
if (!music.isplaying)
{
Play Music
Music. Play ();
}
}
Turn off the music button
if (GUI. button (new Rect (ten, +, +), "Stop Music"))
{
if (music.isplaying)
{
Turn off music
Music. Stop ();
}
}
Pause Music
if (GUI. button (new Rect (ten, +, +), "Pause Music"))
{
if (music.isplaying)
{
Pause Music
Here, after the music is suspended,
Click Play Music to continue playing
And stop later in the Click Play Music
is the new playback
That's the difference between pausing and stopping.
Music. Pause ();
}
}
Create a horizontal slide bar to dynamically modify music volume
First parameter slider range
Second parameter initial slider position
Third parameter start
Fourth end of parameter
Musicvolume = GUI. Horizontalslider (New Rect (), Musicvolume, 0.0F, 1.0F);
Print a percentage of the volume
Gui. Label (new Rect), "Music Volueme is" + (int) (Musicvolume * 100) + "%");
if (music.isplaying)
{
Set Music volume value range from 0.0F to 1.0 in music playback
Music.volume = Musicvolume;
}
}
}
4, associating Audio.cs to main Camera
Call the phone Strapon API as follows: Handheld.vibrate (); The vibration effect of the phone.