Exemplo n.º 1
0
        private void InitAudioSource(SoundComponent soundComponent)
        {
            AudioClip  clip     = soundComponent.AudioClip;
            GameObject target   = soundComponent.Target;
            bool       tracking = soundComponent.Tracking;

            if (target == null)
            {
                target = gameObject;
            }
            else
            {
                if (!tracking)
                {
                    Vector3 position = target.transform.position;
                    target = new GameObject(target.name + _additionalNameStaticSound);
                    target.transform.position = position;
                }
                soundComponent.SetTarget(target);
            }
            AudioSource audioSource = target.AddComponent <AudioSource>();

            audioSource.clip = clip;

            if (soundComponent.SoundType == SoundType.Sound3D)
            {
                audioSource.spatialBlend = 1;
            }

            soundComponent.SetAudioSource(audioSource);
        }
Exemplo n.º 2
0
 private void Update()
 {
     if (Input.GetKeyDown(KeyCode.X))
     {
         SoundCreator.Instance.Create(_nameSound, 1, false);
     }
     if (Input.GetKeyDown(KeyCode.C))
     {
         SoundCreator.Instance.Create(_nameSound, 1, true);
     }
     if (Input.GetKeyDown(KeyCode.V))
     {
         SoundCreator.Instance.Create(_nameSound, 1, false, _target, false);
     }
     if (Input.GetKeyDown(KeyCode.B))
     {
         SoundCreator.Instance.Create(_nameSound, 1, false, _target, true);
     }
     if (Input.GetKeyDown(KeyCode.N))
     {
         SoundCreator.Instance.Create(_nameSound, 1, true, _target, false);
     }
     if (Input.GetKeyDown(KeyCode.M))
     {
         _soundComponentLink = SoundCreator.Instance.Create(_nameSound, 1, true, _target, true);
     }
     if (Input.GetKeyDown(KeyCode.D))
     {
         if (_soundComponentLink != null)
         {
             _soundComponentLink.Stop();
             _soundComponentLink = null;
         }
     }
 }
Exemplo n.º 3
0
        private void InitComponent(SoundComponent soundComponent)
        {
            InitAudioSource(soundComponent);
            soundComponent.AudioSource.loop   = soundComponent.Loop;
            soundComponent.AudioSource.volume = soundComponent.Volume;

            soundComponent.AddOnStop(StopSound);
        }
Exemplo n.º 4
0
        private void OnUpdateComponent(SoundComponent soundComponent)
        {
            if (soundComponent.Started)
            {
                if (!soundComponent.Loop && !soundComponent.AudioSource.isPlaying)          //Если компонент не зациклен и звук закончил воспроизведение
                {
                    DestroySound(soundComponent);
                }
                return;
            }

            soundComponent.AudioSource.Play();
            soundComponent.SetStarted(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sound 2D
        /// </summary>
        /// <param name="name"></param>
        /// <param name="volume"></param>
        /// <param name="cyclical"></param>
        public SoundComponent Create(string name, float volume, bool cyclical)
        {
            AudioClip clip = GetClip(name);

            SoundComponent soundComponent = new SoundComponent(
                clip,
                name,
                volume,
                cyclical
                );

            AddSoundComponentToSoundSystem(soundComponent);

            return(soundComponent);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sound 3D
        /// </summary>
        /// <param name="name"></param>
        /// <param name="volume"></param>
        /// <param name="cyclical"></param>
        /// <param name="target"></param>
        /// <param name="tracking"></param>
        public SoundComponent Create(string name, float volume, bool cyclical, GameObject target, bool tracking)
        {
            AudioClip clip = GetClip(name);

            SoundComponent soundComponent = new SoundComponent(
                clip,
                name,
                volume,
                cyclical,
                target,
                tracking
                );

            AddSoundComponentToSoundSystem(soundComponent);

            return(soundComponent);
        }
Exemplo n.º 7
0
 private void DestroySound(SoundComponent soundComponent)
 {
     if (soundComponent.Target != null)
     {
         if (!soundComponent.Tracking)           //Если нет слежения, то надо уничтожить GameObject, т.к. он создан специально для статичного звука
         {
             Destroy(soundComponent.Target);
         }
         else
         {
             Destroy(soundComponent.AudioSource);
         }
     }
     else
     {
         Destroy(soundComponent.AudioSource);         //Так как нет цели, надо уничтожить только AudioSource
     }
     RemoveItemFromList(soundComponent);
 }
Exemplo n.º 8
0
 public void AddSoundComponent(SoundComponent soundComponent)
 {
     InitComponent(soundComponent);
     _sounds.Add(soundComponent);
 }
Exemplo n.º 9
0
 private void StopSound(SoundComponent soundComponent)
 {
     DestroySound(soundComponent);
 }
Exemplo n.º 10
0
 private void RemoveItemFromList(SoundComponent soundComponent)
 {
     _forRemoveFromListSounds.Add(soundComponent);
 }
Exemplo n.º 11
0
 private void AddSoundComponentToSoundSystem(SoundComponent soundComponent)
 {
     _soundSystem.AddSoundComponent(soundComponent);
 }