-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunused-UIHandler.cs
316 lines (278 loc) · 10.8 KB
/
unused-UIHandler.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
// Handler for UI buttons on the scene. Also performs some
// necessary setup (initializing the firebase app, etc) on
// startup.
public class UIHandler : MonoBehaviour
{
ArrayList leaderBoard;
Vector2 scrollPosition = Vector2.zero;
private Vector2 controlsScrollViewVector = Vector2.zero;
public GUISkin fb_GUISkin;
private const int MaxScores = 5;
private string logText = "";
private string email = "";
private int score = 100;
private Vector2 scrollViewVector = Vector2.zero;
bool UIEnabled = true;
const int kMaxLogSize = 16382;
DependencyStatus dependencyStatus = DependencyStatus.UnavailableOther;
// When the app starts, check to make sure that we have
// the required dependencies to use Firebase, and if not,
// add them if possible.
void Start()
{
dependencyStatus = FirebaseApp.CheckDependencies();
if (dependencyStatus != DependencyStatus.Available)
{
FirebaseApp.FixDependenciesAsync().ContinueWith(task =>
{
dependencyStatus = FirebaseApp.CheckDependencies();
if (dependencyStatus == DependencyStatus.Available)
{
InitializeFirebase();
}
else
{
Debug.LogError(
"Could not resolve all Firebase dependencies: " + dependencyStatus);
}
});
}
else
{
InitializeFirebase();
}
}
// Initialize the Firebase database:
void InitializeFirebase()
{
FirebaseApp app = FirebaseApp.DefaultInstance;
// NOTE: You'll need to replace this url with your Firebase App's database
// path in order for the database connection to work correctly in editor.
app.SetEditorDatabaseUrl("https://fir-db-test-f633c.firebaseio.com");
if (app.Options.DatabaseUrl != null) app.SetEditorDatabaseUrl(app.Options.DatabaseUrl);
leaderBoard = new ArrayList();
leaderBoard.Add("Firebase Top " + MaxScores.ToString() + " Scores");
FirebaseDatabase.DefaultInstance
.GetReference("Leaders").OrderByChild("score")
.ValueChanged += (object sender2, ValueChangedEventArgs e2) =>
{
if (e2.DatabaseError != null)
{
Debug.LogError(e2.DatabaseError.Message);
return;
}
string title = leaderBoard[0].ToString();
leaderBoard.Clear();
leaderBoard.Add(title);
if (e2.Snapshot != null && e2.Snapshot.ChildrenCount > 0)
{
foreach (var childSnapshot in e2.Snapshot.Children)
{
if (childSnapshot.Child("score") == null
|| childSnapshot.Child("score").Value == null)
{
Debug.LogError("Bad data in sample. Did you forget to call SetEditorDatabaseUrl with your project id?");
break;
}
else
{
leaderBoard.Insert(1, childSnapshot.Child("score").Value.ToString()
+ " " + childSnapshot.Child("email").Value.ToString());
}
}
}
};
}
// Output text to the debug log text field, as well as the console.
public void DebugLog(string s)
{
Debug.Log(s);
logText += s + "\n";
while (logText.Length > kMaxLogSize)
{
int index = logText.IndexOf("\n");
logText = logText.Substring(index + 1);
}
scrollViewVector.y = int.MaxValue;
}
// A realtime database transaction receives MutableData which can be modified
// and returns a TransactionResult which is either TransactionResult.Success(data) with
// modified data or TransactionResult.Abort() which stops the transaction with no changes.
TransactionResult AddScoreTransaction(MutableData mutableData)
{
List<object> leaders = mutableData.Value as List<object>;
if (leaders == null)
{
leaders = new List<object>();
}
else if (mutableData.ChildrenCount >= MaxScores)
{
// If the current list of scores is greater or equal to our maximum allowed number,
// we see if the new score should be added and remove the lowest existing score.
long minScore = long.MaxValue;
object minVal = null;
foreach (var child in leaders)
{
if (!(child is Dictionary<string, object>))
continue;
long childScore = (long)((Dictionary<string, object>)child)["score"];
if (childScore < minScore)
{
minScore = childScore;
minVal = child;
}
}
// If the new score is lower than the current minimum, we abort.
if (minScore > score)
{
return TransactionResult.Abort();
}
// Otherwise, we remove the current lowest to be replaced with the new score.
leaders.Remove(minVal);
}
// Now we add the new score as a new entry that contains the email address and score.
Dictionary<string, object> newScoreMap = new Dictionary<string, object>();
newScoreMap["score"] = score;
newScoreMap["email"] = email;
leaders.Add(newScoreMap);
// You must set the Value to indicate data at that location has changed.
mutableData.Value = leaders;
return TransactionResult.Success(mutableData);
}
public void AddScore()
{
if (score == 0 || string.IsNullOrEmpty(email))
{
DebugLog("invalid score or email.");
return;
}
DebugLog(String.Format("Attempting to add score {0} {1}",
email, score.ToString()));
DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("Leaders");
DebugLog("Running Transaction...");
// Use a transaction to ensure that we do not encounter issues with
// simultaneous updates that otherwise might create more than MaxScores top scores.
reference.RunTransaction(AddScoreTransaction)
.ContinueWith(task =>
{
if (task.Exception != null)
{
DebugLog(task.Exception.ToString());
}
else if (task.IsCompleted)
{
DebugLog("Transaction complete.");
}
});
}
// Render the log output in a scroll view.
void GUIDisplayLog()
{
scrollViewVector = GUILayout.BeginScrollView(scrollViewVector);
GUILayout.Label(logText);
GUILayout.EndScrollView();
}
// Render the buttons and other controls.
void GUIDisplayControls()
{
if (UIEnabled)
{
controlsScrollViewVector =
GUILayout.BeginScrollView(controlsScrollViewVector);
GUILayout.BeginVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("Email:", GUILayout.Width(Screen.width * 0.20f));
email = GUILayout.TextField(email);
GUILayout.EndHorizontal();
GUILayout.Space(20);
GUILayout.BeginHorizontal();
GUILayout.Label("Score:", GUILayout.Width(Screen.width * 0.20f));
int.TryParse(GUILayout.TextField(score.ToString()), out score);
GUILayout.EndHorizontal();
GUILayout.Space(20);
if (!String.IsNullOrEmpty(email) && GUILayout.Button("Enter Score"))
{
AddScore();
}
GUILayout.Space(20);
if (GUILayout.Button("Go Offline"))
{
FirebaseDatabase.DefaultInstance.GoOffline();
}
GUILayout.Space(20);
if (GUILayout.Button("Go Online"))
{
FirebaseDatabase.DefaultInstance.GoOnline();
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
}
}
void GUIDisplayLeaders()
{
GUI.skin.box.fontSize = 36;
scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, true);
GUILayout.BeginVertical(GUI.skin.box);
foreach (string item in leaderBoard)
{
GUILayout.Label(item, GUI.skin.box, GUILayout.ExpandWidth(true));
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
}
// Render the GUI:
void OnGUI()
{
GUI.skin = fb_GUISkin;
if (dependencyStatus != DependencyStatus.Available)
{
GUILayout.Label("One or more Firebase dependencies are not present.");
GUILayout.Label("Current dependency status: " + dependencyStatus.ToString());
return;
}
Rect logArea, controlArea, leaderBoardArea;
if (Screen.width < Screen.height)
{
// Portrait mode
controlArea = new Rect(0.0f, 0.0f, Screen.width, Screen.height * 0.5f);
leaderBoardArea = new Rect(0, Screen.height * 0.5f, Screen.width * 0.5f, Screen.height * 0.5f);
logArea = new Rect(Screen.width * 0.5f, Screen.height * 0.5f, Screen.width * 0.5f, Screen.height * 0.5f);
}
else
{
// Landscape mode
controlArea = new Rect(0.0f, 0.0f, Screen.width * 0.5f, Screen.height);
leaderBoardArea = new Rect(Screen.width * 0.5f, 0, Screen.width * 0.5f, Screen.height * 0.5f);
logArea = new Rect(Screen.width * 0.5f, Screen.height * 0.5f, Screen.width * 0.5f, Screen.height * 0.5f);
}
GUILayout.BeginArea(leaderBoardArea);
GUIDisplayLeaders();
GUILayout.EndArea();
GUILayout.BeginArea(logArea);
GUIDisplayLog();
GUILayout.EndArea();
GUILayout.BeginArea(controlArea);
GUIDisplayControls();
GUILayout.EndArea();
}
}