private static bool CanPlaySound(SoundAudioClip newSound) { switch (newSound.sound) { default: return(true); case Sound.PlayerMove: if (soundTimerDictionary.ContainsKey(newSound.sound)) { float lastTimePlayed = soundTimerDictionary[newSound.sound]; float playerMoverTimerMax = newSound.audioTime; if (lastTimePlayed + playerMoverTimerMax < Time.time) { soundTimerDictionary[newSound.sound] = Time.time; return(true); } else { return(false); } } else { return(true); } } }
public void PlaySound(Sound sound, bool loop = false) { GameObject soundObj = new GameObject("Sound"); AudioSource audioSource = soundObj.AddComponent <AudioSource>(); SoundAudioClip soundAudioClip = GetSoundAudioClip(sound); // get volume float musicVolume = SettingsManager.Instance.MusicVolume; float sfxVolume = SettingsManager.Instance.SFXVolume; float volume; if (soundAudioClip.soundType == SoundType.MUSIC) { volume = musicVolume / 10.0f; audioSource.loop = true; } else { volume = sfxVolume / 10.0f; } audioSource.clip = soundAudioClip.audioClip; audioSource.loop = loop; audioSource.volume = volume; audioSource.Play(); if (!loop) { StartCoroutine(DelayDestroyAudio(audioSource)); } }
public static void PauseAllPausableSounds() { for (int i = 0; i < Sounds.Instance.SoundsAudioClips.Length; i++) { SoundAudioClip soundAudioClip = Sounds.Instance.SoundsAudioClips[i]; if (soundAudioClip.IsPausable) { Sounds.Instance.SoundsAudioClips[i].AudioSource.Pause(); } } }
public void Awake() { soundAudioClip = new SoundAudioClip(); if (instance == null) { instance = this; } else if (instance != null) { Destroy(gameObject); } }
public void PlaySound(Sound sound) { SoundAudioClip sc = GetAudioClip(sound); if (CanPlaySound(sc)) { GameObject soundGameObject = new GameObject("Sound"); AudioSource audioSource = soundGameObject.AddComponent <AudioSource>(); audioSource.PlayOneShot(sc.audioClip, efectsVolume); Destroy(soundGameObject, sc.audioClip.length); } }
public void PlaySound(Sounds sound, float pan = 1f, float pitch = 1f) { GameObject soundObj = new GameObject("Sound"); AudioSource newSound = soundObj.AddComponent <AudioSource>(); SoundAudioClip soundAudio = GetAudioClip(sound); newSound.clip = soundAudio.audioClip; newSound.volume = soundAudio.volume; newSound.panStereo = soundAudio.panEffect * pan; newSound.pitch = pitch; newSound.Play(); Destroy(soundObj, newSound.clip.length / newSound.pitch); }
public static void PlaySound(Sound sound) { GameObject soundGameObject = new GameObject("Sound"); AudioSource audioSource = soundGameObject.AddComponent <AudioSource>(); SoundAudioClip soundAudioClip = GetAudioClip(sound); if (soundAudioClip == null) { return; } audioSource.clip = soundAudioClip.audioClip; audioSource.volume = soundAudioClip.volume; audioSource.Play(); Object.Destroy(soundGameObject, 5f); }
public void PlaySound(SoundNames soundName) { SoundAudioClip chosenSound = GetChosenSoundByName(soundName); AudioSource source = chosenSound.AudioSource; if (soundName == SoundNames.Jump) { source.pitch = 1 + (Random.Range(-JumpPitchAlteration, JumpPitchAlteration)); } else if (soundName == SoundNames.CollectCorrect) { source.pitch = goodCollectionPitch; } else { source.pitch = 1; } source.loop = chosenSound.Loop; source.volume = chosenSound.Volume; source.clip = chosenSound.AudioClip; source.Play(); }
public void StopSound(SoundNames soundName) { SoundAudioClip chosenSound = GetChosenSoundByName(soundName); chosenSound.AudioSource.Stop(); }
// Test if a sound can be played or need some time and store it if it exist private bool CanPlaySound(Sound sound) { //Check if the sound exist foreach (SoundAudioClip soundAudioClip in soundAudioClipList) { if (soundAudioClip.sound == sound) { if (soundAudioClip.audioClip != null) { currentCreatedClip = soundAudioClip; // If it exist, check if that sound have a cooldown or not (and is in cooldown or not) switch (sound) { case Sound.EnemyMove: if (soundTimerDictionary.ContainsKey(sound)) { float lastTimePlayed = soundTimerDictionary[sound]; float playerMoveTimerMax = delayWalkSound; if (lastTimePlayed + playerMoveTimerMax < Time.time) { soundTimerDictionary[sound] = Time.time; // Randomize the sound of walk var random = Random.value; if (random >= 0.75) { currentCreatedClip.audioClip = enemyMovementList[0]; } if (random >= 0.50 && random < 0.75) { currentCreatedClip.audioClip = enemyMovementList[1]; } if (random >= 0.25 && random < 0.50) { currentCreatedClip.audioClip = enemyMovementList[2]; } if (random < 0.25) { currentCreatedClip.audioClip = enemyMovementList[3]; } return(true); } else { return(false); } } break; case Sound.none: return(false); default: return(true); } } else { Debug.LogWarning("AudioClip not found"); return(false); } } } return(false); }