Unity Music Playback Global Class

Source: Internet
Author: User

Today late lol, found inside the sound system did very well, so recently wrote a copy anyway later also used to, 2D music global play.

The project is combined with the Poolmanager object pool plug-in.

The problem solved:

1. Already played sound, can pause or reduce the volume immediately, etc.

2. Achieve Music global control

3. Music and sound control separately

4. Music Overlay Playback

Classes are as follows:

Audioshot Music Entity class

Soundcontroller Global Music Control class

Soundpanel Music Panel

The code is as follows:

Audioshot Entity classes:

using Unityengine; using System.Collections;  Public class Audioshot:monobehaviour {    public  audiosource audiosource;      Public string Audioname;}

Soundcontroller class:

usingUnityengine;usingSystem.Collections;usingPathologicalgames;usingSystem.Collections.Generic;/** 2D Music Playback Global Control class * If the music is already playing, change the sound size, or stop playing * can control the music already playing immediately pause, or change the current volume*/ Public classSoundcontroller:monobehaviour {Private floatFxvolume =1;//Audio Sound size    Private floatMusicvolume =1;//Music Sound Size    Private BOOLIsplayfxvolume =true;//whether to play sound sound    Private BOOLIsplaymusicvolume =true;//whether to play music sounds     Publicaudioclip[] Fxclip;  Publicaudioclip[] Musicclip; PrivateSpawnpool Audiospwan;//Music Game Object Pool    PrivateList<audiosource> fxaudiolist;//To control the music that is already playing, you need to record their references    PrivateList<audiosource>musicaudiolist;  Public StaticSoundcontroller instance; Private intAudiocount;//Music Subscript Value Timer     Public voidAwake () {Audiospwan= poolmanager.pools["Audio"]; Fxaudiolist=NewList<audiosource>(); Musicaudiolist=NewList<audiosource>(); Instance= This; }    /// <summary>    ///play sound does not overlap/// </summary>    /// <param name= "name" ></param>     Public voidPlayfxsound (stringname) {        if(isplayfxvolume) { for(inti =0; i < fxclip.length; i++)            {                if(Name = =fxclip[i].name) {Audio.                    Playoneshot (Fxclip[i], fxvolume); return; }            }        }    }    /// <summary>    ///Play audio can be played overlay (Ngui playback mode, can only be played once complete to destroy, it is best not to use)/// </summary>    /// <param name= "name" ></param>     Public voidPlayfxsoundoverlap (stringname) {        if(Fxclip = =NULL)return; if(isplayfxvolume) { for(inti =0; i < fxclip.length; i++)            {                if(Name = =fxclip[i].name) {                    //Nguitools.playsound (Fxclipstatic[i], fxvolume);                    varGameobject = Audiospwan.spawn ("Audioshotgame"); GameObject.audio.clip=Fxclip[i]; GameObject.audio.volume=Fxvolume;                    GameObject.audio.Play (); Audioshot Model= gameobject.getcomponent<audioshot>(); if(string. IsNullOrEmpty (Model.audioname)) {Model.name= Audiocount +""; Audiocount++;      Fxaudiolist.add (Gameobject.audio); //Add to Collection                    }                    return; }            }        }    }        /// <summary>    ///Play Music/// </summary>    /// <param name= "name" ></param>     Public voidPlaymusicsound (stringname) {        if(isplaymusicvolume) { for(inti =0; i < musicclip.length; i++)            {                if(Name = =musicclip[i].name) {Audio.                    Playoneshot (Musicclip[i], musicvolume); return; }            }        }    }     Public voidPlaymusicsoundoverlap (stringname) {        if(Musicclip = =NULL)return; if(isplaymusicvolume) { for(inti =0; i < musicclip.length; i++)            {                if(Name = =musicclip[i].name) {                    varGameobject = Audiospwan.spawn ("Audioshotgame"); GameObject.audio.clip=Musicclip[i]; GameObject.audio.volume=Musicvolume;                    GameObject.audio.Play (); Audioshot Model= gameobject.getcomponent<audioshot>(); if(string. IsNullOrEmpty (Model.audioname)) {Model.audioname= Audiocount +""; Audiocount++; //Add to CollectionMusicaudiolist.add (Gameobject.audio); }                    return; }            }        }    }     Public voidUpdatefxvolume (floatvolume) {         This. Fxvolume =volume; foreach(varIinchfxaudiolist) {I.volume=volume; }    }     Public voidUpdatemusicvolume (floatvolume) {         This. Musicvolume =volume; foreach(varIinchmusicaudiolist) {I.volume=volume; }    }     Public voidIsplayfxvolume (BOOLisplay) {Isplayfxvolume=Isplay; if(Isplay = =false)        {            foreach(varIinchfxaudiolist) {I.volume=0; }        }        Else         {            foreach(varIinchfxaudiolist) {I.volume=Fxvolume; }        }    }     Public voidIsplaymusicvolume (BOOLisplay) {Isplaymusicvolume=Isplay; if(Isplay = =false)        {            foreach(varIinchmusicaudiolist) {I.volume=0; }        }        Else         {            foreach(varIinchmusicaudiolist) {I.volume=Musicvolume; }        }    }     Public voidClearaudiosource () { for(inti =0; i < Musicaudiolist.count; i++)        {            if(Musicaudiolist[i].isplaying = =false) {musicaudiolist.remove (musicaudiolist[i]); }        }         for(inti =0; i < Fxaudiolist.count; i++)        {            if(Fxaudiolist[i].isplaying = =false) {fxaudiolist.remove (fxaudiolist[i]); }        }    }    }

Music Control Panel class:

usingUnityengine;usingSystem.Collections; Public classSoundpanel:monobehaviour { Public voidUpdatefxvolume (floati) {SoundController.instance.UpdateFxVolume (i); }     Public voidUpdatemusicvolume (floati) {SoundController.instance.UpdateMusicVolume (i); }     Public voidIsplayfxsound (BOOLIsOpen)    {SoundController.instance.IsPlayFxVolume (IsOpen); }     Public voidIsplaymusicsound (BOOLIsOpen)    {SoundController.instance.IsPlayMusicVolume (IsOpen); }     Public voidPlaymusic () {SoundController.instance.PlayMusicSoundOverlap ("3550"); }     Public voidPlayfx () {SoundController.instance.PlayFxSoundOverlap ("1843"); }}

Source code Address: Http://yunpan.cn/cKSQeasSgVXbD Extract Code Dedb

Unity Music Playback Global Class

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.