/// <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 static PlaySoundMessage SendToAll(string sndName, Vector3 pos, bool polyphonic = false, GameObject sourceObj = null, ShakeParameters shakeParameters = null, AudioSourceParameters audioSourceParameters = null) { var netId = NetId.Empty; if (sourceObj != null) { var netB = sourceObj.GetComponent <NetworkBehaviour>(); if (netB != null) { netId = netB.netId; } } PlaySoundMessage msg = new PlaySoundMessage { SoundName = sndName, Position = pos, Polyphonic = polyphonic, TargetNetId = netId, ShakeParameters = shakeParameters, AudioSourceParameters = audioSourceParameters }; msg.SendToAll(); return(msg); }
public override void Deserialize(NetworkReader reader) { SoundName = reader.ReadString(); Position = reader.ReadVector3(); Polyphonic = reader.ReadBoolean(); TargetNetId = reader.ReadUInt32(); ShakeParameters = JsonConvert.DeserializeObject <ShakeParameters>(reader.ReadString()); AudioSourceParameters = JsonConvert.DeserializeObject <AudioSourceParameters>(reader.ReadString()); }
/// <summary> /// Send to all client to change the mixer of a playing sound /// </summary> /// <param name="soundName">The name of the sound.</param> /// <param name="audioSourceParameters">The Audio Source Parameters to apply.</param> /// <returns>The sent message</returns> public static ChangeAudioSourceParametersMessage SendToAll(string soundName, AudioSourceParameters audioSourceParameters) { ChangeAudioSourceParametersMessage msg = new ChangeAudioSourceParametersMessage { SoundName = soundName, AudioSourceParameters = audioSourceParameters }; msg.SendToAll(); return(msg); }
public override void Process() { if (string.IsNullOrEmpty(SoundAddressablePath)) { Logger.LogError(ToString() + " has no Addressable Path!", Category.Audio); return; } bool isPositionProvided = Position.RoundToInt() != TransformState.HiddenPos; if (AudioSourceParameters == null) { AudioSourceParameters = new AudioSourceParameters(); } // Recompose a list of a single AddressableAudioSoure from its primart key (Guid) List <AddressableAudioSource> addressableAudioSources = new List <AddressableAudioSource>() { new AddressableAudioSource(SoundAddressablePath) }; if (isPositionProvided) { SoundManager.PlayAtPosition(addressableAudioSources, SoundSpawnToken, Position, Polyphonic, netId: TargetNetId, audioSourceParameters: AudioSourceParameters); } else { SoundManager.Play(addressableAudioSources, SoundSpawnToken, AudioSourceParameters, Polyphonic); } if (ShakeParameters != null && ShakeParameters.ShakeGround) { if (isPositionProvided && PlayerManager.LocalPlayerScript && !PlayerManager.LocalPlayerScript.IsPositionReachable(Position, false, ShakeParameters.ShakeRange)) { //Don't shake if local player is out of range return; } float intensity = Mathf.Clamp(ShakeParameters.ShakeIntensity / (float)byte.MaxValue, 0.01f, 10f); Camera2DFollow.followControl.Shake(intensity, intensity); } }
public override void Process() { if (string.IsNullOrEmpty(SoundName)) { Logger.LogError(ToString() + " has no SoundName!", Category.Audio); return; } bool isPositionProvided = Position.RoundToInt() != TransformState.HiddenPos; if (AudioSourceParameters == null) { AudioSourceParameters = new AudioSourceParameters(); } if (isPositionProvided) { SoundManager.PlayAtPosition(SoundName, Position, Polyphonic, netId: TargetNetId, audioSourceParameters: AudioSourceParameters); } else { SoundManager.Play(SoundName, AudioSourceParameters, Polyphonic); } if (ShakeParameters != null && ShakeParameters.ShakeGround) { if (isPositionProvided && PlayerManager.LocalPlayerScript && !PlayerManager.LocalPlayerScript.IsInReach(Position, false, ShakeParameters.ShakeRange)) { //Don't shake if local player is out of range return; } float intensity = Mathf.Clamp(ShakeParameters.ShakeIntensity / (float)byte.MaxValue, 0.01f, 10f); Camera2DFollow.followControl.Shake(intensity, intensity); } }
/// <summary> /// Send to a specific client to change the Audio Source Parameters of a playing sound /// </summary> /// <param name="recipient">The recipient of the message to be sent.</param> /// <param name="soundSpawnToken">The token that identifies the SoundSpawn uniquely among the server and all clients </param> /// <param name="audioSourceParameters">The Audio Source Parameters to apply.</param> /// <returns>The sent message</returns> public static ChangeAudioSourceParametersMessage Send(GameObject recipient, string soundSpawnToken, AudioSourceParameters audioSourceParameters) { ChangeAudioSourceParametersMessage msg = new ChangeAudioSourceParametersMessage { SoundSpawnToken = soundSpawnToken, AudioSourceParameters = audioSourceParameters }; msg.SendTo(recipient); return(msg); }
public override void Deserialize(NetworkReader reader) { SoundSpawnToken = reader.ReadString(); AudioSourceParameters = JsonConvert.DeserializeObject <AudioSourceParameters>(reader.ReadString()); }