public void StopAllAudiosOfAnGameObject(GameObject targetFather)
        {
            List <TemporaryAudioSource> elementsToRemove = new List <TemporaryAudioSource>();

            foreach (Transform child in targetFather.transform)
            {
                TemporaryAudioSource targetTemporaryAudioSource = usedAudioList.Find(x => x.GetAudioGO() == child.gameObject);
                if (targetTemporaryAudioSource != null)
                {
                    AudioSource audio = targetTemporaryAudioSource.GetAudioGO().GetComponent <AudioSource>();
                    audio.Stop();

                    if (targetTemporaryAudioSource.GetCoroutineForStop() != null)
                    {
                        StopCoroutine(targetTemporaryAudioSource.GetCoroutineForStop());
                    }

                    unusedAudioList.Add(usedAudioList[usedAudioList.IndexOf(targetTemporaryAudioSource)]);
                    elementsToRemove.Add(targetTemporaryAudioSource);

                    ResetAudioSourceValues(audio);
                }
            }

            //Remove the elements added in elementsToRemove
            foreach (TemporaryAudioSource elementToRemove in elementsToRemove)
            {
                UnattachAudioGO(elementToRemove.GetAudioGO());
                usedAudioList.Remove(elementToRemove);
            }
        }
        public void PlayAudio(Enum enumType, bool stopOnSceneChange, AudioSource _targetAudioSource = null)
        {
            TemporaryAudioSource temporaryAudio = null;

            if (_targetAudioSource == null)
            {
                temporaryAudio = GetUnusedAudioGameObject();

                GameObject audioSourceGO = temporaryAudio.GetAudioGO();

                //This is for put a name in the gameobject for a easy search in the Unity hierarchy
                string gameObjectName = Enum.GetName(enumType.GetType(), enumType);

                audioSourceGO.name             = gameObjectName + "Audio";
                audioSourceGO.transform.parent = null;

                if (audioSourceGO.GetComponent <AudioSource>() != null)
                {
                    _targetAudioSource = audioSourceGO.GetComponent <AudioSource>();
                }
                else
                {
                    _targetAudioSource = audioSourceGO.AddComponent <AudioSource>();
                }

                temporaryAudio.SetStopOnSceneChange(stopOnSceneChange);
            }

            SoundType soundType = listSoundType.Find(x => x.EnumType == enumType.GetType());

            _targetAudioSource.clip = soundType.GetAudioClip(enumType);

            //This line put the correct Output in the AudioSource for link this sound with te audio configuration.
            AudioMixerGroup[] mixersGroup = audioMixerMaster.FindMatchingGroups(soundType.AudioMixerGroup);
            if (mixersGroup.Length != 0)
            {
                _targetAudioSource.outputAudioMixerGroup = mixersGroup[0];
            }
            else
            {
                Debug.LogError("AudioMixerGroup " + soundType.AudioMixerGroup + " don't exist inside of AudioMixer");
            }

            _targetAudioSource.Play();

            if (_targetAudioSource.loop == false)
            {
                if (temporaryAudio != null)
                {
                    temporaryAudio.SetCoroutineForStop(StartCoroutine(StopAudioWhenFinish(_targetAudioSource)));
                }
                else
                {
                    StartCoroutine(StopAudioWhenFinish(_targetAudioSource));
                }
            }
        }
        /// <summary>
        /// Return and unusedAudio and refress the lists
        /// </summary>
        /// <returns></returns>
        public TemporaryAudioSource GetUnusedAudioGameObject()
        {
            TemporaryAudioSource audioToSend = unusedAudioList[0];

            usedAudioList.Add(unusedAudioList[0]);
            unusedAudioList.RemoveAt(0);
            audioToSend.GetAudioGO().SetActive(true);

            return(audioToSend);
        }
Esempio n. 4
0
        public void BuildSoundEffect()
        {
            stopOnSceneChange = stopOnSceneChange ?? defaultStopOnSceneChange;
            TemporaryAudioSource temporaryAudio = SoundSystemBankManager.Instance.GetUnusedAudioGameObject()
                                                  .SetStopOnSceneChange((bool)stopOnSceneChange)
                                                  .SetGameObjectAttached(father);

            GameObject audioGO = temporaryAudio.GetAudioGO();

            audioGO.name = Enum.GetName(GetEnumType().GetType(), GetEnumType()) + "Audio";

            AudioSource audio = PrepareAudioSourceAndGO(audioGO);

            SoundSystemBankManager.Instance.PlayAudio(GetEnumType(), (bool)stopOnSceneChange, audio);
        }