Added audiosettings.txt as audio save and load, v1.0.2

This commit is contained in:
Nicola Sovic 2022-02-24 18:09:46 +01:00
parent 82640ba473
commit 94bff88d4c
4 changed files with 48 additions and 3 deletions

View File

@ -1,5 +1,7 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -17,6 +19,8 @@ public class AudioHandler : MonoBehaviour
AudioSource cameraAudio; AudioSource cameraAudio;
AudioSource playerAudio; AudioSource playerAudio;
string filepath = "./audiosettings.txt";
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
@ -96,11 +100,47 @@ public class AudioHandler : MonoBehaviour
public void changeVolumeEffects() public void changeVolumeEffects()
{ {
playerAudio.volume = GameObject.Find("slideMusic").GetComponent<Slider>().value; playerAudio.volume = GameObject.Find("slideEffects").GetComponent<Slider>().value;
} }
private void loadAudioSettings() public void setSlider()
{ {
//Need to work on GameObject.Find("slideEffects").GetComponent<Slider>().value = playerAudio.volume;
GameObject.Find("slideMusic").GetComponent<Slider>().value = cameraAudio.volume;
}
public void loadAudioSettings()
{
if (File.Exists(filepath))
{
string[] lines = File.ReadAllLines(filepath);
cameraAudio.volume = float.Parse(lines[0].Split(':')[1]);
playerAudio.volume = float.Parse(lines[1].Split(':')[1]);
}
else
{
StreamWriter sw = File.CreateText(filepath);
sw.WriteLine("Music:0.5");
sw.WriteLine("Effects:0.5");
sw.Flush();
}
}
public void saveAudioSettings()
{
float music = GameObject.Find("slideMusic").GetComponent<Slider>().value;
float effects = GameObject.Find("slideEffects").GetComponent<Slider>().value;
StreamWriter sw;
if (!File.Exists(filepath))
{
sw = File.CreateText(filepath);
}
else
{
sw = new StreamWriter(filepath);
}
sw.WriteLine("Music:"+ music);
sw.WriteLine("Effects:"+ effects);
sw.Flush();
} }
} }

View File

@ -42,6 +42,7 @@ namespace Assets.Scripts
public void closeOptions() public void closeOptions()
{ {
audioHandler.loadAudioSettings();
audioHandler.playButtonClick(); audioHandler.playButtonClick();
uihandler.closeOptions(); uihandler.closeOptions();
} }
@ -109,6 +110,7 @@ namespace Assets.Scripts
{ {
audioHandler.playButtonClick(); audioHandler.playButtonClick();
uihandler.adaptScreen(); uihandler.adaptScreen();
audioHandler.saveAudioSettings();
} }
public void closeTutorial() public void closeTutorial()

View File

@ -181,6 +181,7 @@ namespace Assets.Scripts
public void openOptions() public void openOptions()
{ {
options.SetActive(true); options.SetActive(true);
GameObject.Find("AudioHandler").GetComponent<AudioHandler>().setSlider();
hideOtherElements(options); hideOtherElements(options);
if (state == UIState.MAINMENU) if (state == UIState.MAINMENU)
{ {

2
audiosettings.txt Normal file
View File

@ -0,0 +1,2 @@
Music:0.07957498
Effects:1