[Unity-2] Unity plays Music

Source: Internet
Author: User

Most of the functions in unity can be implemented by dragging and dropping, but in order to facilitate introductions, they are all implemented in code here.

There are 3 main elements to play music in Unity:

1.AudioSource: Controlling the body of music playback

2.AudioClip: Music that needs to be played

3.AudioListener: Add this control to hear the effect of music playback

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: To add a audiosource to Gameobject, the added function returns the added control and assigns the returned control to M_audiosource.

Second line of code: similarly

The third line of code: loading music from Resource/music/haha, haha is the name of the music.

The four lines of code: play Music.


[Unity-2] Unity plays 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.