0% found this document useful (0 votes)
33 views2 pages

Audio Manager

This script manages audio in a Unity project. It gets the stored background volume setting from PlayerPrefs on start up and applies it. It also allows changing the background volume with a slider and saves the new value. It is a singleton that persists between scenes.

Uploaded by

Francisco Leite
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

Audio Manager

This script manages audio in a Unity project. It gets the stored background volume setting from PlayerPrefs on start up and applies it. It also allows changing the background volume with a slider and saves the new value. It is a singleton that persists between scenes.

Uploaded by

Francisco Leite
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using UnityEngine;

using UnityEngine.UI;

public class AudioManager : MonoBehaviour


{
private static readonly string FirstPlay = "FirsPlay";
private static readonly string BackgroundPref = "BackgroundPref";
private int firstPlayInt;
private float backgroundFloat;
public Slider backgroundSlider;
public AudioSource backgroundAudio;
public static AudioManager instance;

void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
return;
}
DontDestroyOnLoad(gameObject);
}

void Start()
{
firstPlayInt = PlayerPrefs.GetInt(FirstPlay);

if (firstPlayInt == 0)
{
backgroundFloat = .3f;
backgroundSlider.value = backgroundFloat;

PlayerPrefs.SetFloat(BackgroundPref, backgroundFloat);

PlayerPrefs.SetInt(FirstPlay, -1);
}
else
{
backgroundFloat = PlayerPrefs.GetFloat(BackgroundPref);
backgroundSlider.value = backgroundFloat;
}
}

public void UpdateSound()


{
backgroundAudio.volume = backgroundSlider.value;
}

public void SaveSoundSettings()


{
PlayerPrefs.SetFloat(BackgroundPref, backgroundSlider.value);
}

void OnApplicationFocus(bool inFocus)


{
if (!inFocus)
{
SaveSoundSettings();
}
}
}

You might also like