[Unity-2] unity playing music

Source: Internet
Author: User

Most of the functions in unity can be implemented through drag and drop, but in order to facilitate the introduction, code is used here.

There are three main elements for playing music in Unity:

1. audiosource: controls the subject of music playback.

2. audioclip: The music to be played.

3. audiolistener: add this control to hear the effect of playing music.

The Code is as follows:

 
Audiosource m_audiosource = gameobject. addcomponent <audiosource> (); audiolistener m_audiolistener = gameobject. addcomponent <audiolistener> (); audioclip m_audioclips = (audioclip) resources. load <audioclip> ("music/haha"); m_audiosource.playoneshot (m_audioclips );

The first line of code: add an audiosource to the gameobject. The added function returns the added control and assigns the returned control to m_audiosource.

Line 2 code: Same

Line 3: Load music from resource/music/Haha. Haha is the name of the music.

Line 4 Code: play music.


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.