Пример #1
0
        void Awake()
        {
            if (max <= 0)
            {
                max = sfx.Length;
            }

            mDataLookup = new Dictionary <string, SoundData>(sfx.Length);
            mSfxPlaying = new SoundPlaying[max];

            GameObject holderGO = new GameObject("_sfx");

            mHolder        = holderGO.transform;
            mHolder.parent = transform;

            //look-up
            for (int i = 0; i < sfx.Length; i++)
            {
                mDataLookup.Add(sfx[i].name, sfx[i]);
            }

            //generate pool
            for (int i = 0; i < max; i++)
            {
                GameObject go = new GameObject(i.ToString());
                go.transform.parent = mHolder;

                mSfxPlaying[i]     = new SoundPlaying();
                mSfxPlaying[i].src = go.AddComponent(typeof(AudioSource)) as AudioSource;

                go.SetActive(false);
            }

            UserSettingAudio.instance.changeCallback += UserSettingsChanged;
        }
Пример #2
0
        IEnumerator DoSounds()
        {
            WaitForFixedUpdate wait = new WaitForFixedUpdate();

            mSoundActive = true;

            while (mSfxPlayingCount > 0)
            {
                for (int i = 0; i < mSfxPlayingCount; i++)
                {
                    SoundPlaying playing = mSfxPlaying[i];

                    bool isDone = false;

                    if (!playing.src.gameObject.activeSelf)
                    {
                        isDone = true;
                    }
                    else if (!playing.data.loop)
                    {
                        float duration = playing.data.clip.length + playing.data.delay;

                        if (playing.data.realtime)
                        {
                            isDone = Time.realtimeSinceStartup - playing.startTime >= duration;
                        }
                        else
                        {
                            isDone = Time.time - playing.startTime >= duration;
                        }
                    }

                    if (isDone)
                    {
                        playing.src.gameObject.SetActive(false);

                        if (mSfxPlayingCount > 1)
                        {
                            mSfxPlaying[i] = mSfxPlaying[mSfxPlayingCount - 1];
                            mSfxPlaying[mSfxPlayingCount - 1] = playing;
                            i--;
                        }
                        else
                        {
                            mSfxPlayingCount = 0;
                        }

                        if (playing.onEndCallback != null)
                        {
                            playing.onEndCallback(playing.onEndParam);
                        }
                    }
                }

                yield return(wait);
            }

            mSoundActive = false;
        }
Пример #3
0
 void UserSettingsChanged(UserSettingAudio us)
 {
     //if(audio.isPlaying)
     for (int i = 0; i < mSfxPlayingCount; i++)
     {
         SoundPlaying player = mSfxPlaying[i];
         player.src.volume = player.data.volume * us.soundVolume;
     }
 }
Пример #4
0
        public GameObject Play(string name, OnSoundEnd onEndCallback = null, object onEndParam = null)
        {
            SoundData dat;

            GameObject ret = null;

            if (mSfx.TryGetValue(name, out dat))
            {
                ret = GetAvailable();
                if (ret != null)
                {
                    ret.audio.clip   = dat.clip;
                    ret.audio.volume = dat.volume;
                    ret.audio.loop   = dat.loop;

                    SoundPlayer sp = ret.GetComponent <SoundPlayer>();
                    sp.playOnActive  = false;
                    sp.defaultVolume = dat.volume;
                    sp.playDelay     = dat.delay;

                    sp.Play();

                    SoundPlaying newPlay = new SoundPlaying()
                    {
                        data          = dat,
                        player        = sp,
                        startTime     = dat.realtime ? Time.realtimeSinceStartup : Time.time,
                        onEndCallback = onEndCallback,
                        onEndParam    = onEndParam
                    };

                    mSfxPlaying.Add(newPlay);

                    //   sp.StartCoroutine(OnSoundPlayFinish(dat.clip.length + dat.delay, ret, onEndCallback, onEndParam));

                    ret.SetActive(true);
                }

                /*else {
                 *  Debug.LogWarning("Ran out of available sound player for: " + name);
                 * }*/
            }
            else
            {
                Debug.LogWarning("sound player not found: " + name);
            }

            return(ret);
        }
Пример #5
0
        /// <summary>
        /// Call this with GameObject returned by Play, normally use this for looping clip
        /// </summary>
        public void Stop(AudioSource a)
        {
            a.Stop();

            //in case parent is set elsewhere
            if (a.transform.parent != mHolder)
            {
                a.transform.parent = mHolder;
            }

            int ind = -1;

            for (int i = 0; i < mSfxPlayingCount; i++)
            {
                if (mSfxPlaying[i].src == a)
                {
                    ind = i;
                    break;
                }
            }

            if (ind != -1)
            {
                SoundPlaying prev = mSfxPlaying[ind];

                a.gameObject.SetActive(false);

                if (mSfxPlayingCount > 1)
                {
                    mSfxPlaying[ind] = mSfxPlaying[mSfxPlayingCount - 1];
                    mSfxPlaying[mSfxPlayingCount - 1] = prev;
                }
                else
                {
                    mSfxPlayingCount = 0;
                }

                if (prev.onEndCallback != null)
                {
                    prev.onEndCallback(prev.onEndParam);
                }
            }
        }
Пример #6
0
        void OnDisable()
        {
            for (int i = 0; i < mSfxPlayingCount; i++)
            {
                SoundPlaying player = mSfxPlaying[i];

                //in case parent is set elsewhere
                if (player.src.transform.parent != mHolder)
                {
                    player.src.transform.parent = mHolder;
                }

                player.src.Stop();
                player.src.gameObject.SetActive(false);
            }

            mSfxPlayingCount = 0;

            StopAllCoroutines();
        }
Пример #7
0
        void Update()
        {
            for (int i = 0, max = mSfxPlaying.Count; i < max; i++)
            {
                SoundPlaying playing = mSfxPlaying[i];

                bool isDone = false;

                if (!playing.player.gameObject.activeSelf)
                {
                    isDone = true;
                }
                else if (!playing.data.loop)
                {
                    float duration = playing.data.clip.length + playing.data.delay;

                    if (playing.data.realtime)
                    {
                        isDone = Time.realtimeSinceStartup - playing.startTime >= duration;
                    }
                    else
                    {
                        isDone = Time.time - playing.startTime >= duration;
                    }
                }

                if (isDone)
                {
                    mSfxPlaying.RemoveAt(i);
                    max = mSfxPlaying.Count;
                    i--;

                    Stop(playing.player.gameObject);

                    if (playing.onEndCallback != null)
                    {
                        playing.onEndCallback(playing.onEndParam);
                    }
                }
            }
        }
Пример #8
0
        AudioSource Play(SoundData dat, OnSoundEnd onEndCallback, object onEndParam)
        {
            AudioSource ret = null;

            if (mSfxPlayingCount < sfx.Length)
            {
                SoundPlaying plyr = mSfxPlaying[mSfxPlayingCount];
                mSfxPlayingCount++;

                plyr.data          = dat;
                plyr.src.clip      = dat.clip;
                plyr.startTime     = dat.realtime ? Time.realtimeSinceStartup : Time.time;
                plyr.onEndCallback = onEndCallback;
                plyr.onEndParam    = onEndParam;
                plyr.src.volume    = dat.volume * UserSettingAudio.instance.soundVolume;
                plyr.src.loop      = dat.loop;

                if (dat.delay > 0.0f)
                {
                    audio.PlayDelayed(dat.delay);
                }
                else
                {
                    audio.Play();
                }

                if (!mSoundActive)
                {
                    StartCoroutine(DoSounds());
                }
            }
            else
            {
                Debug.LogWarning("Ran out of available player for: " + dat.name);
            }

            return(ret);
        }