Unity3d sound play and invoke phone shake

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.