Unity3d game engine details game development audio playback (15th)

Source: Internet
Author: User
Tags ranges

Unity3d game engine details game development audio playback




If the original Yusong Momo article is reprinted, please note: Reprinted to my independent domain name blog Yusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/550






Playing Game audio plays a very important role in any game. There are two types of audio playing: game music and game sound. The former applies to long music, such as game background music. The second type is trial and short game music, such as the game sound effects that can be played in an instant when a video is shot or beaten. Today, Momo will use the following example to show you how to play unity3d game music and sound effects.

 


The unity3d game engine supports a total of four files in music format.


. AIFF applies to shorter music files and can be used as combat sound effects for games.

. Wav is applicable to shorter music files and can be used as combat sound effects for games.

. MP3 is suitable for long music files that can be used as game background music.

. Ogg is suitable for long music files that can be used as game background music.


Create an empty game object in the scenario.

After gameobject-> createempty is created successfully, I name it audio.

It is very important to add an audiosource attribute to audio, which is mainly used for unity to play music.


Component-> audio source.




Find a music file and I use a .mp3music file. I named it 0.mp3 and dragged it to the audio clip on the right, as shown in.







We found that audio source has many set attributes, so Momo will list some important attributes.


Audioclip: Sound fragment. You can also dynamically extract music files in the code.

Mute: whether to mute

Bypass effects: whether to enable audio effects

Play on awake: automatic playback upon startup

Loop: loop playback

Volume: Specifies the sound size. The value ranges from 0.0 to 1.0.

Pitch: Specifies the playback speed. The value ranges from-3 to 3. If the value is less than 1, the playback speed is accelerated when the playback speed is lower than 1.





Create a script named audio. CS for playing music. In this chapter, you can click three buttons to play, stop, and pause music. With a horizontal drag bar, you can dynamically modify the music sound by dragging your fingers.


Audio. CS

Using unityengine; using system. collections; public class audio: monobehaviour {// music file public audiosource music; // volume public float musicvolume; void start () {// set the default volume musicvolume = 0.5f ;} void ongui () {// play music button if (GUI. button (New rect (10, 10,100, 50), "Play Music") {// If (! Music. isplaying) {// play music. play () ;}// close the music button if (GUI. button (New rect (10, 60,100, 50), "Stop music") {If (music. isplaying) {// close music. stop () ;}// pause the music if (GUI. button (New rect (10,110,100, 50), "pause music") {If (music. isplaying) {// pause the music. // After the music is paused, // After the music is paused, click play to continue playing. // After the music is stopped, click play to play the music again. // This is the difference between pause and stop music. pause ();}} // create a horizontal slide for dynamically changing the Music volume // The first parameter slide range // The second parameter's initial slider position // The third parameter's start point // The fourth parameter's End Point musicvolume = gui. horizontalslider (New rect (160, 10,100, 50), musicvolume, 0.0f, 1.0f); // print the volume percentage out of the GUI. label (New rect (160, 50,300, 20), "Music volueme is" + (INT) (musicvolume * 100) + "%"); If (music. isplaying) {// set the Music volume range to 0.0f to 1.0 music during music playback. volume = musicvolume ;}}}


Bind audio. CS to the camera and drag the audio game object to assign the audiosource object to music. Here we emphasize audiolistenr, which is an audio listener used to listen to the playback of music files. This is an important attribute and must be checked. You can only select this option to play music.






Build and run our game demo. All functions are perfectly implemented ~





Finally, you are welcome to discuss unity3d game development with Momo and learn and make progress together ~~~ Attached to the unity3d project, I will not upload the xcode project and export it by myself. I feel a little unhappy today! 5555555555. So good night ~

:Http://www.xuanyusong.com/archives/550


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.