You are on page 1of 2

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.SceneManagement;

public class AudioManager : MonoBehaviour


{
private static readonly string FirstPlay = "FirstPlay";
private static readonly string BackgroundPref = "BackgroundPref";
private static readonly string SoundEffectsPref = "SoundEffectsPref";
private int firstPlayInt;
public Slider backgroundSlider, soundEffectsSlider;
private float backgroundFloat, soundEffectsFloat;
public AudioSource[] backgroundAudio;
public AudioSource soundEffectsAudio;

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

if (firstPlayInt == 0)
{
backgroundFloat = .25f;
soundEffectsFloat = .75f;
backgroundSlider.value = backgroundFloat;
soundEffectsSlider.value = soundEffectsFloat;
PlayerPrefs.SetFloat(BackgroundPref, backgroundFloat);
PlayerPrefs.SetFloat(SoundEffectsPref, soundEffectsFloat);
PlayerPrefs.SetFloat(FirstPlay, -1);
}
else
{
backgroundFloat = PlayerPrefs.GetFloat(BackgroundPref);
backgroundSlider.value = backgroundFloat;
soundEffectsFloat = PlayerPrefs.GetFloat(SoundEffectsPref);
soundEffectsSlider.value = soundEffectsFloat;
}
}

public void SaveSoundSettings()


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

private void OnApplicationFocus(bool inFocus)


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

public void UpdateSound()


{
for (int i = 0; i < backgroundAudio.Length; i++)
{
backgroundAudio[i].volume = backgroundSlider.value;
}

soundEffectsAudio.volume = soundEffectsSlider.value;
}
}

You might also like