-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundManager1.cs
70 lines (61 loc) · 2.26 KB
/
SoundManager1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; //MUSIC
public class SoundManager1 : MonoBehaviour
{
[SerializeField] Image soundOnIcon; //I'm using serialized fields to constantly update sound on/off icons
[SerializeField] Image soundOffIcon; //I'm using serialized fields to constantly update sound on/off icons
private bool muted1 = false;
private void Start()
{
Load(); //I'm loading the mute state from PlayerPrefs
UpdateButtonIcon();
//I'm finding/creating (if I cant find anything) the BackgroundMusic object
BackgroundMusic backgroundMusic = FindObjectOfType<BackgroundMusic>();
if (backgroundMusic == null)
{
//I'm creeating a new GameObject with BackgroundMusic component
GameObject backgroundMusicObject = new GameObject("BackgroundMusic");
backgroundMusic = backgroundMusicObject.AddComponent<BackgroundMusic>();
DontDestroyOnLoad(backgroundMusicObject);
}
//I'm setting the mute state of the background music
backgroundMusic.ToggleMute(muted1);
}
public void OnButtonPress()
{
muted1 = !muted1; //I'm toggling the mute state
Save(); //I'm saving the mute state to PlayerPrefs
UpdateButtonIcon();
//I'm finding/creating (if I cant find anything) the BackgroundMusic object
BackgroundMusic backgroundMusic = FindObjectOfType<BackgroundMusic>();
if (backgroundMusic != null)
{
//I'm toggling the mute state of the background music
backgroundMusic.ToggleMute(muted1);
}
}
private void UpdateButtonIcon()
{
if (muted1 == false)
{
soundOnIcon.enabled = true;
soundOffIcon.enabled = false;
}
else
{
soundOnIcon.enabled = false;
soundOffIcon.enabled = true;
}
}
private void Load()
{
muted1 = PlayerPrefs.GetInt("muted1") == 1;
}
private void Save()
{
PlayerPrefs.SetInt("muted1", muted1 ? 1 : 0);
PlayerPrefs.Save(); //I'm saving the PlayerPrefs data immediately
}
}