-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIMain.cs
301 lines (244 loc) · 11 KB
/
UIMain.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* This file is part of the "Tanks Multiplayer" project by Rebound Games.
* You are only allowed to use these resources if you've bought them from the Unity Asset Store.
* You shall not license, sublicense, sell, resell, transfer, assign, distribute or
* otherwise make available to any third party the Service or the Content. */
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
namespace TanksMP
{
/// <summary>
/// UI script for all elements, settings and user interactions in the menu scene.
/// </summary>
public class UIMain : Photon.MonoBehaviour
{
/// <summary>
/// Window object for loading screen between connecting and scene switch.
/// </summary>
public GameObject loadingWindow;
/// <summary>
/// Window object for displaying errors with the connection or timeouts.
/// </summary>
public GameObject connectionErrorWindow;
/// <summary>
/// Window object for displaying errors with the billing actions.
/// </summary>
public GameObject billingErrorWindow;
/// <summary>
/// UI button for opening the Everyplay hub, hooked into inspector event.
/// This is automatically hidden on non-supported devices.
/// </summary>
public Button everyplayButton;
/// <summary>
/// Settings: input field for the player name.
/// </summary>
public InputField nameField;
/// <summary>
/// Settings: dropdown selection for network mode.
/// </summary>
public Dropdown networkDrop;
/// <summary>
/// Settings: input field for manual server address,
/// hosting a server in a private network (Photon only).
/// </summary>
public InputField serverField;
/// <summary>
/// Settings: checkbox for playing background music.
/// </summary>
public Toggle musicToggle;
/// <summary>
/// Settings: slider for adjusting game sound volume.
/// </summary>
public Slider volumeSlider;
/// <summary>
/// Settings: checkbox for recording gameplay videos.
/// This is automatically hidden on non-supported devices.
/// </summary>
public Toggle recordToggle;
//how many times the shop has been opened
//private int shopOpened = 0;
public Text MonneyText;
public int MoneyAmount;
//how many times the settings have been opened
//private int settingsOpened = 0;
//initialize player selection in Settings window
//if this is the first time launching the game, set initial values
void Start()
{
//set initial values for all settings
if (!PlayerPrefs.HasKey(PrefsKeys.playerName)) PlayerPrefs.SetString(PrefsKeys.playerName, "User" + System.String.Format("{0:0000}", Random.Range(1, 9999)));
if (!PlayerPrefs.HasKey(PrefsKeys.networkMode)) PlayerPrefs.SetInt(PrefsKeys.networkMode, 2);
if (!PlayerPrefs.HasKey(PrefsKeys.serverAddress)) PlayerPrefs.SetString(PrefsKeys.serverAddress, "127.0.0.1");
if (!PlayerPrefs.HasKey(PrefsKeys.playMusic)) PlayerPrefs.SetString(PrefsKeys.playMusic, "true");
if (!PlayerPrefs.HasKey(PrefsKeys.appVolume)) PlayerPrefs.SetFloat(PrefsKeys.appVolume, 1f);
if (!PlayerPrefs.HasKey(PrefsKeys.recordGame)) PlayerPrefs.SetString(PrefsKeys.recordGame, "true");
if (!PlayerPrefs.HasKey(PrefsKeys.activeTank)) PlayerPrefs.SetString(PrefsKeys.activeTank, Encryptor.Encrypt("0"));
if (!PlayerPrefs.HasKey(PrefsKeys.activeBullet)) PlayerPrefs.SetString(PrefsKeys.activeBullet, Encryptor.Encrypt("0"));
if (!PlayerPrefs.HasKey(PrefsKeys.Money)) PlayerPrefs.SetInt(PrefsKeys.Money, 9999);
PlayerPrefs.Save();
//read the selections and set them in the corresponding UI elements
nameField.text = PlayerPrefs.GetString(PrefsKeys.playerName);
networkDrop.value = PlayerPrefs.GetInt(PrefsKeys.networkMode);
serverField.text = PlayerPrefs.GetString(PrefsKeys.serverAddress);
musicToggle.isOn = bool.Parse(PlayerPrefs.GetString(PrefsKeys.playMusic));
volumeSlider.value = PlayerPrefs.GetFloat(PrefsKeys.appVolume);
recordToggle.isOn = bool.Parse(PlayerPrefs.GetString(PrefsKeys.recordGame));
MoneyAmount = PlayerPrefs.GetInt(PrefsKeys.Money);
MonneyText.text = MoneyAmount.ToString();
//call the onValueChanged callbacks once with their saved values
OnMusicChanged(musicToggle.isOn);
OnVolumeChanged(volumeSlider.value);
//listen to network connection and IAP billing errors
NetworkManagerCustom.connectionFailedEvent += OnConnectionError;
UnityIAPManager.purchaseFailedEvent += OnBillingError;
//add Everyplay callback in case it changes later on,
//but also call it immediately right when its initialization finishes
UnityEveryplayManager.recordingSupportedEvent += OnEveryplaySupport;
OnEveryplaySupport(UnityEveryplayManager.IsSupported());
}
void Update()
{
MonneyText.text = MoneyAmount.ToString();
}
/// <summary>
/// Tries to enter the game scene. Sets the loading screen active while connecting to the
/// Matchmaker and starts the timeout coroutine at the same time.
/// </summary>
public void Play()
{
//UnityAnalyticsManager.MainSceneClosed(shopOpened, settingsOpened, musicToggle.isOn,
// Encryptor.Decrypt(PlayerPrefs.GetString(PrefsKeys.activeTank)));
loadingWindow.SetActive(true);
/*
int networkModeInt = PlayerPrefs.GetInt("TM_networkMode");
NetworkMode networkMode = (NetworkMode)networkModeInt;
NetworkManagerCustom.StartMatch(networkMode);
*/
NetworkManagerCustom.StartMatch((NetworkMode)PlayerPrefs.GetInt(PrefsKeys.networkMode));
StartCoroutine(HandleTimeout());
}
//coroutine that waits 10 seconds before cancelling joining a match
IEnumerator HandleTimeout()
{
yield return new WaitForSeconds(10);
//timeout has passed, we would like to stop joining a game now
PhotonNetwork.Disconnect();
//display connection issue window
OnConnectionError();
}
//activates the connection error window to be visible
void OnConnectionError()
{
StopCoroutine(HandleTimeout());
loadingWindow.SetActive(false);
connectionErrorWindow.SetActive(true);
}
//activates the billing error window to be visible
void OnBillingError(string error)
{
//get text label to display billing failed reason
Text errorLabel = billingErrorWindow.GetComponentInChildren<Text>();
if (errorLabel)
errorLabel.text = "Purchase failed.\n" + error;
billingErrorWindow.SetActive(true);
}
/// <summary>
/// Increase counter when opening the shop.
/// Used for Unity Analytics purposes.
/// </summary>
public void OpenShop()
{
//shopOpened++;
}
/// <summary>
/// Increase counter when opening settings.
/// Used for Unity Analytics purposes.
/// </summary>
public void OpenSettings()
{
//settingsOpened++;
}
/// <summary>
/// Allow additional input of server address only in network mode LAN.
/// Otherwise, the input field will be hidden in the settings (Photon only).
/// </summary>
public void OnNetworkChanged(int value)
{
serverField.gameObject.SetActive((NetworkMode)value == NetworkMode.LAN ? true : false);
}
/// <summary>
/// Modify music AudioSource based on player selection.
/// Called by Toggle onValueChanged event.
/// </summary>
public void OnMusicChanged(bool value)
{
AudioManager.GetInstance().musicSource.enabled = musicToggle.isOn;
AudioManager.PlayMusic(0);
}
/// <summary>
/// Modify global game volume based on player selection.
/// Called by Slider onValueChanged event.
/// </summary>
public void OnVolumeChanged(float value)
{
volumeSlider.value = value;
AudioListener.volume = value;
}
/// <summary>
/// Saves all player selections chosen in the Settings window on the device.
/// </summary>
public void CloseSettings()
{
PlayerPrefs.SetString(PrefsKeys.playerName, nameField.text);
PlayerPrefs.SetInt(PrefsKeys.networkMode, networkDrop.value);
PlayerPrefs.SetString(PrefsKeys.serverAddress, serverField.text);
PlayerPrefs.SetString(PrefsKeys.playMusic, musicToggle.isOn.ToString());
PlayerPrefs.SetFloat(PrefsKeys.appVolume, volumeSlider.value);
PlayerPrefs.SetString(PrefsKeys.recordGame, recordToggle.isOn.ToString());
PlayerPrefs.SetInt(PrefsKeys.Money, MoneyAmount);
PlayerPrefs.Save();
}
/// <summary>
/// Sets the visibility of Everyplay related UI elements.
/// Called by Everplay when initialization finishes.
/// </summary>
public void OnEveryplaySupport(bool supported)
{
everyplayButton.gameObject.SetActive(supported);
recordToggle.gameObject.SetActive(supported);
}
/// <summary>
/// Displays the Everyplay community hub using our manager.
/// </summary>
public void ShowEveryplay()
{
UnityEveryplayManager.ShowMenu();
}
/// <summary>
/// Opens a browser window to the App Store entry for this app.
/// </summary>
public void RateApp()
{
//UnityAnalyticsManager.RateStart();
//default app url on non-mobile platforms
//replace with your website, for example
string url = "";
#if UNITY_ANDROID
url = "http://play.google.com/store/apps/details?id=" + Application.identifier;
#elif UNITY_IPHONE
url = "https://itunes.apple.com/app/idXXXXXXXXX";
#endif
if(string.IsNullOrEmpty(url) || url.EndsWith("XXXXXX"))
{
Debug.LogWarning("UIMain: You didn't replace your app links!");
return;
}
Application.OpenURL(url);
}
public void ExitGame()
{
Application.Quit();
}
}
}