Adding BGM And On/Off Sound Button - Easy Unity Tutorial

Sdílet
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

Komentáře • 88

  • @hoosontech
    @hoosontech  Před 3 lety +8

    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

    • @losantosw
      @losantosw Před 2 lety

      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

  • @KRaJWanders
    @KRaJWanders Před 6 měsíci

    the best one out there
    Had been looking for this for some days now.
    Thanks a lot bro, you saved me

  • @phellippemottarini
    @phellippemottarini Před rokem

    WOW, easy, simple and PERFECT. Thank you!

  • @VeronicaJessie
    @VeronicaJessie Před 3 lety +2

    Thank you so much. You make this so simple and easy to understand!

  • @jasperhwong3341
    @jasperhwong3341 Před 3 lety

    Straight to the point, thanks a lot!

  • @gabrielulate
    @gabrielulate Před 3 lety

    Awesome video dude, keep it up!

  • @crispychips9868
    @crispychips9868 Před 2 lety

    Thank you for making these videos they're a really big help

  • @kingstar9995
    @kingstar9995 Před 2 lety

    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

  • @Blazikennnn
    @Blazikennnn Před 3 lety

    thanks dude, it really helped me for my thesis project :D

  • @rodsal
    @rodsal Před 3 lety

    Amazing Tutorial!
    You got a new sub :)

  • @eddiea6569
    @eddiea6569 Před 3 lety

    Thanks for the tutorial, it helped!

  • @zamharir_
    @zamharir_ Před 3 lety

    Thanks for this amazing tutorial, it really helped

  • @saiganeshr1436
    @saiganeshr1436 Před 2 lety

    It really helped me alot today and in future, Thanks for this wonderful information

  • @CutyDina
    @CutyDina Před rokem

    Very useful, thanks!

  • @yncor9756
    @yncor9756 Před 2 lety

    thanks dude! you're the best ;)

  • @megakumalasarihrp6785
    @megakumalasarihrp6785 Před 2 lety

    Thanks a lot, your tutorial work 👍

  • @chermainehoo214
    @chermainehoo214 Před 3 lety

    Great video!!

  • @dazniputra
    @dazniputra Před 3 lety

    It worked. Thanks

  • @alphaplus4969
    @alphaplus4969 Před 3 lety

    thank you very much. it helped me a lot

  • @makofujihara5533
    @makofujihara5533 Před 2 lety

    By the gods, you are a life saver T^T

  • @ahmedkhabar
    @ahmedkhabar Před 2 lety

    Thanks A lot Man, New Subscribe ❤❤❤

  • @VIMES_
    @VIMES_ Před 4 měsíci

    thank you!

  • @hdggfhjb6768
    @hdggfhjb6768 Před 3 lety

    I like your tutorial. Because have good UI & TEXT on video for extra teach

  • @syedmuhammadalishah5684

    Thanks Nice Work

  • @luck8762
    @luck8762 Před 2 lety

    Thank you so much!!!!!

  • @MegaTVscreen
    @MegaTVscreen Před 2 lety +3

    @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 🙂

  • @remybeaumont8089
    @remybeaumont8089 Před 2 lety

    Merci mec, tu m'as permis d'avoir 10%

  • @Asif-ze5hp
    @Asif-ze5hp Před 9 měsíci

    Great

  • @hasanboyhakimov9144
    @hasanboyhakimov9144 Před 2 lety

    Thank you bro

  • @indiestudio23
    @indiestudio23 Před 2 lety

    Thank You Brother...

  • @ake.studio
    @ake.studio Před 2 lety

    thanks for video

  • @sadksazdag851
    @sadksazdag851 Před 2 lety

    thank you bro

  • @oterobot
    @oterobot Před rokem +1

    Thank you

  • @abderrazeksimmou8753
    @abderrazeksimmou8753 Před 3 lety +25

    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
      @HOJ2 Před 11 měsíci +2

      thx bro!

  • @losantosw
    @losantosw Před 2 lety

    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?

  • @enjoylearning8585
    @enjoylearning8585 Před 2 lety

    best one

  • @UpcomingChris
    @UpcomingChris Před 2 lety +1

    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?

  • @PixluxGames
    @PixluxGames Před rokem

    Thanks

  • @neowb8495
    @neowb8495 Před 3 lety

    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

  • @saswatamohanta1023
    @saswatamohanta1023 Před 2 lety

    how would i do it to turn off only music and not sound effects? Pausing the audio listener turns off all the sounds

  • @benjib20
    @benjib20 Před 6 měsíci

    yo im having an issue where the on and off are on the screen at the same time. what do i do?

  • @shivaprasadkm5423
    @shivaprasadkm5423 Před rokem

    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.

  • @rizwanshaikh8175
    @rizwanshaikh8175 Před 2 lety

    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)

  • @FranyxD
    @FranyxD Před 3 lety

    i want to in one scene in specify dont play that music
    how can i do it?

  • @atokosto
    @atokosto Před 2 lety

    🔥

  • @weejunjie93
    @weejunjie93 Před 2 lety

    I know this a long time vid, but just to ask why at the unity On Click Part, I cant add Function to it

  • @miguelespeso401
    @miguelespeso401 Před rokem

    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.

  • @shahpranay1236
    @shahpranay1236 Před 3 lety

    what is the getkeydown replacement for android can u help please?

  • @nguyentrunghieu6865
    @nguyentrunghieu6865 Před 2 lety

    I like video

  • @xcade2652
    @xcade2652 Před 10 měsíci

    Hi the buttons weren't showing up is there a reason to it why isnt it showing when i hit play?

  • @CurtisJensenGames
    @CurtisJensenGames Před 3 lety +2

    Tip for anyone wanting to mute all sound effects: write a script for the audio listener, not the audio source

  • @talismanskulls2857
    @talismanskulls2857 Před rokem

    I like the concept but i am using sliders to adjust audio source. how do i mute those/

  • @vladimirvetchinnikov6916

    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);
    }

  • @F.Fifie.
    @F.Fifie. Před 2 lety

    why mine didnt have music if i change to different scene? please helppp

  • @rizwanshaikh8175
    @rizwanshaikh8175 Před 2 lety

    after going back to sound button it restart and play the music

  • @adsikaros9611
    @adsikaros9611 Před 5 měsíci

    but it also muted the sfx how to make it only stop the bgm

  • @NeyNyxia
    @NeyNyxia Před 3 lety

    why I have errors like, error CS0117: 'PlayerPrefs' does not contain a definition for 'Haskey' ??

    • @hoosontech
      @hoosontech  Před 3 lety

      Can I take a look at your script? You can post the codes here

    • @gomolemomelela6052
      @gomolemomelela6052 Před 3 lety

      k needs to be a capital letter, so it needs to be HasKey, not Haskey

    • @yncor9756
      @yncor9756 Před 2 lety

      HasKey

  • @texnofest
    @texnofest Před 3 lety

    Please make this for vibrate

  • @sheejarajendran137
    @sheejarajendran137 Před 3 lety +3

    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
      @sheejarajendran137 Před 3 lety +1

      Please replay bro

    • @mcpedab7442
      @mcpedab7442 Před 3 lety +4

      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
      @sheejarajendran137 Před 3 lety +1

      @@mcpedab7442 ok thank you bro 😃

    • @densaldanha
      @densaldanha Před 2 lety

      @@mcpedab7442 Absolute life saver, thanks a lot!

    • @muraterguzel8795
      @muraterguzel8795 Před 2 lety

      @@mcpedab7442 Thanks! This is working

  • @IlyasMMVII
    @IlyasMMVII Před 7 měsíci +2

    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;
    }
    }
    }

  • @arindameika3805
    @arindameika3805 Před 3 lety

    that's very helpful thanks, btw, do you know how to add different BGM in different scenes?

  • @syedsadiq8631
    @syedsadiq8631 Před 3 lety

    Make a mobile game in unity plz

  • @SheReact
    @SheReact Před rokem

    Bro I don't understand

  • @daniionas2888
    @daniionas2888 Před rokem +1

    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);
    }
    }

  • @sillyninja65
    @sillyninja65 Před 3 lety +2

    fuck me i hate coding.

  • @wimpykiddd
    @wimpykiddd Před 3 lety

    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
      @hoosontech  Před 3 lety

      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
      @wimpykiddd Před 3 lety

      @@hoosontech Hahaha XD my bad sorry, but thanks for the reply

  • @JuanFranco-mq7js
    @JuanFranco-mq7js Před 2 lety

    Thanks