示例#1
0
 private void _populatePool(AudioPoolType g)
 {
     while (_inactiveSources[g].Count < _poolSizes[g])
     {
         _inactiveSources[g].Push(PooledAudioSource.CreateInstance(this.transform, g));
     }
 }
    public static PooledAudioSource CreateInstance(Transform parent, AudioPoolType g)
    {
        GameObject        obj  = new GameObject("[" + Enum.GetName(typeof(AudioPoolType), g) + "] PooledAudioSource");
        PooledAudioSource inst = obj.AddComponent <PooledAudioSource>();

        inst._group = g;
        inst.Source = inst.gameObject.AddComponent <AudioSource>();
        inst.transform.SetParent(parent);
        inst.ResetValues(); // init values
        return(inst);
    }
示例#3
0
 public PooledAudioSource PlayDelayed(AudioClip clip, AudioPoolType group, string mixerGroup, float delay, Action onReturnedToPool)
 {
     return(Play(
                clip,
                group,
                mixerGroup,
                PooledAudioSource.DEFAULT_VOLUME,
                PooledAudioSource.DEFAULT_PITCH,
                PooledAudioSource.DEFAULT_LOOP,
                delay,
                onReturnedToPool));
 }
示例#4
0
 public PooledAudioSource Play(AudioClip clip, AudioPoolType group, string mixerGroup, float volume, float pitch, Action onReturnedToPool)
 {
     return(Play(
                clip,
                group,
                mixerGroup,
                volume,
                pitch,
                PooledAudioSource.DEFAULT_LOOP,
                PooledAudioSource.DEFAULT_DELAY,
                onReturnedToPool));
 }
示例#5
0
 public PooledAudioSource Play(AudioClip clip, AudioPoolType group, string mixerGroup, bool loop, Action onReturnedToPool)
 {
     return(Play(
                clip,
                group,
                mixerGroup,
                PooledAudioSource.DEFAULT_VOLUME,
                PooledAudioSource.DEFAULT_PITCH,
                loop,
                PooledAudioSource.DEFAULT_DELAY,
                onReturnedToPool));
 }
示例#6
0
    /// <summary>
    /// Plays a clip and returns the active audio source (for later reference). Returns null if no sources available in the pool.
    /// </summary>
    public PooledAudioSource Play(AudioClip clip, AudioPoolType group, string mixerGroup, float volume, float pitch, bool loop, float delay, Action onReturnedToPool)
    {
        PooledAudioSource source = _getFromPool(group);

        if (source == null)
        {
            return(null);
        }

        source.Init(clip, mixerGroup, volume, pitch, loop, onReturnedToPool);
        source.Play(delay);
        return(source);
    }
示例#7
0
 private PooledAudioSource _getFromPool(AudioPoolType g)
 {
     if (_inactiveSources[g].Count > 0)
     {
         PooledAudioSource source = _inactiveSources[g].Pop();
         _activeSources[g].Add(source);
         return(source);
     }
     else
     {
         Debug.LogWarning("[AudioManager] Audio source pool is empty! Consider increasing the size of the pool to play all these dang sounds.");
         return(null);
     }
 }
示例#8
0
 private void _updateSources()
 {
     // check for sources that need to be returned to the pool
     for (int i = 0; i < _allGroups.Length; i++)
     {
         AudioPoolType g = _allGroups[i];
         for (int j = _activeSources[g].Count - 1; j >= 0; j--)
         {
             if (_activeSources[g][j].IsStopped)
             {
                 _returnToPool(_activeSources[g][j]);
             }
         }
     }
 }
示例#9
0
    private void _initialize()
    {
        _mainMixer = Resources.Load <AudioMixer>(Constants.Mixer.Path);

        _inactiveSources = new Dictionary <AudioPoolType, Stack <PooledAudioSource> >();
        _activeSources   = new Dictionary <AudioPoolType, List <PooledAudioSource> >();

        _poolSizes = new Dictionary <AudioPoolType, uint>();
        _allGroups = (AudioPoolType[])Enum.GetValues(typeof(AudioPoolType));
        for (int i = 0; i < _allGroups.Length; i++)
        {
            AudioPoolType g = _allGroups[i];
            _poolSizes.Add(g, DEF_POOL_SIZE);
            _activeSources.Add(g, new List <PooledAudioSource>());
            _inactiveSources.Add(g, new Stack <PooledAudioSource>());
            _populatePool(g);
        }
    }
示例#10
0
    /// <summary>
    /// Resizes a pool to the specified size.
    /// If the size is smaller than the current number of active sources,
    /// they will not be removed until after they are finished playing.
    /// </summary>
    /// <param name="group">The pool being resized</param>
    /// <param name="size">The new size</param>
    public void SetPoolSize(AudioPoolType group, uint size)
    {
        if (size > _poolSizes[group])
        {
            // add more sources (ez)
            _poolSizes[group] = size;
            _populatePool(group);
        }
        else if (size < _poolSizes[group])
        {
            // remove sources (lil harder)
            // attempt to remove as many inactive sources as we can
            float delta = _poolSizes[group] - size;
            while (_inactiveSources[group].Count > 0 && delta > 0)
            {
                //TODO: move to "None" pool group instead of destroying?
                Destroy(_inactiveSources[group].Pop().gameObject);
            }

            _poolSizes[group] = size;
        }
    }
示例#11
0
 /// <summary>
 /// Plays a clip and does not return the source
 /// </summary>
 public void PlayOneShot(AudioClip clip, AudioPoolType group, string mixerGroup, float volume = PooledAudioSource.DEFAULT_VOLUME, float pitch = PooledAudioSource.DEFAULT_PITCH, float delay = PooledAudioSource.DEFAULT_DELAY)
 {
     Play(clip, group, mixerGroup, volume, pitch, false, delay, null);
 }