Forked from fairinrenish/gist:da233147657e563d45ac3f7a8ffde398
Last active
July 24, 2018 18:39
-
-
Save codehoose/63eef427cc9e7403f695b3a76f0975fc to your computer and use it in GitHub Desktop.
Slider Volume updation after returning to main menu from a different scene
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class SliderVolumeValue : MonoBehaviour | |
{ | |
const float DefaultVolume = 1f; | |
// Reference to Audio Source component | |
private AudioSource audioSrc; | |
private void Awake() | |
{ | |
audioSrc = GetComponent<AudioSource>(); | |
var volume = PlayerPrefs.GetFloat("musicVol", DefaultVolume); | |
audioSrc.volume = volume; | |
} | |
// Method that is called by slider game object | |
// This method takes vol value passed by slider | |
// and sets it as musicValue | |
public void SetVolume(float vol) | |
{ | |
PlayerPrefs.SetFloat("musicVol", vol); | |
PlayerPrefs.Save(); | |
audioSrc.volume = vol; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment