Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using UnityEngine.UI;
  6. using PlayFab;
  7. using PlayFab.ClientModels;
  8. using System;
  9.  
  10. #if GOOGLEGAMES
  11. using GooglePlayGames;
  12. using GooglePlayGames.BasicApi;
  13. #endif
  14.  
  15. public class LoginWindowView : MonoBehaviour
  16. {
  17. public bool ClearPlayerPrefs;
  18.  
  19. public TextMeshProUGUI usernameField;
  20. public TextMeshProUGUI passwordField;
  21. public TextMeshProUGUI confirmPasswordField;
  22. public TextMeshProUGUI infoText;
  23.  
  24. public Button loginButton;
  25. public Button loginWithGoogle;
  26. public Button registerButton;
  27. public Button cancelRegisterButton;
  28. public Toggle rememberMe;
  29.  
  30. public GameObject registerPanel;
  31. public GameObject mainPanel;
  32.  
  33. public GetPlayerCombinedInfoRequestParams InfoRequestParams;
  34.  
  35. // Reference to our Authentication service
  36. private PlayFabAuthService _AuthService = PlayFabAuthService.Instance;
  37.  
  38. public void Awake()
  39. {
  40.  
  41. #if GOOGLEGAMES
  42. PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
  43. .AddOauthScope("profile")
  44. .RequestServerAuthCode(false)
  45. .Build();
  46. PlayGamesPlatform.InitializeInstance(config);
  47.  
  48. PlayGamesPlatform.DebugLogEnabled = true;
  49.  
  50. PlayGamesPlatform.Activate();
  51. #endif
  52.  
  53.  
  54. if (ClearPlayerPrefs)
  55. {
  56. _AuthService.UnlinkSilentAuth();
  57. _AuthService.ClearRememberMe();
  58. _AuthService.AuthType = Authtypes.None;
  59. }
  60.  
  61. //Set our remember me button to our remembered state.
  62. rememberMe.isOn = _AuthService.RememberMe;
  63.  
  64. //Subscribe to our Remember Me toggle
  65. rememberMe.onValueChanged.AddListener((toggle) =>
  66. {
  67. _AuthService.RememberMe = toggle;
  68. });
  69. }
  70.  
  71. void Start()
  72. {
  73. mainPanel.SetActive(false);
  74. registerPanel.SetActive(false);
  75.  
  76. // subscribe to events that happen after we authenticate
  77. // we are telling the delegate in PlayFabAuthService to call this method
  78. PlayFabAuthService.OnDisplayAuthentication += OnDisplayAuthentication;
  79. PlayFabAuthService.OnLoginSuccess += OnLoginSuccess;
  80. PlayFabAuthService.OnPlayFabError += OnPlayFaberror;
  81.  
  82. loginButton.onClick.AddListener(OnLoginClicked);
  83. loginWithGoogle.onClick.AddListener(OnLoginWithGoogleClicked);
  84. registerButton.onClick.AddListener(OnRegisterButtonClicked);
  85. cancelRegisterButton.onClick.AddListener(OnCancelRegisterButtonClicked);
  86.  
  87. _AuthService.InfoRequestParams = InfoRequestParams;
  88.  
  89. _AuthService.Authenticate();
  90. }
  91.  
  92. private void OnCancelRegisterButtonClicked()
  93. {
  94. //Reset all forms
  95. usernameField.text = string.Empty;
  96. passwordField.text = string.Empty;
  97. confirmPasswordField.text = string.Empty;
  98. //Show panels
  99. registerPanel.SetActive(false);
  100. }
  101.  
  102. private void OnRegisterButtonClicked()
  103. {
  104. if (passwordField.text != confirmPasswordField.text)
  105. {
  106. infoText.text = "Passwords do not match.";
  107. return;
  108. }
  109.  
  110. _AuthService.Email = usernameField.text;
  111. _AuthService.Password = passwordField.text;
  112. _AuthService.Authenticate(Authtypes.RegisterPlayFabAccount);
  113. }
  114.  
  115. private void OnLoginSuccess(LoginResult result)
  116. {
  117. Debug.LogFormat("Logged In as: {0}", result.PlayFabId);
  118. SceneLoader.Load(SceneLoader.Scene.Menu);
  119. }
  120.  
  121. private void OnPlayFaberror(PlayFabError error)
  122. {
  123. switch (error.Error)
  124. {
  125. case PlayFabErrorCode.InvalidEmailAddress:
  126. case PlayFabErrorCode.InvalidPassword:
  127. case PlayFabErrorCode.InvalidEmailOrPassword:
  128. Debug.Log("Invalid email or password");
  129. break;
  130. case PlayFabErrorCode.AccountNotFound:
  131. registerPanel.SetActive(true);
  132. return;
  133. default:
  134. Debug.Log(error.GenerateErrorReport());
  135. break;
  136.  
  137. }
  138.  
  139. Debug.Log(error.Error);
  140. }
  141.  
  142. private void OnLoginWithGoogleClicked()
  143. {
  144. // using unity social platform
  145. Social.localUser.Authenticate((success) =>
  146. {
  147. if (success)
  148. {
  149. // will get the sever auth code
  150. // need to take it and send it to playfab to auth
  151. var serverAuthCode = PlayGamesPlatform.Instance.GetServerAuthCode();
  152. _AuthService.AuthTicket = serverAuthCode;
  153. _AuthService.Authenticate(Authtypes.Google);
  154. }
  155. });
  156. }
  157.  
  158. private void OnLoginClicked()
  159. {
  160. infoText.text = string.Format("Logging in as {0} ...", usernameField.text);
  161.  
  162. _AuthService.Email = usernameField.text;
  163. _AuthService.Password = passwordField.text;
  164. _AuthService.Authenticate(Authtypes.EmailAndPassword);
  165. }
  166.  
  167. /// <summary>
  168. /// Choose to display the Auth UI or any other action.
  169. /// </summary>
  170. private void OnDisplayAuthentication()
  171. {
  172.  
  173. //#if FACEBOOK
  174. // if (FB.IsInitialized)
  175. // {
  176. // Debug.LogFormat("FB is Init: AccessToken:{0} IsLoggedIn:{1}",AccessToken.CurrentAccessToken.TokenString, FB.IsLoggedIn);
  177. // if (AccessToken.CurrentAccessToken == null || !FB.IsLoggedIn)
  178. // {
  179. // Panel.SetActive(true);
  180. // }
  181. // }
  182. // else
  183. // {
  184. // Panel.SetActive(true);
  185. // Debug.Log("FB Not Init");
  186. // }
  187. //#else
  188. //Here we have choses what to do when AuthType is None.
  189. mainPanel.SetActive(true);
  190. //#endif
  191. /*
  192. * Optionally we could Not do the above and force login silently
  193. *
  194. * _AuthService.Authenticate(Authtypes.Silent);
  195. *
  196. * This example, would auto log them in by device ID and they would
  197. * never see any UI for Authentication.
  198. *
  199. */
  200. }
  201.  
  202. /// <summary>
  203. /// Play As a guest, which means they are going to silently authenticate
  204. /// by device ID or Custom ID
  205. /// </summary>
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement