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);
            }
        }
Beispiel #2
0
 public void PlayEnemySound(Vector3 pos, AudioClip clip, float range)
 {
     if (clip != null)
     {
         TemporaryAudioSource.PlayTempClip(clip, pos);
     }
     PlayerCamera.CastWaveOnAll(pos, 15f, range, Color.yellow, Color.red, Color.red);
 }
    public static void PlayTempClip(AudioClip clip, Vector3 pos)
    {
        GameObject go = new GameObject();

        go.transform.position = pos;
        TemporaryAudioSource src = go.AddComponent <TemporaryAudioSource>();

        src.Play(clip);
    }
        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);
        }
Beispiel #6
0
 public void PlayHearableSound(Vector3 pos, AudioClip clip, float range)
 {
     if (clip != null)
     {
         TemporaryAudioSource.PlayTempClip(clip, pos);
     }
     PlayerCamera.CastWaveOnAll(pos, 10f, range, Color.cyan, Color.blue, Color.magenta);
     if (Vector3.Distance(pos, jason.transform.position) < range)
     {
         jason.SetDestination(pos);
     }
 }
Beispiel #7
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);
        }
        /// <summary>
        /// Init totalUnusedAudioList, unusedAudioList and usedAudioList and fill the first two.
        /// </summary>
        void InitTemporaryAudioList()
        {
            totalUnusedAudioList = new List <TemporaryAudioSource>();
            unusedAudioList      = new List <TemporaryAudioSource>();
            usedAudioList        = new List <TemporaryAudioSource>();

            TemporaryAudioSource tempAudio;

            foreach (Transform child in unusedAudioGO.transform)
            {
                tempAudio = new TemporaryAudioSource()
                            .SetAudioGO(child.gameObject);
                ResetAudioSourceValues(child.gameObject.AddComponent <AudioSource>());
                totalUnusedAudioList.Add(tempAudio);
                unusedAudioList.Add(tempAudio);
            }
        }