Skip to content

Adding custom sound effects (v1.7)

Daniel Andrusiewicz edited this page Oct 2, 2024 · 3 revisions

Written by @IvythePoS

Prepare your custom sound effects

  1. Get an audio file of the sound you want to add, either WAV or OGG are recommended.
  2. in your project, go to Assets/Resources/Sound/, and place your sound effect either directly in that folder or inside a subfolder that suits the sound effect best.
  3. Look for Enums.cs, and inside the script, look for public enum Sounds : byte.
  4. Copy and paste (or select text, then press CTRL + D to duplicate) any sound data entry, such as:
    [SoundData("world/block_powerup")]                      World_Block_Powerup = 69,
    and change the filepath within the SoundData attribute to point to the audio file you just added (without "Assets/Resources/Sound"). You should also change the enum's name (currently World_Block_Powerup), as well as its value (currently 69 (it's nice indeed)). If you don't change the name of the sound, it will return an error saying that it's already defined. If you don't change the value, it will be read as the one that's already defined with another sound.

Playing sounds

If you want a sound to play in MainMenuManager.cs or GameManager.cs, you can use sfx.PlayOneShot(Enums.Sounds.Your_Sound.GetClip()); where you think it's appropriate. If you have using NSMB.Extensions; at the top of your .cs file, you can omit the .GetClip().

If you want to place it in PlayerController.cs for the player, there's three different options:

  1. PlaySound(Enums.Sounds.Your_Sound); will play it for the local player only, taking into account the camera's distance to the sound source.

  2. PlaySoundEverywhere(Enums.Sounds.Your_Sound); will play it for the local player only, regardless of the camera's distance from the sound source.

  3. photonView.RPC(nameof(PlaySound), RpcTarget.All, Enums.Sounds.Your_Sound); will play it for all players over the network, in the scene at your current location.

    Note that if you use the photonView.RPC version in code that runs for every player (such as PlayerController.HandleMovement), it will play too many times. If you are unsure, add a photonView.IsMine check before the RPC. For example:

    if (photonView.IsMine) {
        photonView.RPC(nameof(PlaySound), RpcTarget.All, Enums.Sounds.World_Block_Bump);
    }