-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIManager.cs
179 lines (153 loc) · 4.61 KB
/
UIManager.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;
public class UIManager : MonoBehaviour
{
private static UIManager uiManager;
public static UIManager instance
{
get
{
if (!uiManager)
{
uiManager = FindObjectOfType(typeof(UIManager)) as UIManager;
if (!uiManager)
Debug.LogError("Needs 1 active UIManager script on a GO in your scene!");
}
return uiManager;
}
}
private GameObject canvas;
[SerializeField]
private GameObject beginButton;
[SerializeField]
private GameObject optionsButton;
[SerializeField]
private GameObject creditsButton;
[SerializeField]
private GameObject quitButton;
[SerializeField]
private GameObject howToPlayButton;
[SerializeField]
private GameObject logo;
[SerializeField]
private GameObject optionsPanel;
[SerializeField]
private GameObject creditsPanel;
[SerializeField]
private AudioClip menuMusic;
[SerializeField]
private AudioClip gameMusic;
private Scene activeScene;
private AudioSource music;
private Slider musicVolumeSlider;
private AudioSource effects;
private Slider effectsVolumeSlider;
[SerializeField]
private GameObject howToPlayPanel;
private float musicVol;
private float effectsVol;
//Begin button pressed
public void BeginClick()
{
SceneManager.LoadScene(1);
}
private void ToggleMenu()
{
logo.SetActive(!logo.activeInHierarchy);
beginButton.SetActive(!beginButton.activeInHierarchy);
optionsButton.SetActive(!optionsButton.activeInHierarchy);
creditsButton.SetActive(!creditsButton.activeInHierarchy);
quitButton.SetActive(!quitButton.activeInHierarchy);
howToPlayButton.SetActive(!howToPlayButton.activeInHierarchy);
}
public void ToggleOptionsPanel()
{
if(activeScene.buildIndex == 0)
{
ToggleMenu();
optionsPanel.SetActive(!optionsPanel.activeInHierarchy);
}
else
{
if (Time.timeScale == 0.0f)
Time.timeScale = 1.0f;
else
Time.timeScale = 0.0f;
}
}
public void ToggleHowToPlayPanel()
{
ToggleMenu();
howToPlayPanel.SetActive(!howToPlayPanel.activeInHierarchy);
}
public void ToggleCredits()
{
ToggleMenu();
creditsPanel.SetActive(!creditsPanel.activeInHierarchy);
}
public void QuitClick()
{
Application.Quit();
}
//Player pressed play button,
//show reference image, then begin countdown.
public void Play()
{
EventManager.TriggerEvent("GameStart");
}
public void OnSceneChange(Scene scene, LoadSceneMode mode)
{
activeScene = scene;
if (scene.buildIndex == 1)
{
effects = Camera.main.GetComponent<AudioSource>();
effects.volume = effectsVol;
music.clip = gameMusic;
music.Play();
Play();
}
else
music.clip = menuMusic;
}
public void CloseHowTo()
{
howToPlayPanel = GameObject.FindGameObjectWithTag("h2p");
howToPlayPanel.SetActive(false);
}
public void OnMusicVolumeChange()
{
float newVol = musicVolumeSlider.value;
music.volume = newVol;
PlayerPrefs.SetFloat("MusicVol", newVol);
}
public void OnEffectsVolumeChange()
{
float newVol = effectsVolumeSlider.value;
if (activeScene.buildIndex == 1)
effects.volume = newVol;
PlayerPrefs.SetFloat("EffectsVol", newVol);
}
private void Start()
{
canvas = GameObject.FindGameObjectWithTag("Canvas");
activeScene = SceneManager.GetActiveScene();
SceneManager.sceneLoaded += OnSceneChange;
music = GetComponent<AudioSource>();
musicVolumeSlider = optionsPanel.transform.GetChild(2).GetComponent<Slider>();
effectsVolumeSlider = optionsPanel.transform.GetChild(4).GetComponent<Slider>();
musicVol = PlayerPrefs.GetFloat("MusicVol", 0.5f);
musicVolumeSlider.value = musicVol;
music.volume = musicVol;
effectsVol = PlayerPrefs.GetFloat("EffectsVol", 0.5f);
effectsVolumeSlider.value = effectsVol;
if (uiManager == null)
uiManager = this;
else if (uiManager != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
}