How to put a soundtrack?

2

I'm doing a mobile game on Unity but I can not put a soundtrack, the sound file has already been converted, but I do not know the right command to start playing the track

I tried to use audio.PlayOneShot() , but I can not stop the sound when the game ends.

    
asked by anonymous 08.12.2014 / 03:08

1 answer

5

There is not much secret in using audio at Unity. The tool is very cool and facilitates this enormously. The whole point is that you need to necessarily have an audio source (that is, an object that contains the Audio Source component) and a "listener" (an object that contains the Audio Listener ).

When you create a new project, the Camera ( Main Camera ) already contains an Audio Listener by default (because it makes sense for the player to be able to hear when in the main camera). Since the game music is something that accompanies the player constantly, you can also add an Audio Source in the camera itself (select "Main Camera" in the Hierarchy window and click " Add Component "in the" Inspector "window by choosing" Audio -> Audio Source "), just to play the songs, then import the music as a new Asset menu New Asset ... ") and select it in the Audio Source component in the Audio Clip field (in the example, I am using the song

  

Important:Youcanleavechecked(bydefault)thePlayonoption  Awakethatcausesthesongtobestartedalongwiththeobject(inthe  examplecase,thecamerathatisstartedalongwiththegame).I  Ijustunmarkedforexamples.

OnceyouhaveanobjectattachedwithanAudioSourceandanAudioClip,simplyaccessthe%object_contentattributetoexecutethe%audioorPlay().The%wrappermethodyouusedrequiresyoutotelltheaudioclip(andthisisanotherpossibleapproach),butitisusuallymucheasierandmorepracticaltoleavethissettingthroughtheclipassignedtoaudiosource(ifyouwanttochangethemusic,itisbettertochangetheaudioclipintheobject,andthusthemusicstartandpausemenuneednotbemorecomplexthanIexemplifybelow.

ToillustratethecodeIhavecreatedtwobuttons,Stop()andPause(theyareactuallyGUITexturescreatedfromthemenuGameObject->CreateOther->GuiTextureThetextureimagesarefromthe

08.12.2014 / 13:25