Skip to content

Commit 2840b94

Browse files
committed
feat: passport login ui prefab
1 parent c005c8c commit 2840b94

File tree

9 files changed

+4161
-0
lines changed

9 files changed

+4161
-0
lines changed

PassportPanel.png

3.02 KB
Loading

src/Packages/Passport/Samples~/PassportManagerPrefab/Prefabs/PassportLoginUI.prefab

Lines changed: 3637 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using UnityEngine;
2+
using UnityEngine.UI;
3+
using Immutable.Passport.Model;
4+
5+
namespace Immutable.Passport.UI
6+
{
7+
/// <summary>
8+
/// Handles email submission for Passport login.
9+
/// Connects the email input field to the submit button and PassportManager.
10+
/// </summary>
11+
public class EmailSubmitHandler : MonoBehaviour
12+
{
13+
[Header("UI References")]
14+
[Tooltip("The email input field to get the email from")]
15+
public InputField emailInputField;
16+
17+
private Button submitButton;
18+
private PassportManager passportManager;
19+
private Toggle marketingConsentToggle;
20+
21+
private void Start()
22+
{
23+
submitButton = GetComponent<Button>();
24+
passportManager = GetComponentInParent<PassportManager>();
25+
26+
if (submitButton == null)
27+
{
28+
Debug.LogError("[EmailSubmitHandler] No Button component found!");
29+
return;
30+
}
31+
32+
if (passportManager == null)
33+
{
34+
Debug.LogError("[EmailSubmitHandler] No PassportManager found in parent!");
35+
return;
36+
}
37+
38+
// Find marketing consent toggle
39+
marketingConsentToggle = FindMarketingConsentToggle();
40+
41+
// Wire up the submit button
42+
submitButton.onClick.AddListener(OnSubmitClicked);
43+
44+
// Also allow Enter key to submit
45+
if (emailInputField != null)
46+
{
47+
emailInputField.onEndEdit.AddListener(OnEmailEndEdit);
48+
}
49+
50+
Debug.Log("[EmailSubmitHandler] Email submit handler initialized");
51+
}
52+
53+
private void OnSubmitClicked()
54+
{
55+
SubmitEmail();
56+
}
57+
58+
private void OnEmailEndEdit(string email)
59+
{
60+
// Submit when user presses Enter
61+
if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
62+
{
63+
SubmitEmail();
64+
}
65+
}
66+
67+
private void SubmitEmail()
68+
{
69+
if (emailInputField == null || passportManager == null)
70+
{
71+
Debug.LogError("[EmailSubmitHandler] Missing required components!");
72+
return;
73+
}
74+
75+
string email = emailInputField.text.Trim();
76+
77+
if (string.IsNullOrEmpty(email))
78+
{
79+
Debug.LogWarning("[EmailSubmitHandler] Email field is empty!");
80+
return;
81+
}
82+
83+
// Basic email validation
84+
if (!IsValidEmail(email))
85+
{
86+
Debug.LogWarning($"[EmailSubmitHandler] Invalid email format: {email}");
87+
return;
88+
}
89+
90+
// Get marketing consent status
91+
MarketingConsentStatus consentStatus = MarketingConsentStatus.Unsubscribed;
92+
if (marketingConsentToggle != null)
93+
{
94+
// Note: The toggle text says "I don't want to receive..." so we invert the logic
95+
consentStatus = marketingConsentToggle.isOn ? MarketingConsentStatus.Unsubscribed : MarketingConsentStatus.OptedIn;
96+
}
97+
98+
Debug.Log($"[EmailSubmitHandler] Submitting email login - Email: {email}, Consent: {consentStatus}");
99+
100+
// Create login options with email and consent
101+
DirectLoginOptions loginOptions = new DirectLoginOptions
102+
{
103+
directLoginMethod = DirectLoginMethod.Email, // Default email login
104+
email = email,
105+
marketingConsentStatus = consentStatus
106+
};
107+
108+
// Trigger login via PassportManager
109+
passportManager.Login(loginOptions);
110+
}
111+
112+
private Toggle FindMarketingConsentToggle()
113+
{
114+
// Look for marketing consent toggle in the UI hierarchy
115+
Transform root = transform.root;
116+
Transform consentContainer = FindChildRecursive(root, "MarketingConsentContainer");
117+
118+
if (consentContainer != null)
119+
{
120+
Toggle toggle = consentContainer.GetComponentInChildren<Toggle>();
121+
if (toggle != null)
122+
{
123+
Debug.Log("[EmailSubmitHandler] Found marketing consent toggle");
124+
return toggle;
125+
}
126+
}
127+
128+
Debug.LogWarning("[EmailSubmitHandler] Marketing consent toggle not found");
129+
return null;
130+
}
131+
132+
private Transform FindChildRecursive(Transform parent, string name)
133+
{
134+
foreach (Transform child in parent)
135+
{
136+
if (child.name == name)
137+
return child;
138+
139+
Transform found = FindChildRecursive(child, name);
140+
if (found != null)
141+
return found;
142+
}
143+
return null;
144+
}
145+
146+
private bool IsValidEmail(string email)
147+
{
148+
try
149+
{
150+
var addr = new System.Net.Mail.MailAddress(email);
151+
return addr.Address == email;
152+
}
153+
catch
154+
{
155+
return false;
156+
}
157+
}
158+
}
159+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using UnityEngine;
2+
3+
namespace Immutable.Passport.UI
4+
{
5+
/// <summary>
6+
/// Manages cursor lock state when the Passport UI is active.
7+
/// Automatically unlocks the cursor for UI interaction and restores the previous state when done.
8+
/// </summary>
9+
public class PassportUICursorManager : MonoBehaviour
10+
{
11+
[Header("Cursor Management Settings")]
12+
[Tooltip("Enable automatic cursor unlock when UI is active")]
13+
public bool enableCursorManagement = true;
14+
15+
[Tooltip("Force cursor to stay unlocked while UI is active (overrides game cursor locks)")]
16+
public bool forceCursorUnlock = true;
17+
18+
private CursorLockMode previousCursorLockMode;
19+
private bool previousCursorVisible;
20+
private Canvas canvas;
21+
private bool hasStoredState = false;
22+
23+
private void Start()
24+
{
25+
if (!enableCursorManagement) return;
26+
27+
canvas = GetComponentInChildren<Canvas>();
28+
if (canvas != null)
29+
{
30+
StoreCursorState();
31+
SetCursorForUI(true);
32+
33+
Debug.Log($"[PassportUICursorManager] Initialized - Lock: {previousCursorLockMode}, Visible: {previousCursorVisible}");
34+
}
35+
}
36+
37+
private void Update()
38+
{
39+
if (!enableCursorManagement || !forceCursorUnlock) return;
40+
41+
// Ensure cursor stays unlocked while UI is active
42+
if (canvas != null && canvas.gameObject.activeInHierarchy)
43+
{
44+
if (Cursor.lockState != CursorLockMode.None)
45+
{
46+
SetCursorForUI(true);
47+
}
48+
}
49+
}
50+
51+
private void OnDestroy()
52+
{
53+
if (enableCursorManagement && hasStoredState)
54+
{
55+
RestoreCursorState();
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Call this when hiding/closing the UI
61+
/// </summary>
62+
public void OnUIHidden()
63+
{
64+
if (enableCursorManagement && hasStoredState)
65+
{
66+
RestoreCursorState();
67+
}
68+
}
69+
70+
/// <summary>
71+
/// Call this when showing the UI
72+
/// </summary>
73+
public void OnUIShown()
74+
{
75+
if (enableCursorManagement)
76+
{
77+
if (!hasStoredState)
78+
{
79+
StoreCursorState();
80+
}
81+
SetCursorForUI(true);
82+
}
83+
}
84+
85+
/// <summary>
86+
/// Enable or disable cursor management at runtime
87+
/// </summary>
88+
public void SetCursorManagementEnabled(bool enabled)
89+
{
90+
enableCursorManagement = enabled;
91+
92+
if (!enabled && hasStoredState)
93+
{
94+
RestoreCursorState();
95+
}
96+
else if (enabled && canvas != null && canvas.gameObject.activeInHierarchy)
97+
{
98+
if (!hasStoredState)
99+
{
100+
StoreCursorState();
101+
}
102+
SetCursorForUI(true);
103+
}
104+
}
105+
106+
private void StoreCursorState()
107+
{
108+
previousCursorLockMode = Cursor.lockState;
109+
previousCursorVisible = Cursor.visible;
110+
hasStoredState = true;
111+
}
112+
113+
private void SetCursorForUI(bool enableForUI)
114+
{
115+
if (enableForUI)
116+
{
117+
Cursor.lockState = CursorLockMode.None;
118+
Cursor.visible = true;
119+
Debug.Log("[PassportUICursorManager] Cursor unlocked for UI interaction");
120+
}
121+
}
122+
123+
private void RestoreCursorState()
124+
{
125+
if (hasStoredState)
126+
{
127+
Cursor.lockState = previousCursorLockMode;
128+
Cursor.visible = previousCursorVisible;
129+
hasStoredState = false;
130+
Debug.Log($"[PassportUICursorManager] Restored cursor state - Lock: {previousCursorLockMode}, Visible: {previousCursorVisible}");
131+
}
132+
}
133+
134+
/// <summary>
135+
/// Force unlock cursor (useful for debugging)
136+
/// </summary>
137+
[System.Diagnostics.Conditional("UNITY_EDITOR")]
138+
public void ForceUnlockCursor()
139+
{
140+
SetCursorForUI(true);
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)