-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathUILoginMenu.cs
More file actions
1402 lines (1227 loc) · 57 KB
/
Copy pathUILoginMenu.cs
File metadata and controls
1402 lines (1227 loc) · 57 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2026 Epic Games Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace PlayEveryWare.EpicOnlineServices.Samples
{
using Epic.OnlineServices;
using Epic.OnlineServices.Auth;
using Epic.OnlineServices.UI;
using Epic.OnlineServices.Ecom;
using Epic.OnlineServices.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
using PlayEveryWare.EpicOnlineServices;
public class UILoginMenu : MonoBehaviour
{
[Header("Authentication UI")]
public Text DemoTitle;
public Dropdown SceneSwitcherDropDown;
public Dropdown loginTypeDropdown;
public RectTransform idContainer;
public Text idText;
public UIConsoleInputField idInputField;
public Text tokenText;
public UIConsoleInputField tokenInputField;
public UITooltip tokenTooltip;
public RectTransform connectTypeContainer;
public Dropdown connectTypeDropdown;
public Text loginButtonText;
private string _OriginalloginButtonText;
public Button loginButton;
private Coroutine PreventLogIn = null;
public Button logoutButton;
public Button removePersistentTokenButton;
public UnityEvent OnLogin;
public UnityEvent OnLogout;
/// <summary>
/// This field contains information about the scenes that the user can select from.
/// </summary>
[Header("Scene Information")]
public SceneData SceneInformation;
[Header("Controller")]
public GameObject UIFirstSelected;
public GameObject UIFindSelectable;
private GameObject selectedGameObject;
//use to indicate Connect login instead of Auth
private const LoginCredentialType connect = (LoginCredentialType)(-1);
private LoginCredentialType loginType = LoginCredentialType.Developer;
//default to invalid value
private const ExternalCredentialType invalidConnectType = (ExternalCredentialType)(-1);
private ExternalCredentialType connectType = invalidConnectType;
Apple.EOSSignInWithAppleManager signInWithAppleManager = null;
// Retain Id/Token inputs across scenes
public static string IdGlobalCache = string.Empty;
public static string TokenGlobalCache = string.Empty;
private void Awake()
{
idInputField.InputField.onEndEdit.AddListener(CacheIdInputField);
tokenInputField.InputField.onEndEdit.AddListener(CacheTokenField);
#if UNITY_EDITOR
loginType = LoginCredentialType.AccountPortal; // Default in editor
#elif UNITY_SWITCH || UNITY_SWITCH2
loginType = LoginCredentialType.PersistentAuth; // Default on switch
#elif UNITY_PS4 || UNITY_PS5 || UNITY_GAMECORE
loginType = LoginCredentialType.ExternalAuth; // Default on other consoles
#else
loginType = LoginCredentialType.AccountPortal; // Default on other platforms
#endif
// TODO: This will fail on anything that is mac, windows, or linux, or is an editor version of any of the above
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_STANDALONE_LINUX
idInputField.InputField.text = "localhost:7777"; //default on pc
#endif
#if !ENABLE_INPUT_SYSTEM && (UNITY_XBOXONE || UNITY_GAMECORE_XBOXONE || UNITY_GAMECORE_SCARLETT || UNITY_PS4 || UNITY_PS5 || UNITY_SWITCH || UNITY_SWITCH2)
Debug.LogError("Input currently handled by Input Manager. Input System Package is required for controller support on consoles.");
#endif
}
private void CacheIdInputField(string value)
{
IdGlobalCache = value;
}
private void CacheTokenField(string value)
{
TokenGlobalCache = value;
}
public void OnLoginTypeChanged(int value)
{
switch (value)
{
case 1:
loginType = LoginCredentialType.AccountPortal;
ConfigureUIForAccountPortalLogin();
break;
case 2:
loginType = LoginCredentialType.PersistentAuth;
ConfigureUIForPersistentLogin();
break;
case 3:
loginType = LoginCredentialType.ExternalAuth;
ConfigureUIForExternalAuth();
break;
case 4:
loginType = LoginCredentialType.ExchangeCode;
break;
case 5:
loginType = connect;
break;
case 0:
default:
loginType = LoginCredentialType.Developer;
ConfigureUIForDevAuthLogin();
break;
}
if (loginType == connect)
{
connectType = GetConnectType();
}
else
{
connectType = invalidConnectType;
}
ConfigureUIForLogin();
}
public void OnConnectDropdownChange()
{
if (loginType != connect)
{
return;
}
connectType = GetConnectType();
ConfigureUIForLogin();
}
private ExternalCredentialType GetConnectType()
{
string typeName = connectTypeDropdown.options[connectTypeDropdown.value].text;
if (Enum.TryParse(typeName, out ExternalCredentialType externalType))
{
return externalType;
}
else
{
return invalidConnectType;
}
}
public void Start()
{
_OriginalloginButtonText = loginButtonText.text;
InitConnectDropdown();
ConfigureUIForLogin();
// Populate the Scene dropdown.
SetupSceneDropdown();
}
private void SetupSceneDropdown()
{
// Get the currently active scene name.
string currentSceneName = SceneManager.GetActiveScene().name;
string currentSceneFriendlyName = string.Empty;
// To store the friendly names for the scenes.
List<string> sceneFriendlyNames = new();
// Loop through the scene information provided.
foreach (var sceneInfo in SceneInformation.scenes)
{
// TODO: Currently, checking to see if scene is a valid name does not work.
// It functions properly in the editor, but there is apparently some
// trickery that is unclear regarding getting scene names during runtime.
// This should be investigated further, but is fine for the time being.
// Check to make sure it's a valid scene. This should prevent
// the scene data asset from ever listing scenes that cannot be loaded.
//if (!SceneUtility.IsValidSceneName(sceneInfo.sceneName))
//{
// foreach(var name in SceneUtility.GetSceneNames())
// Debug.Log($"Scene name: \"{name}\".");
// throw new InvalidDataException($"Invalid scene name \"{sceneInfo.sceneName}\" provided in SceneData asset.");
//}
// Check to make sure that the scene actually exists
// look for the friendly name of the current active scene.
if (sceneInfo.sceneName == currentSceneName)
currentSceneFriendlyName = sceneInfo.friendlyName;
// populate the list of friendly names for the scene.
sceneFriendlyNames.Add(sceneInfo.friendlyName);
}
// Check to make sure that the friendly name for the scene was found.
if (string.IsNullOrEmpty(currentSceneFriendlyName))
{
throw new ArgumentException($"Cannot find friendly name for scene name \"{currentSceneName}\".");
}
// alphabetize the scene names.
sceneFriendlyNames.Sort();
// Add the sorted list of friendly names to the dropdown, clearing the options first.
SceneSwitcherDropDown.ClearOptions();
SceneSwitcherDropDown.AddOptions(sceneFriendlyNames);
// Determine which scene to indicate is currently loaded.
int currentSceneIndex = sceneFriendlyNames.IndexOf(currentSceneFriendlyName);
// Set that index to be the current value.
SceneSwitcherDropDown.value = currentSceneIndex;
// Listen for the value to change, and load the selected scene when it does.
SceneSwitcherDropDown.onValueChanged.AddListener(index =>
{
string friendlySceneNameSelected = SceneSwitcherDropDown.options[index]?.text;
var sceneToLoad = SceneInformation.scenes
.Where(info => info.friendlyName == friendlySceneNameSelected)
.Select(info => info.sceneName)
.FirstOrDefault();
Debug.LogFormat("UILoginMenu (OnDemoSceneChanged): value = {0}", sceneToLoad);
SceneManager.LoadScene(sceneToLoad);
});
}
private void EnterPressedToLogin()
{
if (loginButton.IsActive())
{
OnLoginButtonClick();
}
}
/// <summary>
/// If no GameObject is selected, but one was selected before - then determines if the
/// de-selected GameObject should actually still be detected. If so, brings focus back
/// to the previously selected game object. If not, then the first selected GameObject
/// is focused. Broadly speaking, this prevents a focus state wherein no GameObject has
/// focus.
/// </summary>
/// <param name="previouslySelectedGameObject">Reference to the GameObject that had focus on the last update.</param>
private static void PreventDeselection(ref GameObject previouslySelectedGameObject)
{
// Prevent Deselection
if (EventSystem.current.currentSelectedGameObject != null && EventSystem.current.currentSelectedGameObject != previouslySelectedGameObject)
{
previouslySelectedGameObject = EventSystem.current.currentSelectedGameObject;
}
else if (EventSystem.current.currentSelectedGameObject == null || EventSystem.current.currentSelectedGameObject.activeInHierarchy == false)
{
// If there is no selected object, or if the currently selected object is not visible.
if (previouslySelectedGameObject == null || previouslySelectedGameObject.activeInHierarchy == false)
{
// Then set the currently selected object to be the first selected game object.
previouslySelectedGameObject = EventSystem.current.firstSelectedGameObject;
}
EventSystem.current.SetSelectedGameObject(previouslySelectedGameObject);
}
}
/// <summary>
/// Determines whether input should be passed to the scene, or if it should be skipped.
/// This is in-order to prevent input from being handled when the EOS overlay is active.
/// </summary>
/// <returns>True if input should be handled, false if not.</returns>
private static bool ShouldInputBeHandled()
{
// Event System isn't found, so main app cannot handle input
if (null == EventSystem.current)
{
Debug.Log("EventSystem is null");
return false;
}
// Main app handles input if overlay isn't open
bool shouldHandle = !EOSManager.Instance.IsOverlayOpenWithExclusiveInput();
#if ENABLE_INPUT_SYSTEM
EventSystem.current.currentInputModule.enabled = shouldHandle;
EventSystem.current.sendNavigationEvents = shouldHandle;
#else
// TODO: Clarify why this is only evaluated for PS4 and PS5
#if UNITY_PS4 || UNITY_PS5
EventSystem.current.sendNavigationEvents = shouldHandle;
#endif
#endif
#if ENABLE_DEBUG_INPUT
LogInputChanged(shouldHandle);
#endif
return shouldHandle;
}
#if ENABLE_DEBUG_INPUT
private static bool previousShouldHandle = false;
private static void LogInputChanged(bool shouldHandle)
{
if (previousShouldHandle != shouldHandle)
{
Debug.LogWarning($"Input {(shouldHandle ? "enabled" : "disabled")} for main app");
previousShouldHandle = shouldHandle;
}
}
#endif
/// <summary>
/// Determines whether a GameObject needs to be set as selected.
/// </summary>
/// <returns>Returns true if a GameObject needs to be set, false otherwise.</returns>
private static bool ShouldSetSelectedGameObject()
{
bool wasInputDetected = InputUtility.WasGamepadUsedLastFrame() || InputUtility.WasMouseUsedLastFrame();
// If there was no input, or if there is a currently selected game object that is active,
// then stop the process.
return (false == wasInputDetected ||
(null != EventSystem.current.currentSelectedGameObject &&
EventSystem.current.currentSelectedGameObject.activeInHierarchy));
}
/// <summary>
/// Determines which GameObject should be selected.
/// </summary>
/// <param name="firstGameObject">Reference to the GameObject that is considered to be the "first" in tab focus order.</param>
/// <param name="findSelectable">Reference to the GameObject that can be used to find other selectables if doing so is necessary.</param>
private static void SetSelectedGameObject(ref GameObject firstGameObject, ref GameObject findSelectable)
{
if (null != EventSystem.current.currentSelectedGameObject)
return;
var nextSelectable = FindObjectsOfType<Selectable>(false)
.FirstOrDefault(s => s.navigation.mode != Navigation.Mode.None);
if (null != nextSelectable)
{
EventSystem.current.SetSelectedGameObject(nextSelectable.gameObject);
}
}
private static void HandleTabInput()
{
// Stop handling if Tab is not pressed, or if there is no current event system.
if (!InputUtility.TabWasPressed() || null == EventSystem.current || null == EventSystem.current.currentSelectedGameObject)
{
return;
}
// Find the next selectable by selecting up or down based on whether the shift or shift equivalent is pressed.
Selectable next = (InputUtility.ShiftIsPressed())
? EventSystem.current.currentSelectedGameObject
.GetComponent<Selectable>().FindSelectableOnUp()
: EventSystem.current.currentSelectedGameObject
.GetComponent<Selectable>().FindSelectableOnDown();
// NOTE: Previously the following while loop was only executed when ENABLE_INPUT_SYSTEM is set.
// TODO: Confirm no regressions in functionality.
while (null != next && !next.gameObject.activeSelf)
{
next = InputUtility.ShiftIsPressed() ? next.FindSelectableOnDown() : next.FindSelectableOnUp();
}
if (next != null)
{
// If the "next" control getting focus has an input field component.
if (next.TryGetComponent<InputField>(out var inputField))
{
// Then simulate a pointer click on that component.
inputField.OnPointerClick(new PointerEventData(EventSystem.current));
}
}
else
{
// Find the navigable selectable with the highest y position (highest on the
// screen), and set "next" to that selectable.
next = Selectable.allSelectablesArray
.Where(selectable => selectable.navigation.mode != Navigation.Mode.None && selectable.gameObject.activeInHierarchy && selectable.gameObject.activeSelf)
.OrderByDescending(selectable => selectable.transform.position.y)
.FirstOrDefault();
}
// If a "next" control has been found, then set the selected game object to the
// game object associated with it.
if (next != null)
{
EventSystem.current.SetSelectedGameObject(next.gameObject);
}
}
/// <summary>
/// Handles Enter or Enter equivalent to press the login.
/// </summary>
private void HandleEnterInput()
{
// Skip if enter wasn't pressed, if the event system is null, or if there is no currently selected game object
if (!InputUtility.WasEnterPressed() || null == EventSystem.current ||
null == EventSystem.current.currentSelectedGameObject)
{
return;
}
// NOTE: Previously, this was only checked when the new Input System was being used
// TODO: Test behavior of "Enter" for both input systems.
InputField inputField = EventSystem.current.currentSelectedGameObject.GetComponent<InputField>();
UIConsoleInputField consoleInputField = EventSystem.current.currentSelectedGameObject.GetComponent<UIConsoleInputField>();
if (inputField != null || consoleInputField != null)
{
EnterPressedToLogin();
}
}
/// <summary>
/// Handles a variety of common input tasks executed every frame.
/// </summary>
/// <param name="previouslySelected">The GameObject that was most recently selected.</param>
/// <param name="firstSelected">The firstselectable (typically UIFirstSelected which is the GameObject controller for the scene.</param>
/// <param name="findSelectable">The findSelectable.</param>
private static void HandleInput(ref GameObject previouslySelected, ref GameObject firstSelected,
ref GameObject findSelectable)
{
// Prevents game object from being de-selected.
// NOTE: This seems to intentionally be called *before* determining if input should be handled.
// TODO: Determine whether it should be called after determining if input should be handled.
PreventDeselection(ref previouslySelected);
// Determines whether or not to handle the input (typically not if the EOS overlay is active).
// If input should not be handled, then the process stops here.
if (!ShouldInputBeHandled()) { return; }
// Set the selected GameObject if doing so is necessary.
if (ShouldSetSelectedGameObject())
{
SetSelectedGameObject(ref firstSelected, ref findSelectable);
}
// If tab was pressed, progress the selected control to the next appropriate one.
HandleTabInput();
}
public void Update()
{
HandleInput(ref selectedGameObject, ref UIFirstSelected, ref UIFindSelectable);
HandleEnterInput();
if (null != signInWithAppleManager)
{
signInWithAppleManager.Update();
}
}
private void ConfigureUIForDevAuthLogin()
{
loginTypeDropdown.value = loginTypeDropdown.options.FindIndex(option => option.text == "Dev Auth");
if (!string.IsNullOrEmpty(IdGlobalCache))
{
idInputField.InputField.text = IdGlobalCache;
}
if (!string.IsNullOrEmpty(TokenGlobalCache))
{
tokenInputField.InputField.text = TokenGlobalCache;
}
idContainer.gameObject.SetActive(true);
connectTypeContainer.gameObject.SetActive(false);
idInputField.gameObject.SetActive(true);
tokenInputField.gameObject.SetActive(true);
idText.gameObject.SetActive(true);
tokenText.text = "Username";
tokenTooltip.Text = "Username configured in EOS Dev Auth Tool";
tokenText.gameObject.SetActive(true);
removePersistentTokenButton.gameObject.SetActive(false);
tokenInputField.InputFieldButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = idInputField.InputFieldButton,
selectOnDown = loginButton
};
loginTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = SceneSwitcherDropDown,
selectOnDown = idInputField.InputFieldButton
};
loginButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = tokenInputField.InputFieldButton,
selectOnDown = logoutButton,
selectOnLeft = logoutButton
};
}
private void ConfigureUIForAccountPortalLogin()
{
loginTypeDropdown.value = loginTypeDropdown.options.FindIndex(option => option.text == "Account Portal");
idContainer.gameObject.SetActive(true);
connectTypeContainer.gameObject.SetActive(false);
idInputField.gameObject.SetActive(false);
tokenInputField.gameObject.SetActive(false);
idText.gameObject.SetActive(false);
tokenText.gameObject.SetActive(false);
removePersistentTokenButton.gameObject.SetActive(false);
loginTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = SceneSwitcherDropDown,
selectOnDown = loginButton
};
loginButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = loginTypeDropdown,
selectOnDown = logoutButton,
selectOnLeft = logoutButton
};
// AC/TODO: Reduce duplicated UI code for the different login types
SceneSwitcherDropDown.gameObject.SetActive(true);
DemoTitle.gameObject.SetActive(true);
loginTypeDropdown.gameObject.SetActive(true);
loginButtonText.text = _OriginalloginButtonText;
if (PreventLogIn != null)
StopCoroutine(PreventLogIn);
loginButton.enabled = true;
loginButton.gameObject.SetActive(true);
logoutButton.gameObject.SetActive(false);
EventSystem.current.SetSelectedGameObject(UIFirstSelected);
}
private void ConfigureUIForPersistentLogin()
{
loginTypeDropdown.value = loginTypeDropdown.options.FindIndex(option => option.text == "PersistentAuth");
idContainer.gameObject.SetActive(true);
connectTypeContainer.gameObject.SetActive(false);
idInputField.gameObject.SetActive(false);
tokenInputField.gameObject.SetActive(false);
idText.gameObject.SetActive(false);
tokenText.gameObject.SetActive(false);
removePersistentTokenButton.gameObject.SetActive(true);
loginTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = SceneSwitcherDropDown,
selectOnDown = loginButton
};
loginButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = loginTypeDropdown,
selectOnDown = logoutButton,
selectOnLeft = logoutButton
};
}
//-------------------------------------------------------------------------
private void ConfigureUIForExternalAuth()
{
loginTypeDropdown.value = loginTypeDropdown.options.FindIndex(option => option.text == "ExternalAuth");
idContainer.gameObject.SetActive(true);
connectTypeContainer.gameObject.SetActive(false);
idInputField.gameObject.SetActive(false);
tokenInputField.gameObject.SetActive(false);
idText.gameObject.SetActive(false);
tokenText.gameObject.SetActive(false);
removePersistentTokenButton.gameObject.SetActive(false);
loginTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = SceneSwitcherDropDown,
selectOnDown = loginButton
};
loginButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = loginTypeDropdown,
selectOnDown = logoutButton,
selectOnLeft = logoutButton
};
}
private void ConfigureUIForExchangeCode()
{
loginTypeDropdown.value = loginTypeDropdown.options.FindIndex(option => option.text == "ExchangeCode");
idContainer.gameObject.SetActive(true);
connectTypeContainer.gameObject.SetActive(false);
idInputField.gameObject.SetActive(false);
tokenInputField.gameObject.SetActive(false);
idText.gameObject.SetActive(false);
tokenText.gameObject.SetActive(false);
removePersistentTokenButton.gameObject.SetActive(false);
loginTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = SceneSwitcherDropDown,
selectOnDown = loginButton
};
loginButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = loginTypeDropdown,
selectOnDown = logoutButton,
selectOnLeft = logoutButton
};
}
private void ConfigureUIForConnectLogin()
{
idContainer.gameObject.SetActive(false);
connectTypeContainer.gameObject.SetActive(true);
tokenInputField.gameObject.SetActive(false);
tokenText.gameObject.SetActive(false);
removePersistentTokenButton.gameObject.SetActive(false);
switch (connectType)// might need to check this better, use ifdefs to turn on platform specific cases that switch off the login button
{
//case ExternalCredentialType.GogSessionTicket:
//case ExternalCredentialType.ItchioJwt:
//case ExternalCredentialType.ItchioKey:
//case ExternalCredentialType.AmazonAccessToken:
#if !UNITY_ANDROID || UNITY_EDITOR
case ExternalCredentialType.GoogleIdToken:
loginButton.interactable = false;
loginButtonText.text = "Platform not supported.";
break;
#endif
#if !(UNITY_STANDALONE)
case ExternalCredentialType.SteamSessionTicket:
case ExternalCredentialType.SteamAppTicket:
loginButton.interactable = false;
loginButtonText.text = "Platform not set up.";
break;
#endif
#if !(UNITY_STANDALONE || UNITY_ANDROID)
case ExternalCredentialType.OculusUseridNonce:
loginButton.interactable = false;
loginButtonText.text = "Platform not set up.";
break;
#endif
#if !(UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS)
case ExternalCredentialType.DiscordAccessToken:
case ExternalCredentialType.OpenidAccessToken:
loginButton.interactable = false;
loginButtonText.text = "Platform not set up.";
break;
#endif
#if !(UNITY_IOS || UNITY_STANDALONE_OSX)
case ExternalCredentialType.AppleIdToken:
loginButton.interactable = false;
loginButtonText.text = "Platform not supported.";
break;
#endif
case ExternalCredentialType.DeviceidAccessToken:
default:
break;
}
if (connectType == ExternalCredentialType.OpenidAccessToken)
{
tokenText.text = "Credentials";
tokenTooltip.Text = "Credentials for OpenID login sample in the form of username:password";
tokenInputField.gameObject.SetActive(true);
tokenText.gameObject.SetActive(true);
connectTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = loginTypeDropdown,
selectOnDown = tokenInputField.InputFieldButton,
selectOnLeft = logoutButton
};
loginButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = tokenInputField.InputFieldButton,
selectOnDown = logoutButton,
selectOnLeft = logoutButton
};
tokenInputField.InputFieldButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = connectTypeDropdown,
selectOnDown = loginButton
};
}
else
{
connectTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = loginTypeDropdown,
selectOnDown = loginButton,
selectOnLeft = logoutButton
};
loginButton.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = connectTypeDropdown,
selectOnDown = logoutButton,
selectOnLeft = logoutButton
};
}
loginTypeDropdown.navigation = new Navigation()
{
mode = Navigation.Mode.Explicit,
selectOnUp = SceneSwitcherDropDown,
selectOnDown = connectTypeDropdown
};
}
private void InitConnectDropdown()
{
List<string> externalCredentialTypeLabels = (from ExternalCredentialType type in Enum.GetValues(typeof(ExternalCredentialType))
where type.IsDemoed()
select type.ToString()).ToList();
externalCredentialTypeLabels.Sort();
connectTypeDropdown.options = externalCredentialTypeLabels.Select(
label => new Dropdown.OptionData() { text=label }).ToList();
}
private void ConfigureUIForLogin()
{
if (OnLogout != null)
{
OnLogout.Invoke();
}
SceneSwitcherDropDown.gameObject.SetActive(true);
DemoTitle.gameObject.SetActive(true);
loginTypeDropdown.gameObject.SetActive(true);
loginButtonText.text = _OriginalloginButtonText;
if (PreventLogIn != null)
StopCoroutine(PreventLogIn);
loginButton.enabled = true;
loginButton.interactable = true;
loginButton.gameObject.SetActive(true);
logoutButton.gameObject.SetActive(false);
switch (loginType)
{
case LoginCredentialType.AccountPortal:
ConfigureUIForAccountPortalLogin();
break;
case LoginCredentialType.PersistentAuth:
ConfigureUIForPersistentLogin();
break;
case LoginCredentialType.ExternalAuth:
ConfigureUIForExternalAuth();
break;
case LoginCredentialType.ExchangeCode:
ConfigureUIForExchangeCode();
break;
case connect:
ConfigureUIForConnectLogin();
break;
case LoginCredentialType.Developer:
default:
ConfigureUIForDevAuthLogin();
break;
}
// Controller
//EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(UIFirstSelected);
}
private void ConfigureUIForLogout()
{
SceneSwitcherDropDown.gameObject.SetActive(false);
DemoTitle.gameObject.SetActive(false);
loginTypeDropdown.gameObject.SetActive(false);
loginButton.gameObject.SetActive(false);
logoutButton.gameObject.SetActive(true);
removePersistentTokenButton.gameObject.SetActive(false);
idText.gameObject.SetActive(false);
tokenText.gameObject.SetActive(false);
idInputField.gameObject.SetActive(false);
tokenInputField.gameObject.SetActive(false);
connectTypeContainer.gameObject.SetActive(false);
if (OnLogin != null)
{
OnLogin.Invoke();
}
}
public void OnLogoutButtonClick()
{
// if the readme is open, then close it.
UIReadme readme = UIReadme.FindObjectOfType<UIReadme>();
readme?.CloseReadme();
if (EOSManager.Instance.GetLocalUserId() == null)
{
EOSManager.Instance.ClearConnectId(EOSManager.Instance.GetProductUserId());
ConfigureUIForLogin();
return;
}
EOSManager.Instance.StartLogout(EOSManager.Instance.GetLocalUserId(), (ref LogoutCallbackInfo data) => {
if (data.ResultCode == Result.Success)
{
#if (UNITY_PS4 || UNITY_PS5) && !UNITY_EDITOR
#if UNITY_PS4
var psnManager = EOSManager.Instance.GetOrCreateManager<EOSPSNManagerPS4>();
#elif UNITY_PS5
var psnManager = EOSManager.Instance.GetOrCreateManager<EOSPSNManagerPS5>();
#endif
///TODO: Use activating controller index here
psnManager.StartLogoutWithPSN(0);
#endif
print("Logout Successful. [" + data.ResultCode + "]");
ConfigureUIForLogin();
}
});
}
// For now, the only supported login type that requires a 'username' is the dev auth one
bool SelectedLoginTypeRequiresUsername()
{
return loginType == LoginCredentialType.Developer;
}
// For now, the only supported login type that requires a 'password' is the dev auth one
bool SelectedLoginTypeRequiresPassword()
{
return loginType == LoginCredentialType.Developer;
}
private IEnumerator TurnButtonOnAfter15Sec()
{
for (int i = 15; i >= 0; i--)
{
yield return new WaitForSecondsRealtime(1);
loginButtonText.text = _OriginalloginButtonText + " (" + i + ")";
}
loginButton.enabled = true;
loginButtonText.text = _OriginalloginButtonText;
}
/// <summary>
/// Start a login operation using Steam.
/// </summary>
private void StartLoginWithSteam()
{
var steamManager = Steam.SteamManager.Instance;
string steamId = steamManager?.GetSteamID();
string steamToken = steamManager?.GetSessionTicket();
// Use a coroutine to accomplish a non-UI blocking delay between obtaining
// the steam session ticket and the login operation that uses it. This delay
// is implemented here in the Scene instead of the EOSManager. It is up to
// developers to determine how best to deal with timing issues, and we do not
// want to introduce delays to code that are outside the direct and explicit
// control of game developers. When you implement Steam authentication, you
// may find that implementing such a delay improves your user's experience.
StartCoroutine(StartLoginWithSteamDelayed(() =>
{
if (steamId == null)
{
Debug.LogError("ExternalAuth failed: Steam ID not valid");
}
else if (steamToken == null)
{
Debug.LogError("ExternalAuth failed: Steam session ticket not valid");
}
else
{
EOSManager.Instance.StartLoginWithLoginTypeAndToken(
LoginCredentialType.ExternalAuth,
ExternalCredentialType.SteamSessionTicket,
steamId,
steamToken,
StartLoginWithLoginTypeAndTokenCallback);
}
}));
}
/// <summary>
/// This function is to be called by MonoBehavior's "StartCoroutine" function. While it's
/// signature implies functionality unique to the situation of providing a non-blocking
/// delay to the steam login operation, it's functionality is generic and might later
/// be abstracted into a more generic function.
/// </summary>
/// <param name="action">The action to execute after the indicated seconds have elapsed.</param>
/// <param name="secondsToDelay">The seconds to wait before executing the indicated action.</param>
/// <returns>An enumerator.</returns>
private IEnumerator StartLoginWithSteamDelayed(Action action, float secondsToDelay = 0.5f)
{
yield return new WaitForSeconds(secondsToDelay);
action();
}
//-------------------------------------------------------------------------
// Username and password aren't always the username and password
public void OnLoginButtonClick()
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
Debug.LogError("Internet not reachable.");
return;
}
string usernameAsString = idInputField.InputField.text.Trim();
string passwordAsString = tokenInputField.InputField.text.Trim();
if (SelectedLoginTypeRequiresUsername() && usernameAsString.Length <= 0)
{
print("Username is missing.");
return;
}
if (SelectedLoginTypeRequiresPassword() && passwordAsString.Length <= 0)
{