-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMainMenu.cs
134 lines (117 loc) · 4.05 KB
/
MainMenu.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
using System;
using System.Collections.Generic;
using HyperCasual.Core;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Immutable.Passport;
namespace HyperCasual.Runner
{
/// <summary>
/// This View contains main menu functionalities
/// </summary>
public class MainMenu : View
{
[SerializeField]
HyperCasualButton m_StartButton;
[SerializeField]
AbstractGameEvent m_StartButtonEvent;
[SerializeField]
TextMeshProUGUI m_Email;
[SerializeField]
HyperCasualButton m_LogoutButton;
[SerializeField]
GameObject m_Loading;
Passport passport;
async void OnEnable()
{
ShowLoading(true);
// Set listener to 'Start' button
m_StartButton.RemoveListener(OnStartButtonClick);
m_StartButton.AddListener(OnStartButtonClick);
// Set listener to 'Logout' button
m_LogoutButton.RemoveListener(OnLogoutButtonClick);
m_LogoutButton.AddListener(OnLogoutButtonClick);
// Initialise Passport
string clientId = "Jf3dcKYrYUC6MLCMsDsOvKGoirlAzGJR";
string environment = Immutable.Passport.Model.Environment.SANDBOX;
string redirectUri = null;
string logoutUri = null;
passport = await Passport.Init(clientId, environment, redirectUri, logoutUri);
// Check if the player is supposed to be logged in and if there are credentials saved
if (SaveManager.Instance.IsLoggedIn && await Passport.Instance.HasCredentialsSaved())
{
// Try to log in using saved credentials
bool success = await Passport.Instance.Login(useCachedSession: true);
// Update the login flag
SaveManager.Instance.IsLoggedIn = success;
// Set up wallet if successful
if (success)
{
await Passport.Instance.ConnectEvm();
await Passport.Instance.ZkEvmRequestAccounts();
}
}
else
{
// No saved credentials to re-login the player, reset the login flag
SaveManager.Instance.IsLoggedIn = false;
}
ShowLoading(false);
// Show the logout button if the player is logged in
ShowLogoutButton(SaveManager.Instance.IsLoggedIn);
}
void OnDisable()
{
m_StartButton.RemoveListener(OnStartButtonClick);
}
void OnStartButtonClick()
{
m_StartButtonEvent.Raise();
AudioManager.Instance.PlayEffect(SoundID.ButtonSound);
}
async void OnLogoutButtonClick()
{
try
{
// Hide the 'Logout' button
ShowLogoutButton(false);
// Show loading
ShowLoading(true);
// Logout
await passport.Logout();
// Reset the login flag
SaveManager.Instance.IsLoggedIn = false;
// Successfully logged out, hide 'Logout' button
ShowLogoutButton(false);
// Reset all other values
SaveManager.Instance.Clear();
}
catch (Exception ex)
{
Debug.Log($"Failed to log out: {ex.Message}");
// Failed to logout so show 'Logout' button again
ShowLogoutButton(true);
}
// Hide loading
ShowLoading(false);
}
void ShowLoading(bool show)
{
m_Loading.gameObject.SetActive(show);
ShowStartButton(!show);
}
void ShowStartButton(bool show)
{
m_StartButton.gameObject.SetActive(show);
}
void ShowLogoutButton(bool show)
{
m_LogoutButton.gameObject.SetActive(show);
}
void ShowEmail(bool show)
{
m_Email.gameObject.SetActive(show);
}
}
}