/// <summary> /// Serverside: Play sound at given position for all clients. /// </summary> /// <param name="addressableAudioSource">The sound to be played.</param> /// <param name="worldPos">The position at which the sound is played</param> /// <param name="audioSourceParameters">Extra parameters of the audio source.</param> /// <param name="polyphonic">Is the sound to be played polyphonic</param> /// <param name="global">Does everyone will receive the sound our just nearby players</param> /// <param name="shakeParameters">Camera shake effect associated with this sound</param> /// <param name="sourceObj">The object that is the source of the sound</param> /// <returns>The SoundSpawn Token generated that identifies the same sound spawn instance across server and clients</returns> public static async Task <string> PlayNetworkedAtPosAsync(AddressableAudioSource addressableAudioSource, Vector3 worldPos, AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false, bool global = true, ShakeParameters shakeParameters = new ShakeParameters(), GameObject sourceObj = null) { if (addressableAudioSource == null || string.IsNullOrEmpty(addressableAudioSource.AssetAddress) || addressableAudioSource.AssetAddress == "null") { Logger.LogWarning($"SoundManager received a null AudioSource to be played at World Position: {worldPos}", Category.Audio); return(null); } addressableAudioSource = await AudioManager.GetAddressableAudioSourceFromCache(addressableAudioSource); if (global) { return(PlaySoundMessage.SendToAll(addressableAudioSource, worldPos, polyphonic, sourceObj, shakeParameters, audioSourceParameters)); } else { return(PlaySoundMessage.SendToNearbyPlayers(addressableAudioSource, worldPos, polyphonic, sourceObj, shakeParameters, audioSourceParameters)); } }
/// <summary> /// Play sound for all clients. /// If more than one sound is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">List of sounds to be played. If more than one sound is specified, one will be picked at random</param> public static async Task PlayNetworked(List <AddressableAudioSource> addressableAudioSources, float pitch = -1, bool polyphonic = false, bool shakeGround = false, byte shakeIntensity = 64, int shakeRange = 30) { ShakeParameters shakeParameters = null; if (shakeGround == true) { shakeParameters = new ShakeParameters { ShakeGround = shakeGround, ShakeIntensity = shakeIntensity, ShakeRange = shakeRange }; } AudioSourceParameters audioSourceParameters = null; if (pitch > 0) { audioSourceParameters = new AudioSourceParameters { Pitch = pitch }; } AddressableAudioSource addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSources); PlaySoundMessage.SendToAll(addressableAudioSource, TransformState.HiddenPos, polyphonic, null, shakeParameters, audioSourceParameters); }
/// <summary> /// Serverside: Play sound at given position for particular player. /// ("Doctor, there are voices in my head!") /// If more than one is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">The sound to be played. If more than one is specified, one will be picked at random.</param> public static async Task PlayNetworkedForPlayerAtPos(GameObject recipient, Vector3 worldPos, List <AddressableAudioSource> addressableAudioSources, float pitch = -1, bool polyphonic = false, bool shakeGround = false, byte shakeIntensity = 64, int shakeRange = 30, GameObject sourceObj = null) { ShakeParameters shakeParameters = null; if (shakeGround) { shakeParameters = new ShakeParameters { ShakeGround = shakeGround, ShakeIntensity = shakeIntensity, ShakeRange = shakeRange }; } AudioSourceParameters audioSourceParameters = null; if (pitch > 0) { audioSourceParameters = new AudioSourceParameters { Pitch = pitch }; } AddressableAudioSource addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSources); PlaySoundMessage.Send(recipient, addressableAudioSource, worldPos, polyphonic, sourceObj, shakeParameters, audioSourceParameters); }
/// <summary> /// Add an addressable audio source to the common source pool. /// Caching it and loading it in RAM in the same process. /// </summary> /// <param name="addressableAudioSources">The sound to be played. If more than one is specified, a single one will be picked at random</param> /// <returns>The addressable audio source with it's component loaded</returns> private static async Task <AddressableAudioSource> EnsureAddressableAudioSourceFromCache(List <AddressableAudioSource> addressableAudioSources) { AddressableAudioSource addressableAudioSource = addressableAudioSources.PickRandom(); AddressableAudioSource addressableAudioSourceFromCache = null; lock (Instance.SoundsLibrary) { addressableAudioSourceFromCache = Instance.SoundsLibrary.FirstOrDefault(p => p.AssetAddress == addressableAudioSource.AssetAddress); } if (addressableAudioSourceFromCache == null) { lock (Instance.SoundsLibrary) { Instance.SoundsLibrary.Add(addressableAudioSource); } } // Ensure it's loaded and valid AudioSource audioSource; GameObject gameObject = await addressableAudioSource.Load(); if (!gameObject.TryGetComponent(out audioSource)) { Logger.LogError($"AddressableAudioSource in SoundManager doesn't contain an AudioSource: {addressableAudioSource.AssetAddress}", Category.Addressables); return(null); } return(addressableAudioSource); }
/// <summary> /// Serverside: Play sound at given position for all clients. /// Please use PlayNetworkedAtPosAsync if possible. /// </summary> /// <param name="addressableAudioSource">The sound to be played.</param> /// <param name="worldPos">The position at which the sound is played</param> /// <param name="audioSourceParameters">Extra parameters of the audio source.</param> /// <param name="polyphonic">Is the sound to be played polyphonic</param> /// <param name="global">Does everyone will receive the sound our just nearby players</param> /// <param name="shakeParameters">Camera shake effect associated with this sound</param> /// <param name="sourceObj">The object that is the source of the sound</param> /// <returns>The SoundSpawn Token generated that identifies the same sound spawn instance across server and clients</returns> public static void PlayNetworkedAtPos(AddressableAudioSource addressableAudioSource, Vector3 worldPos, AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false, bool global = true, ShakeParameters shakeParameters = new ShakeParameters(), GameObject sourceObj = null) { _ = PlayNetworkedAtPosAsync(addressableAudioSource, worldPos, audioSourceParameters, polyphonic, global, shakeParameters, sourceObj); }
/// <summary> /// Play a sound locally /// If more than one is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">The sound to be played. If more than one is specified, one will be picked at random.</param> /// <param name="soundSpawnToken">The SoundSpawn Token that identifies the same sound spawn instance across server and clients</returns> /// <param name="audioSourceParameters">Parameters for how to play the sound</param> /// <param name="polyphonic">Should the sound be played polyphonically</param> public static async Task Play(List <AddressableAudioSource> addressableAudioSources, string soundSpawnToken = "", AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false) { AddressableAudioSource addressableAudioSource = addressableAudioSources.PickRandom(); await Play(addressableAudioSource, soundSpawnToken, audioSourceParameters, polyphonic); }
/// <summary> /// Send a Music to be played to a specific client /// </summary> /// <returns>The MusicSpawn Token generated that identifies the same Music spawn instance across server and clients</returns> public static void Send(GameObject recipient, AddressableAudioSource addressableAudioSource, Vector3 pos, GameObject sourceObj = null, AudioSourceParameters audioSourceParameters = new AudioSourceParameters()) { var netId = NetId.Empty; if (sourceObj != null) { var netB = sourceObj.GetComponent <NetworkBehaviour>(); if (netB != null) { netId = netB.netId; } } NetMessage msg = new NetMessage { MusicAddressablePath = addressableAudioSource.AssetAddress, TargetNetId = netId, AudioParameters = audioSourceParameters }; SendTo(recipient, msg); }
/// <summary> /// Send a sound to be played to all nearby players /// </summary> /// <returns>The SoundSpawn Token generated that identifies the same sound spawn instance across server and clients</returns> public static string SendToNearbyPlayers(AddressableAudioSource addressableAudioSource, Vector3 pos, bool polyphonic = false, GameObject sourceObj = null, ShakeParameters shakeParameters = new ShakeParameters(), AudioSourceParameters audioSourceParameters = new AudioSourceParameters()) { var netId = NetId.Empty; if (sourceObj != null) { var netB = sourceObj.GetComponent <NetworkBehaviour>(); if (netB != null) { netId = netB.netId; } } string soundSpawnToken = Guid.NewGuid().ToString(); PlaySoundMessage msg = new PlaySoundMessage { SoundAddressablePath = addressableAudioSource.AssetAddress, Position = pos, Polyphonic = polyphonic, TargetNetId = netId, ShakeParameters = shakeParameters, AudioParameters = audioSourceParameters, SoundSpawnToken = soundSpawnToken }; msg.SendToNearbyPlayers(pos); return(soundSpawnToken); }
public void MentionSoundIndexChange(int newValue) { MentionSoundIndex = newValue; CurrentMentionSound = MentionSounds[MentionSoundIndex]; PlayerPrefs.SetInt(PlayerPrefKeys.MentionSoundIndex, newValue); PlayerPrefs.Save(); }
/// <summary> /// Play explosion sound and shake ground /// </summary> /// <param name="worldPosition">position explosion is centered at</param> /// <param name="shakeIntensity">intensity of shaking</param> /// <param name="shakeDistance">how far away the shaking can be felt</param> public static void PlaySoundAndShake(Vector3Int worldPosition, byte shakeIntensity, int shakeDistance) { AddressableAudioSource explosionSound = EXPLOSION_SOUNDS.PickRandom(); AddressableAudioSource groanSound = STATION_GROAN_SOUNDS.PickRandom(); AddressableAudioSource distantSound = DISTANT_EXPLOSION_SOUNDS.PickRandom(); AudioSourceParameters audioSourceParameters = new AudioSourceParameters(0f, 100f); ShakeParameters shakeParameters = new ShakeParameters(true, shakeIntensity, shakeDistance); //Closet sound _ = SoundManager.PlayNetworkedAtPosAsync(explosionSound, worldPosition, audioSourceParameters, true, false, shakeParameters); //Next sound if (distantSound != null) { AudioSourceParameters distantSoundAudioSourceParameters = new AudioSourceParameters(0f, 100f, minDistance: 29, maxDistance: 63); _ = SoundManager.PlayNetworkedAtPosAsync(distantSound, worldPosition, distantSoundAudioSourceParameters); } //Furthest away sound if (groanSound != null) { AudioSourceParameters groanSoundAudioSourceParameters = new AudioSourceParameters(0f, 100f, minDistance: 63, maxDistance: 200); _ = SoundManager.PlayNetworkedAtPosAsync(groanSound, worldPosition, groanSoundAudioSourceParameters); } }
/// <summary> /// Play sound locally at given world position. /// If more than one element is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">Sound to be played. If more than one is specified, one will be picked at random.</param> /// <param name="soundSpawnToken">The token that identifies the SoundSpawn uniquely among the server and all clients </param> public static async Task PlayAtPosition(List <AddressableAudioSource> addressableAudioSources, string soundSpawnToken, Vector3 worldPos, bool polyphonic = false, bool isGlobal = false, uint netId = NetId.Empty, AudioSourceParameters audioSourceParameters = null) { AddressableAudioSource addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSources); SoundSpawn soundSpawn = Instance.GetSoundSpawn(addressableAudioSource, addressableAudioSource.AudioSource, soundSpawnToken); ApplyAudioSourceParameters(audioSourceParameters, soundSpawn); if (netId != NetId.Empty) { if (NetworkIdentity.spawned.ContainsKey(netId)) { soundSpawn.transform.parent = NetworkIdentity.spawned[netId].transform; soundSpawn.transform.localPosition = Vector3.zero; } else { soundSpawn.transform.parent = Instance.transform; soundSpawn.transform.position = worldPos; } } else { soundSpawn.transform.parent = Instance.transform; soundSpawn.transform.position = worldPos; } Instance.PlaySource(soundSpawn, polyphonic, isGlobal, audioSourceParameters != null && audioSourceParameters.MixerType != MixerType.Unspecified); }
/// <summary> /// Play sound for particular player. /// ("Doctor, there are voices in my head!") /// If more than one is specified, one will be picked at random. /// </summary> /// <param name="recipient">The player that will receive the sound</param> /// <param name="addressableAudioSources">A list of sounds. One will be played at random</param> /// <param name="audioSourceParameters">Extra parameters of the audio source.</param> /// <param name="polyphonic">Is the sound to be played polyphonic</param> /// <param name="shakeParameters">Camera shake effect associated with this sound</param> /// <param name="sourceObj">The object that is the source of the sound</param> public static async Task PlayNetworkedForPlayer(GameObject recipient, List <AddressableAudioSource> addressableAudioSources, AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false, ShakeParameters shakeParameters = new ShakeParameters(), GameObject sourceObj = null) { AddressableAudioSource addressableAudioSource = addressableAudioSources.PickRandom(); await PlayNetworkedForPlayer(recipient, addressableAudioSource, audioSourceParameters, polyphonic, shakeParameters, sourceObj).ConfigureAwait(false); }
/// <summary> /// Play sound locally. /// If more than one element is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">Sound to be played. If more than one is specified, one will be picked at random.</param> /// <param name="soundSpawnToken">The SoundSpawn Token that identifies the same sound spawn instance across server and clients</returns> /// <param name="polyphonic">Should the sound be played polyphonically</param> /// <param name="global">Should the sound be played for the default mixer or false to check if it should play muffled</param> /// <remarks> /// If Global is true, the sound may still be muffled if the source is configured with the muffled mixer. /// </remarks> public static async Task Play(List <AddressableAudioSource> addressableAudioSources, string soundSpawnToken, bool polyphonic = false, bool global = true) { AddressableAudioSource addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSources); var sound = Instance.GetSoundSpawn(addressableAudioSource, addressableAudioSource.AudioSource, soundSpawnToken); Instance.PlaySource(sound, polyphonic, global); }
/// <summary> /// Plays a sound for all clients. /// </summary> /// <param name="addressableAudioSource">The sound to be played.</param> /// <param name="audioSourceParameters">Extra parameters of the audio source</param> /// <param name="polyphonic">Is the sound to be played polyphonic</param> /// <param name="shakeParameters">Extra parameters that define the sound's associated shake</param> public static async Task PlayNetworked(AddressableAudioSource addressableAudioSource, AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false, ShakeParameters shakeParameters = new ShakeParameters()) { addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSource); PlaySoundMessage.SendToAll(addressableAudioSource, TransformState.HiddenPos, polyphonic, null, shakeParameters, audioSourceParameters); }
public void CmdPlayMusic(AddressableAudioSource addressableAudioSource, NetworkConnectionToClient sender = null) { if (IsAdmin(sender, out var admin) == false) { return; } MusicManager.PlayNetworked(addressableAudioSource); }
private void Start() { offSize = itemAttributes.Size; offHitSound = itemAttributes.ServerHitSound; offHitDamage = itemAttributes.ServerHitDamage; offThrowDamage = itemAttributes.ServerThrowDamage; offAttackVerbs = new List <string>(itemAttributes.ServerAttackVerbs); }
/// <summary> /// Play sound from a list for all clients. /// If more than one sound is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">A list of sounds. One will be played at random</param> /// <param name="audioSourceParameters">Extra parameters of the audio source</param> /// <param name="polyphonic">Is the sound to be played polyphonic</param> /// <param name="shakeParameters">Camera shake effect associated with this sound</param> public static async Task PlayNetworked(List <AddressableAudioSource> addressableAudioSources, AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false, ShakeParameters shakeParameters = new ShakeParameters()) { AddressableAudioSource addressableAudioSource = addressableAudioSources.PickRandom(); await PlayNetworked(addressableAudioSource, audioSourceParameters, polyphonic, shakeParameters).ConfigureAwait(false); }
/// <summary> /// Play sound from a list for all clients. /// If more than one sound is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">A list of sounds. One will be played at random</param> /// <param name="audioSourceParameters">Extra parameters of the audio source</param> /// <param name="polyphonic">Is the sound to be played polyphonic</param> /// <param name="shakeParameters">Camera shake effect associated with this sound</param> public static string PlayNetworked(List <AddressableAudioSource> addressableAudioSources, AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false, ShakeParameters shakeParameters = new ShakeParameters()) { AddressableAudioSource addressableAudioSource = addressableAudioSources.PickRandom(); return(PlayNetworked(addressableAudioSource, audioSourceParameters, polyphonic, shakeParameters)); }
/// <summary> /// Get a fully loaded addressableAudioSource from the loaded cache. This ensures that everything is ready to use. /// If more than one addressableAudioSource is provided, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">A list containing audio to be played. If more than one is specified, one will be picked at random.</param> /// <returns>A fully loaded and ready to use AddressableAudioSource</returns> public static async Task <AddressableAudioSource> GetAddressableAudioSourceFromCache(List <AddressableAudioSource> addressableAudioSources) { AddressableAudioSource addressableAudioSource = addressableAudioSources.PickRandom(); addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSource); return(addressableAudioSource); }
public void OnSpawnServer(SpawnInfo info) { // Roll for the big bell if (Random.value <= 0.005) { RingSound = BigBellRingSound; BellSpriteRenderer.ChangeSpriteVariant(1); } }
/// <summary> /// Play sound locally at given world position. /// This static method is for specifically attaching sound play to a target object (it will /// parent itself to the target and set its local position to Vector3.zero before playing) /// This is useful for moving objects that play sounds /// </summary> /// <param name="addressableAudioSources">Sound to be played.</param> /// <param name="soundSpawnToken">The SoundSpawn Token that identifies the same sound spawn instance across server and clients</returns> public static void PlayAtPositionAttached(AddressableAudioSource addressableAudioSource, Vector3 worldPos, GameObject gameObject, string soundSpawnToken, bool polyphonic = false, bool isGlobal = false, AudioSourceParameters audioSourceParameters = new AudioSourceParameters()) { PlayAtPositionAttached(new List <AddressableAudioSource> { addressableAudioSource }, worldPos, gameObject, soundSpawnToken, polyphonic, isGlobal, audioSourceParameters); }
private void ClientPlaySound(AddressableAudioSource sound) { if (CustomNetworkManager.IsHeadless) { return; } _ = SoundManager.PlayAtPosition(sound, gameObject.WorldPosClient()); }
public static void StopAudio(AddressableAudioSource audioSource) { if (audioSource == null || Instance.playingSource.ContainsKey(audioSource) == false) { return; } audioSource.AudioSource.loop = false; SoundManager.Stop(Instance.playingSource[audioSource]); }
/// <summary> /// Serverside: Play sound at given position for all clients. /// If more than one sound is specified, the sound will be chosen at random /// </summary> /// <param name="addressableAudioSource">The sound to be played.</param> /// <param name="worldPos">The position at which the sound is played</param> /// <param name="audioSourceParameters">Extra parameters of the audio source.</param> /// <param name="polyphonic">Is the sound to be played polyphonic</param> /// <param name="global">Does everyone will receive the sound our just nearby players</param> /// <param name="shakeParameters">Camera shake effect associated with this sound</param> /// <param name="sourceObj">The object that is the source of the sound</param> /// <returns>The SoundSpawn Token generated that identifies the same sound spawn instance across server and clients</returns> public static async Task <string> PlayNetworkedAtPosAsync(List <AddressableAudioSource> addressableAudioSources, Vector3 worldPos, AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false, bool global = true, ShakeParameters shakeParameters = new ShakeParameters(), GameObject sourceObj = null) { AddressableAudioSource addressableAudioSource = addressableAudioSources.PickRandom(); return(await PlayNetworkedAtPosAsync(addressableAudioSource, worldPos, audioSourceParameters, polyphonic, global, shakeParameters, sourceObj)); }
/// <summary> /// Play a sound locally /// If more than one is specified, one will be picked at random. /// </summary> /// <param name="addressableAudioSources">The sound to be played. If more than one is specified, one will be picked at random.</param> /// <param name="soundSpawnToken">The SoundSpawn Token that identifies the same sound spawn instance across server and clients</returns> /// <param name="audioSourceParameters">Parameters for how to play the sound</param> /// <param name="polyphonic">Should the sound be played polyphonically</param> public static async Task Play(List <AddressableAudioSource> addressableAudioSources, string soundSpawnToken, AudioSourceParameters audioSourceParameters, bool polyphonic = false) { AddressableAudioSource addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSources); SoundSpawn soundSpawn = Instance.GetSoundSpawn(addressableAudioSource, addressableAudioSource.AudioSource, soundSpawnToken); ApplyAudioSourceParameters(audioSourceParameters, soundSpawn); Instance.PlaySource(soundSpawn, polyphonic, forceMixer: audioSourceParameters != null && audioSourceParameters.MixerType != MixerType.Unspecified); }
/// <summary> /// Play a sound locally /// </summary> /// <param name="addressableAudioSource">The sound to be played.</param> /// <param name="soundSpawnToken">The SoundSpawn Token that identifies the same sound spawn instance across server and clients</returns> /// <param name="audioSourceParameters">Parameters for how to play the sound</param> /// <param name="polyphonic">Should the sound be played polyphonically</param> public static async Task Play(AddressableAudioSource addressableAudioSource, string soundSpawnToken = "", AudioSourceParameters audioSourceParameters = new AudioSourceParameters(), bool polyphonic = false) { addressableAudioSource = await GetAddressableAudioSourceFromCache(addressableAudioSource).ConfigureAwait(false); SoundSpawn soundSpawn = Instance.GetSoundSpawn(addressableAudioSource, addressableAudioSource.AudioSource, soundSpawnToken); ApplyAudioSourceParameters(audioSourceParameters, soundSpawn); Instance.PlaySource(soundSpawn, polyphonic, true, audioSourceParameters.MixerType); }
private void PlayAudio(AddressableAudioSource clipToPlay) { if (clipToPlay == null) { return; } SoundManager.Stop(guid); guid = Guid.NewGuid().ToString(); _ = SoundManager.Play(clipToPlay, guid); }
/// <summary> /// Send a Music to be played to all clients /// </summary> /// <returns>The MusicSpawn Token generated that identifies the same Music spawn instance across server and clients</returns> public static void SendToAll(AddressableAudioSource addressableAudioSource, AudioSourceParameters audioSourceParameters = new AudioSourceParameters()) { NetMessage msg = new NetMessage { MusicAddressablePath = addressableAudioSource.AssetAddress, AudioParameters = audioSourceParameters, }; SendToAll(msg); }
private SoundSpawn GetSoundSpawn(AddressableAudioSource addressableAudioSource, AudioSource audioSource, string soundSpawnToken) { if (NonplayingSounds.ContainsKey(addressableAudioSource.AssetAddress)) { var ToReturn = NonplayingSounds[addressableAudioSource.AssetAddress][0]; NonplayingSounds[addressableAudioSource.AssetAddress].RemoveAt(0); return(ToReturn); } return(GetNewSoundSpawn(addressableAudioSource, audioSource, soundSpawnToken)); }
private void PlayAudio(AddressableAudioSource clipToPlay) { if (clipToPlay == null) { return; } SoundAmbientManager.StopAudio(playing); playing = clipToPlay; SoundAmbientManager.PlayAudio(clipToPlay); }