Example #1
0
    /// <summary>
    /// Returns a channeld idx to play a sound.
    /// Could be:
    /// 1. An empty channel (not yet used)
    /// 2. An IDLE channel
    /// 3. A busy channel but with less priority
    /// 4. A busy channel with the same priority
    ///
    /// If there isn't a channel that satisfy these conditions, returns -1
    ///
    /// </summary>
    /// <returns></returns>
    private static int getChannelIdx(SoundProp sp)
    {
        for (int i = 0; i < CHANNELS; i++)
        {
            if (fx[i].clip != null)
            {
                if (!fx[i].isPlaying)
                {
                    // Found a audiosource that is not currently being played

                    if (fx[i].clip != sp.audioClip)
                    {
                        return(i);
                    }
                }
            }
            else
            {
                return(i);
            }
        }

        // No audiosource idle. Find a busy audiosource with less priority than the new one
        for (int i = 0; i < CHANNELS; i++)
        {
            SoundProp prop = soundList.GetSoundPropByName(fx[i].clip.name);
            if (sp.priority > prop.priority)
            {
                return(i);
            }
        }

        // Try something with the same priority
        for (int i = 0; i < CHANNELS; i++)
        {
            SoundProp prop = soundList.GetSoundPropByName(fx[i].clip.name);
            if (sp.priority == prop.priority)
            {
                return(i);
            }
        }

        // Cannot find a suitable channel
        return(-1);
    }