Adding BGM And On/Off Sound Button - Easy Unity Tutorial
Тәжірибелік нұсқаулар және стиль
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
Пікірлер: 88
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! kzread.info/dash/bejne/q4t3qsONp7nInM4.html
@losantosw
2 жыл бұрын
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
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); } }
@HOJ2
11 ай бұрын
thx bro!
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!
the best one out there Had been looking for this for some days now. Thanks a lot bro, you saved me
Thank you for making these videos they're a really big help
Thanks for the tutorial, it helped!
Thanks for this amazing tutorial, it really helped
thanks dude, it really helped me for my thesis project :D
Amazing Tutorial! You got a new sub :)
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! you're the best ;)
Very useful, thanks!
It really helped me alot today and in future, Thanks for this wonderful information
Great video!!
Thanks a lot, your tutorial work 👍
It worked. Thanks
By the gods, you are a life saver T^T
thank you very much. it helped me a lot
Thanks A lot Man, New Subscribe ❤❤❤
Merci mec, tu m'as permis d'avoir 10%
Thanks Nice Work
I like your tutorial. Because have good UI & TEXT on video for extra teach
Thank you so much!!!!!
thank you!
Thank You Brother...
Thank you bro
thanks for video
thank you bro
Great
@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 🙂
Thank you
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.
best one
🔥
Tip for anyone wanting to mute all sound effects: write a script for the audio listener, not the audio source
Thanks
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?
I like video
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?
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
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
yo im having an issue where the on and off are on the screen at the same time. what do i do?
what is the getkeydown replacement for android can u help please?
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); }
I like the concept but i am using sliders to adjust audio source. how do i mute those/
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.
Hi the buttons weren't showing up is there a reason to it why isnt it showing when i hit play?
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
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; } } }
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
@sheejarajendran137
3 жыл бұрын
Please replay bro
@mcpedab7442
3 жыл бұрын
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.
@sheejarajendran137
3 жыл бұрын
@@mcpedab7442 ok thank you bro 😃
@densaldanha
2 жыл бұрын
@@mcpedab7442 Absolute life saver, thanks a lot!
@muraterguzel8795
2 жыл бұрын
@@mcpedab7442 Thanks! This is working
why I have errors like, error CS0117: 'PlayerPrefs' does not contain a definition for 'Haskey' ??
@hoosontech
3 жыл бұрын
Can I take a look at your script? You can post the codes here
@gomolemomelela6052
3 жыл бұрын
k needs to be a capital letter, so it needs to be HasKey, not Haskey
@yncor9756
2 жыл бұрын
HasKey
Make a mobile game in unity plz
that's very helpful thanks, btw, do you know how to add different BGM in different scenes?
@chsufyansharif2405
3 жыл бұрын
yes i knowm
@arindameika3805
3 жыл бұрын
@@chsufyansharif2405 Do you have the tutorial?
@chsufyansharif2405
3 жыл бұрын
@@arindameika3805 No i dont have tutorial
@himanshukharkar8886
3 жыл бұрын
make your bgm script static and use dontdestroyonload() method, thats all
@AYANKHAN-eh6ve
2 жыл бұрын
just add this to your very first scene, and the music will play all along until we exit the game.
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); } }
@moshi1002
Жыл бұрын
thanks mate
Bro I don't understand
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; }
@hoosontech
3 жыл бұрын
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
@wimpykiddd
3 жыл бұрын
@@hoosontech Hahaha XD my bad sorry, but thanks for the reply
Thanks