Beispiel #1
0
    /// <summary>
    /// Get audiosourceassociate to a key if it not exist will create a new depend of parameter
    /// </summary>
    /// <param name="a_key">key of the audio</param>
    /// <param name="out_res">out</param>
    /// <param name="a_createIfNotPresent">Do we create a new source if not exist</param>
    /// <returns>True if the key ever exist, false if not</returns>
    bool GetAudioSource(int a_key, out AudioSourceExtend out_res, bool a_createIfNotPresent = true)
    {
        Assert.IsFalse(a_key <0 || a_key> m_maxAllocatedKey, "Bad sound key");

        bool res = true;

        if (!m_audioSourcesExtendWithKey.TryGetValue(a_key, out out_res))
        {
            res = false;
            if (a_createIfNotPresent)
            {
                out_res = new AudioSourceExtend(CreateAudioSource());
                if (a_key != (int)AUDIOSOURCE_KEY.NO_KEY_AUTODESTROY)
                {
                    m_audioSourcesExtendWithKey.Add(a_key, out_res);
                    out_res.Speed       = -m_speedFade;
                    out_res.AutoDestroy = true;
                }
                else
                {
                    out_res.AutoDestroy = true;
                }
                out_res.Key = a_key;
                m_audioSourcesExtend.Add(out_res);
            }
        }


        return(res);
    }
Beispiel #2
0
    //TODO see to move initialisation of audiosource elsewhere - store key inside audio extends instead of dictionnary?
    //TODO : Store a part of audioclips here and associate key

    /// <summary>
    /// Start an audio depending of parameters - TODO simplify it and move elswhere some logic
    /// </summary>
    /// <param name="a_clip">Clip we want to play</param>
    /// <param name="a_mixerGroupType">Mixer we want to output</param>
    /// <param name="a_isFading">If the clip is fading</param>
    /// <param name="a_isLooping">If the clip it autolooping</param>
    /// <param name="a_key">the key we want</param>
    /// <param name="a_delay">if we play with a delay</param>
    /// <returns></returns>
    public int StartAudio(AudioClip a_clip, MIXER_GROUP_TYPE a_mixerGroupType = MIXER_GROUP_TYPE.AMBIANT, bool a_isFading = true, bool a_isLooping = true, AUDIOSOURCE_KEY a_key = AUDIOSOURCE_KEY.CREATE_KEY, ulong a_delay = 0)
    {
        int res = -1;
        int key = (int)a_key;

        res = GetKey(key);
        AudioMixerGroup mixer = null;

        try
        {
            mixer = m_listMixerGroup.Find(x => x.MixerType == a_mixerGroupType).MixerGroup;
            AudioSourceExtend source;

            if (GetAudioSource(key, out source))
            {
                if (source.AudioSource.clip == a_clip)
                {
                    Debug.Log("Clip ever play : " + a_clip.name);
                    return(key);
                }
                AudioSourceExtend new_source = new AudioSourceExtend(CreateAudioSource());
                m_audioSourcesExtend.Add(new_source);
                m_audioSourcesExtendWithKey[key] = new_source;
                if (a_isFading)
                {
                    source.Speed = -m_speedFade;
                }
                else
                {
                    source.Stop();
                }
                source.AutoDestroy = true;
                AudioSourceExtend temp = source;
                source     = new_source;
                new_source = temp;
            }

            if (a_isFading)
            {
                source.Speed = m_speedFade;
                source.AudioSource.volume = 0;
            }

            source.AudioSource.outputAudioMixerGroup = mixer;
            source.AudioSource.clip = a_clip;
            source.AudioSource.loop = a_isLooping;
            source.AudioSource.Play(a_delay);

            Debug.Log("StartAudio : " + a_clip.name);
        }
        catch
        {
            Debug.LogError("Error when try to launch sound");
            if (mixer == null)
            {
                Debug.LogError("Mixer doesn't exist");
            }
        }
        return(res);
    }