Unity3d Music Sound Learning Notes

Source: Internet
Author: User

For the music sound of Unity3d this piece has not been good to see, now ready to study well, and as a note record.

Supported formats

In the game, there are usually two kinds of music, one is a long time background music, one is short time sound (such as button click, shooting Sound, etc.).

Unity3d supports the following types of music formats:

    • AIFF: For shorter music files can be used as game fighting sounds
    • WAV: For shorter music files can be used as game fighting sounds
    • MP3: For longer music files can be used as a game background music
    • OGG: For longer music files can be used as a game background music
Music components

The music is packaged in Unity3d, in general, 3 basic components are needed to play the music, so let's take a look at these 3 components.

Audiolistener

In general, we create the scene on the main camera with this component, which has only one function, is to listen to all the sound of the current scene play and the audio output, without this component, no sound will be issued. The good news is that we don't need to create multiple components in the general scenario, just add a component on any gameobject, but make sure that this gameobject is not destroyed, so you can always add it to the main camera as unity does.

Audiosource

Controls a component that specifies music playback, and you can control some of the effects of music by using property settings to view the official documentation: Http://docs.unity3d.com/Manual/class-AudioSource.html.

Some common properties are listed below:

    • AudioClip: Audio clips can also be used to dynamically capture music files in code.
    • Mute: Mute.
    • Bypass effects: Whether to turn on audio effects.
    • Play on awake: auto play on boot.
    • Loop: Loops the playback.
    • Volume: Sound size, Value range 0.0 to 1.0.
    • Pitch: Playback speed, the value range between 3 to 3 is set 1 for normal playback, less than 1 for slowing playback greater than 1 for accelerated playback.
AudioClip

When we import a music into the Unity3d, the music file becomes a AudioClip object, that is, we can drag it directly into the AudioClip attribute of Audiosource, It can also be loaded through resources or assetbundle, and the type of object being loaded is AudioClip.

A simple example of playing music

In this example we do not write a single line of code to play the music directly with a simple drag-and-drop.

Create a new scene, add an audio source component to the main camera, and drag our music file onto the audio clip property, and tick loop to make it loop.

You can hear the sound as you run the program.

3D Sound Effects

Why does Unity want to split music playback into these 3 components? Of course there is a reason, personally think that the most important thing is to achieve 3D sound effects.

If we think of audio listener as a pair of ears, it's good to understand what a 3D sound effect is, and unity will be based on the Gameobject and audio of the audio listener object. The source of the gameobject determines the distance and position to simulate the real-world volume near the small effect.

First of all, find our imported music files, must be set to 3D music, the default is. Of course, if it is 2D music will not have nearly large and small effect.

We create a new scene, add 3 Gameobject, add an audio listener component to the first one, the other two add the audio source component and give two music files.

Remove the audio listener component from the main camera and place the 3 components in the following location.

Run the game, return to the scene window, drag the audio listener to the location of the component, you can feel like moving between the two audio effect.

Resources loading

Here we look at how the code implements music control and playback.

We put our music files in the resources folder, create a new scene and a gameobject, and add the following script to this gameobject:

1 usingUnityengine;2 3  Public classResourcesdemo:monobehaviour4 {5     PrivateAudiosource _audiosource;6 7     voidStart ()8     {9         //Add Audio Source componentTen_audiosource = This.gameobject.addcomponent<audiosource>(); One  A         //load Audio Clip object -AudioClip AudioClip = resources.load<audioclip> ("BGM"); -  the         //Play -_audiosource.loop =true; -_audiosource.clip =AudioClip; - _audiosource.play (); +     } -}

You can hear the sound as you run.

Assetbundle loading

How to control and play music in the Assetbundle?

First we need to package the file through a script.

1 usingUnityeditor;2 usingUnityengine;3 4  Public classAssetbundlecreator5 {6[MenuItem ("Demo/createassetbundle")]7     Private Static voidCreateassetbundle ()8     {9Buildpipeline.buildassetbundle (NULL,New []Ten             { OneAssetdatabase.loadmainassetatpath ("Assets/audio/bgm.ogg"), AAssetdatabase.loadmainassetatpath ("Assets/audio/sound.ogg") -             }, -Application.streamingassetspath +"/audio.assetbundle", thebuildassetbundleoptions.collectdependencies | Buildassetbundleoptions.completeassets |Buildassetbundleoptions.uncompressedassetbundle, - buildtarget.standalonewindows64); -     } -}

Next, create a new scene and a gameobject, and add the following script to this gameobject:

1 usingUnityengine;2 3  Public classAssetbundledemo:monobehaviour4 {5     PrivateAudiosource _audiosource;6 7     voidStart ()8     {9Assetbundle Assetbundle = assetbundle.createfromfile (Application.streamingassetspath +"/audio.assetbundle");Ten  One         //Add Audio Source component A_audiosource = This.gameobject.addcomponent<audiosource>(); -  -         //load Audio Clip object theAudioClip AudioClip = Assetbundle.load (" Sound",typeof(AudioClip)) asAudioClip; -  -         //Play -_audiosource.loop =true; +_audiosource.clip =AudioClip; - _audiosource.play (); +     } A}
source File Download

Http://pan.baidu.com/s/1eQCW7AE

Unity3d Music Sound Learning Notes

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.