Adding BGM And On/Off Sound Button - Easy Unity Tutorial
Vložit
- čas přidán 5. 08. 2024
- In this unity tutorial, you'll learn how to add background music and make an on/off sound button for our game. Background music enables players to immerse themselves in the world at hand and brings players gaming experience to the next level. Adding a on/off sound button in your game settings gives players the option to play with or without sound.
➤Adventure On The Road: assetstore.unity.com/packages...
Timestamps:
0:00 Intro
0:18 Background Music Scripting
1:18 Sound Button UI Setup
1:50 Sound Manager Scripting
5:14 Conclusion
#Hooson #Unity3D
Play my first mobile game: Bot Attack - 2D Top-down shooter
➤Google Play: play.google.com/store/apps/de...
➤App Store: Coming soon
➤Follow my Instagram: / hoosontech - Jak na to + styl
Hi guys! I have a tutorial on how to add a Volume Slider! Do check it out if you would like to add a Volume Slider instead of an On/Off Sound Button! czcams.com/video/yWCHaTwVblk/video.html
Thank you very much, I was already wondering where I would find another video where I would put in addition to the audio on and off options, a volume adjustment. Now I'll go to the next video to learn the other part lol
the best one out there
Had been looking for this for some days now.
Thanks a lot bro, you saved me
WOW, easy, simple and PERFECT. Thank you!
Thank you so much. You make this so simple and easy to understand!
Straight to the point, thanks a lot!
Awesome video dude, keep it up!
Thank you for making these videos they're a really big help
Hi i really like your channel man and your editing you also the explenation that you give us while you are working on it i really love it
thanks dude, it really helped me for my thesis project :D
Amazing Tutorial!
You got a new sub :)
Thanks for the tutorial, it helped!
Thanks for this amazing tutorial, it really helped
It really helped me alot today and in future, Thanks for this wonderful information
Very useful, thanks!
thanks dude! you're the best ;)
Thanks a lot, your tutorial work 👍
Great video!!
It worked. Thanks
thank you very much. it helped me a lot
By the gods, you are a life saver T^T
Thanks A lot Man, New Subscribe ❤❤❤
thank you!
I like your tutorial. Because have good UI & TEXT on video for extra teach
Thanks Nice Work
Thank you so much!!!!!
@Hooson how would you add pressedState for the mute buttons with this solution you showed us in the video (changing sprites)? You know, to indicate to the users that the button was pressed (e.g. changing the sprite's color to indicate that).
Right now utilizing your method I am unable to find a solution on how to do apply a pressed effect to the mute/unmute button. Any suggestions? Thanks 🙂
Merci mec, tu m'as permis d'avoir 10%
Great
Thank you bro
Thank You Brother...
thanks for video
thank you bro
Thank you
Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SoundManager : MonoBehaviour
{
[SerializeField] Image soundOnIcon;
[SerializeField] Image soundOffIcon;
private bool muted = false;
// Start is called before the first frame update
void Start()
{
if(!PlayerPrefs.HasKey("muted"))
{
PlayerPrefs.SetInt("muted", 0);
Load();
}
UpdateButtonIcon();
AudioListener.pause = muted;
}
public void OnButtonPress()
{
if (muted == false)
{
muted = true;
AudioListener.pause = true;
}
else
{
muted = false;
AudioListener.pause = false;
}
Save();
UpdateButtonIcon();
}
private void UpdateButtonIcon()
{
if(muted == false)
{
soundOnIcon.enabled = true;
soundOffIcon.enabled = false;
}
else
{
soundOnIcon.enabled = false;
soundOffIcon.enabled = true;
}
}
private void Load()
{
muted = PlayerPrefs.GetInt("muted") == 1;
}
private void Save()
{
PlayerPrefs.SetInt("muted", muted ? 1 : 0);
}
}
thx bro!
Hi @Hooson,
I replicated this code to make something to turn sounds and effects on and off. The problem is that in the effects, where I set another audio, when I click the button it plays the audio sound again. I don't understand why this behavior, since they are two different scripts. Can you help me?
best one
The PlayerPrefs part of this isn't working like I'd expect, if I close the game, change scene or something, the music will always start on regardless if I turned it off or not and I don't know why, is there something I'm missing?, code is identical, isn't it mean to be persistently saved?
Thanks
I have a problem, when I changed scenes the icon goes back to OnMusic, but last time i left it, it is on OffMusic icon
how would i do it to turn off only music and not sound effects? Pausing the audio listener turns off all the sounds
yo im having an issue where the on and off are on the screen at the same time. what do i do?
Hello i got problem that is, whenever the game starts the sound off button is enabled its not saving as set in last game,i got same issue in slider also (as u pinned in comment video).any suggestions.
save system is working properly the only issue is when I go to option menu it restart the setting (when I set muted and go to option menu its unmuted)
i want to in one scene in specify dont play that music
how can i do it?
🔥
I know this a long time vid, but just to ask why at the unity On Click Part, I cant add Function to it
Congratulations
It's an amazing video.
I managed to do what I was looking for to see your video.
I would be very grateful if you activated the subtitles in Spanish to fully understand your logic, I like how you explain your video, but I don't fully understand it.
what is the getkeydown replacement for android can u help please?
I like video
Hi the buttons weren't showing up is there a reason to it why isnt it showing when i hit play?
Tip for anyone wanting to mute all sound effects: write a script for the audio listener, not the audio source
I like the concept but i am using sliders to adjust audio source. how do i mute those/
Please, help me. What should I write here?
UpdateButtonIcon();
AudioListener.pause = muted;
if I use AudioMixer
public void MusicButtonOnOff()
{
if (mutedMusic == false)
{
mutedMusic = true;
Mixer.audioMixer.SetFloat("Music", -80);
}
else
{
mutedMusic = false;
Mixer.audioMixer.SetFloat("Music", 0);
}
why mine didnt have music if i change to different scene? please helppp
after going back to sound button it restart and play the music
but it also muted the sfx how to make it only stop the bgm
why I have errors like, error CS0117: 'PlayerPrefs' does not contain a definition for 'Haskey' ??
Can I take a look at your script? You can post the codes here
k needs to be a capital letter, so it needs to be HasKey, not Haskey
HasKey
Please make this for vibrate
Ok bro it's working but there is a problem for me, when I mute my music and go to another scene and come back again but the audio is playing, it's not saving
How to solve this
Please replay bro
Ya I was having the same issue.
I had to do a bit of tweaking to the Sound Manager script but it now works.
Here's the code
using UnityEngine;
using UnityEngine.UI;
public class SoundManager : MonoBehaviour
{
[SerializeField]
GameObject soundonImage;
[SerializeField]
GameObject soundoffImage;
private bool muted = false;
void Start()
{
muted = PlayerPrefs.GetInt("muted") == 1;
if(muted == false)
{
soundonImage.SetActive(true);
soundoffImage.SetActive(false);
}
else
{
soundonImage.SetActive(false);
soundoffImage.SetActive(true);
}
if (PlayerPrefs.HasKey("muted"))
{
Load();
}
else
{
PlayerPrefs.SetInt("muted",1);
Save();
}
AudioListener.pause = muted;
}
private void UpdateButtonIcon()
{
if(muted == false)
{
soundonImage.SetActive(true);
soundoffImage.SetActive(false);
}
else
{
soundonImage.SetActive(false);
soundoffImage.SetActive(true);
}
}
public void OnButtonPress()
{
if (muted == false)
{
muted = true;
AudioListener.pause = true;
}
else
{
muted = false;
AudioListener.pause = false;
}
UpdateButtonIcon();
Save();
}
private void Load()
{
muted = PlayerPrefs.GetInt("muted") == 1;
}
private void Save()
{
PlayerPrefs.SetInt("muted", muted ? 1 : 0);
}
}
Just copy paste the script and reassign the values and it should work like a charm.
@@mcpedab7442 ok thank you bro 😃
@@mcpedab7442 Absolute life saver, thanks a lot!
@@mcpedab7442 Thanks! This is working
Backgound Music:
using UnityEngine;
public class BackgroundMusic : MonoBehaviour
{
private static BackgroundMusic backgroundMusic;
void Awake()
{
if (backgroundMusic == null)
{
backgroundMusic = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
}
SoundManager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SoundManager : MonoBehaviour
{
[SerializeField] Image soundOnIcon;
[SerializeField] Image soundOffIcon;
private bool muted = false;
// Start is called before the first frame update
void Start()
{
if (!PlayerPrefs.HasKey("muted"))
{
PlayerPrefs.SetInt("muted", 0);
Load();
}
else
{
Load();
}
UpdateButtonIcon();
AudioListener.pause = muted;
}
public void OnButtonPress()
{
if (muted == false)
{
muted = true;
AudioListener.pause = true;
}
else
{
muted = false;
AudioListener.pause = false;
}
Save();
UpdateButtonIcon();
}
private void Load()
{
muted = PlayerPrefs.GetInt("muted") == 1;
}
private void Save()
{
PlayerPrefs.SetInt("muted", muted ? 1 : 0);
}
private void UpdateButtonIcon()
{
if (muted)
{
soundOnIcon.enabled = false;
soundOffIcon.enabled = true;
}
else
{
soundOnIcon.enabled = true;
soundOffIcon.enabled = false;
}
}
}
that's very helpful thanks, btw, do you know how to add different BGM in different scenes?
yes i knowm
@@chsufyansharif2405 Do you have the tutorial?
@@arindameika3805 No i dont have tutorial
make your bgm script static and use dontdestroyonload() method, thats all
just add this to your very first scene, and the music will play all along until we exit the game.
Make a mobile game in unity plz
Bro I don't understand
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SoundManager : MonoBehaviour
{
[SerializeField] Image SoundONNIcon;
[SerializeField] Image SoundOFFIcon;
private bool muted = false;
void Start()
{
if (!PlayerPrefs.HasKey("muted"))
{
PlayerPrefs.SetInt("muted", 0);
Load();
}
else
{
Load();
}
UpdateButtonIcon();
AudioListener.pause = muted;
}
public void OnButtonPress()
{
if (muted == false)
{
muted = true;
AudioListener.pause = true;
}
else
{
muted = false;
AudioListener.pause = false;
}
Save();
UpdateButtonIcon();
}
private void UpdateButtonIcon()
{
if(muted == false)
{
SoundONNIcon.enabled = true;
SoundOFFIcon.enabled = true;
}
else
{
SoundONNIcon.enabled = false;
SoundOFFIcon.enabled = true;
}
}
private void Load()
{
muted = PlayerPrefs.GetInt("muted") == 1;
}
private void Save()
{
PlayerPrefs.SetInt("muted", muted ? 1 : 0);
}
}
thanks mate
fuck me i hate coding.
I have trouble
"Assets\script\SoundManager.cs(16,13): error CS0103: The name 'PlayerPref' does not exist in the current context"
here the code
void Start()
{
if (!PlayerPrefs.HasKey("muted"))
{
PlayerPref.SetInt("muted", 0);
Load();
}
else
{
Load();
}
UpdateButtonIcon();
AudioListener.pause = muted;
}
There is a slight typo error in line 16. It should be "PlayerPrefs.SetInt("muted", 0);". You're missing a letter "s" in your PlayerPref
@@hoosontech Hahaha XD my bad sorry, but thanks for the reply
Thanks