예제 #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);
        }
예제 #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;
         }
     }
 }
예제 #3
0
        private void InitComponent(SoundComponent soundComponent)
        {
            InitAudioSource(soundComponent);
            soundComponent.AudioSource.loop   = soundComponent.Loop;
            soundComponent.AudioSource.volume = soundComponent.Volume;

            soundComponent.AddOnStop(StopSound);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
 }
예제 #8
0
 public void AddSoundComponent(SoundComponent soundComponent)
 {
     InitComponent(soundComponent);
     _sounds.Add(soundComponent);
 }
예제 #9
0
 private void StopSound(SoundComponent soundComponent)
 {
     DestroySound(soundComponent);
 }
예제 #10
0
 private void RemoveItemFromList(SoundComponent soundComponent)
 {
     _forRemoveFromListSounds.Add(soundComponent);
 }
예제 #11
0
 private void AddSoundComponentToSoundSystem(SoundComponent soundComponent)
 {
     _soundSystem.AddSoundComponent(soundComponent);
 }