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); }
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; } } }
private void InitComponent(SoundComponent soundComponent) { InitAudioSource(soundComponent); soundComponent.AudioSource.loop = soundComponent.Loop; soundComponent.AudioSource.volume = soundComponent.Volume; soundComponent.AddOnStop(StopSound); }
private void OnUpdateComponent(SoundComponent soundComponent) { if (soundComponent.Started) { if (!soundComponent.Loop && !soundComponent.AudioSource.isPlaying) //Если компонент не зациклен и звук закончил воспроизведение { DestroySound(soundComponent); } return; } soundComponent.AudioSource.Play(); soundComponent.SetStarted(true); }
/// <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); }
/// <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); }
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); }
public void AddSoundComponent(SoundComponent soundComponent) { InitComponent(soundComponent); _sounds.Add(soundComponent); }
private void StopSound(SoundComponent soundComponent) { DestroySound(soundComponent); }
private void RemoveItemFromList(SoundComponent soundComponent) { _forRemoveFromListSounds.Add(soundComponent); }
private void AddSoundComponentToSoundSystem(SoundComponent soundComponent) { _soundSystem.AddSoundComponent(soundComponent); }