Example #1
0
        public static int SchedulePercussion(PercussionEnum percussionEnum, int volume, float scheduleTime = 0)
        {
            if (!instance)
            {
                return(0);
            }

            if (scheduleTime <= 0)
            {
                return(Percussion(percussionEnum, volume));
            }
            else
            {
                instance.percussionAudioClip = percussionSettings.GetAudioClip(percussionEnum);
                if (instance.percussionAudioClip)
                {
                    var item = new ScheduledPercussion();
                    item.clip    = instance.percussionAudioClip;
                    item.volume  = volume.ToVolume();
                    item.dspTime = AudioSettings.dspTime + scheduleTime;
                    instance.scheduledPercussionItems.Add(item);

                    //Debug.Log("Adding percussion : " + item.dspTime);

                    instance.scheduledPercussionItems.Sort((a, b) =>
                    {
                        return(-1 * a.dspTime.CompareTo(b.dspTime));
                    });

                    return(1);
                }
            }
            return(0);
        }
Example #2
0
            public void Schedule(ScheduledPercussion scheduledPercussion)
            {
                this.dspTime = scheduledPercussion.dspTime;

                if (audioSource)
                {
                    audioSource.clip   = scheduledPercussion.clip;
                    audioSource.volume = scheduledPercussion.volume;
                    audioSource.PlayScheduled(scheduledPercussion.dspTime);
                }
            }
Example #3
0
        //void OnGUI()
        //{
        //    GUILayout.Label(playingPercussionItems.Count.ToString());
        //}

        void Update()
        {
            for (int i = playingPercussionItems.Count - 1; i >= 0; i--)
            {
                if (!playingPercussionItems[i].IsPlaying())
                {
                    playingPercussionItems[i].TryRelease();
                    playingPercussionItems.RemoveAt(i);
                }
            }

            for (int i = scheduledPercussionItems.Count - 1; i >= 0; i--)
            {
                scheduledPercussion = scheduledPercussionItems[i];

                if (audioSourcePool.AvailableCount > 0)
                {
                    audioSourcePoolItem = audioSourcePool.Acquire();

                    if (!audioSourcePoolItem.audioSource)
                    {
                        audioSourcePoolItem.audioSource             = gameObject.AddComponent <AudioSource>();
                        audioSourcePoolItem.audioSource.playOnAwake = false;
                    }

                    audioSourcePoolItem.Schedule(scheduledPercussion);
                    playingPercussionItems.Add(audioSourcePoolItem);
                    playingPercussionItems.Sort((a, b) =>
                    {
                        return(-1 * a.dspTime.CompareTo(b.dspTime));
                    });

                    scheduledPercussionItems.RemoveAt(i);
                }
            }
        }