Advertisement
Guest User

LoginMenu C#

a guest
Jan 6th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.67 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using DatabaseControl; // << Remember to add this reference to your scripts which use DatabaseControl
  5.  
  6. public class LoginMenu : MonoBehaviour
  7. {
  8. //All public variables bellow are assigned in the Inspector
  9.  
  10. //These are the GameObjects which are parents of groups of UI elements. The objects are enabled and disabled to show and hide the UI elements.
  11. public GameObject loginParent;
  12. public GameObject registerParent;
  13. public GameObject loadingParent;
  14.  
  15. //These are all the InputFields which we need in order to get the entered usernames, passwords, etc
  16. public InputField Login_UsernameField;
  17. public InputField Login_PasswordField;
  18. public InputField Register_UsernameField;
  19. public InputField Register_PasswordField;
  20. public InputField Register_ConfirmPasswordField;
  21.  
  22. //These are the UI Texts which display errors
  23. public Text Login_ErrorText;
  24. public Text Register_ErrorText;
  25.  
  26. //Called at the very start of the game
  27. void Awake()
  28. {
  29. ResetAllUIElements();
  30. }
  31.  
  32. //Called by Button Pressed Methods to Reset UI Fields
  33. void ResetAllUIElements()
  34. {
  35. //This resets all of the UI elements. It clears all the strings in the input fields and any errors being displayed
  36. Login_UsernameField.text = "";
  37. Login_PasswordField.text = "";
  38. Register_UsernameField.text = "";
  39. Register_PasswordField.text = "";
  40. Register_ConfirmPasswordField.text = "";
  41. blankErrors();
  42. }
  43.  
  44. void blankErrors()
  45. {
  46. //blanks all error texts when part is changed e.g. login > Register
  47. Login_ErrorText.text = "";
  48. Register_ErrorText.text = "";
  49. }
  50.  
  51. //Called by Button Pressed Methods. These use DatabaseControl namespace to communicate with server.
  52. IEnumerator LoginUser(string username, string password)
  53. {
  54. IEnumerator e = DCF.Login(username, password); // << Send request to login, providing username and password
  55. while (e.MoveNext())
  56. {
  57. yield return e.Current;
  58. }
  59. string response = e.Current as string; // << The returned string from the request
  60.  
  61. if (response == "Success")
  62. {
  63. //Username and Password were correct. Stop showing 'Loading...' and show the LoggedIn UI. And set the text to display the username.
  64. ResetAllUIElements();
  65. // loadingParent.gameObject.SetActive(false);
  66. // backGround.gameObject.SetActive(false);
  67. // loggedInParent.gameObject.SetActive(true);
  68. UserAccountManager.instance.LogIn(username, password);
  69. }
  70. else
  71. {
  72. //Something went wrong logging in. Stop showing 'Loading...' and go back to LoginUI
  73. loadingParent.gameObject.SetActive(false);
  74. loginParent.gameObject.SetActive(true);
  75. if (response == "UserError")
  76. {
  77. //The Username was wrong so display relevent error message
  78. Login_ErrorText.text = "Error: Username not Found";
  79. }
  80. else
  81. {
  82. if (response == "PassError")
  83. {
  84. //The Password was wrong so display relevent error message
  85. Login_ErrorText.text = "Error: Password Incorrect";
  86. }
  87. else
  88. {
  89. //There was another error. This error message should never appear, but is here just in case.
  90. Login_ErrorText.text = "Error: Unknown Error. Please try again later.";
  91. }
  92. }
  93. }
  94. }
  95.  
  96. IEnumerator RegisterUser(string username, string password, string data)
  97. {
  98. IEnumerator e = DCF.RegisterUser(username, password, data); // << Send request to register a new user, providing submitted username and password. It also provides an initial value for the data string on the account, which is "Hello World".
  99. while (e.MoveNext())
  100. {
  101. yield return e.Current;
  102. }
  103. string response = e.Current as string; // << The returned string from the request
  104.  
  105. if (response == "Success")
  106. {
  107. //Username and Password were valid. Account has been created. Stop showing 'Loading...' and show the loggedIn UI and set text to display the username.
  108. ResetAllUIElements();
  109. //loadingParent.gameObject.SetActive(false);
  110. UserAccountManager.instance.LogIn(username, password);
  111. }
  112. else
  113. {
  114. //Something went wrong logging in. Stop showing 'Loading...' and go back to RegisterUI
  115. loadingParent.gameObject.SetActive(false);
  116. registerParent.gameObject.SetActive(true);
  117. if (response == "UserError")
  118. {
  119. //The username has already been taken. Player needs to choose another. Shows error message.
  120. Register_ErrorText.text = "Error: Username Already Taken";
  121. }
  122. else
  123. {
  124. //There was another error. This error message should never appear, but is here just in case.
  125. Login_ErrorText.text = "Error: Unknown Error. Please try again later.";
  126. }
  127. }
  128. }
  129.  
  130. //UI Button Pressed Methods
  131. public void Login_LoginButtonPressed()
  132. {
  133. //Called when player presses button to Login
  134.  
  135. //Get the username and password the player entered
  136. string username = Login_UsernameField.text;
  137. string pass = Login_PasswordField.text;
  138. //Check the lengths of the username and password. (If they are wrong, we might as well show an error now instead of waiting for the request to the server)
  139. if (username.Length > 3)
  140. {
  141. if (pass.Length > 5)
  142. {
  143. //Username and password seem reasonable. Change UI to 'Loading...'. Start the Coroutine which tries to log the player in.
  144. loginParent.gameObject.SetActive(false);
  145. loadingParent.gameObject.SetActive(true);
  146. StartCoroutine(LoginUser(username, pass));
  147. }
  148. else
  149. {
  150. //Password too short so it must be wrong
  151. Login_ErrorText.text = "Error: Password format Incorrect (length must be > 5)";
  152. }
  153. }
  154. else if (username.Length == 0 && pass.Length == 0)
  155. Login_ErrorText.text = "Error : Blank Field, Try again please.";
  156. else
  157. {
  158. //Username too short so it must be wrong
  159. Login_ErrorText.text = "Error: Username format Incorrect (length must be > 3)";
  160. }
  161. }
  162.  
  163. public void Login_RegisterButtonPressed() //QUAND LE MEC APPUIE SUR LE BOUTON REGISTER(qui n'estpas actif de base, pour ouvrir les elements)
  164. {
  165. //Called when the player hits register on the Login UI, so switches to the Register UI
  166. ResetAllUIElements();
  167. loginParent.gameObject.SetActive(false);
  168. registerParent.gameObject.SetActive(true);
  169. }
  170.  
  171. public void Register_RegisterButtonPressed() //APRES AVOIR REMPLIT LES 3 champs USER PASS CONFIRM ...
  172. {
  173. //Called when the player presses the button to register
  174.  
  175. //Get the username and password and repeated password the player entered
  176. string username = Register_UsernameField.text;
  177. string password = Register_PasswordField.text;
  178. string confirmedPassword = Register_ConfirmPasswordField.text;
  179.  
  180. //Make sure username and password are long enough
  181. if (username.Length > 3)
  182. {
  183. if (password.Length > 5)
  184. {
  185. //Check the two passwords entered match
  186. if (password == confirmedPassword)
  187. {
  188. //Username and passwords seem reasonable. Switch to 'Loading...' and start the coroutine to try and register an account on the server
  189. registerParent.gameObject.SetActive(false);
  190. loadingParent.gameObject.SetActive(true);
  191. StartCoroutine(RegisterUser(username, password, "[KILLS]0/[DEATHS]0"));
  192. }
  193. else
  194. {
  195. //Passwords don't match, show error
  196. Register_ErrorText.text = "Error: Password's don't Match";
  197. }
  198. }
  199. else
  200. {
  201. //Password too short so show error
  202. Register_ErrorText.text = "Error: Password too Short";
  203. }
  204. }
  205. else
  206. {
  207. //Username too short so show error
  208. Register_ErrorText.text = "Error: Username too Short";
  209. }
  210. }
  211.  
  212. public void Register_BackButtonPressed() //PAS ENVIE DE MENREGISTRER LOL, DONC REVIENS EN ARRIERE
  213. {
  214. //Called when the player presses the 'Back' button on the register UI. Switches back to the Login UI
  215. ResetAllUIElements();
  216. loginParent.gameObject.SetActive(true);
  217. registerParent.gameObject.SetActive(false);
  218. }
  219.  
  220. public void LoggedIn_LogoutButtonPressed() // A METTRE UN BOUTON LOGOUT SUR LE MENU PRINCIPALE
  221. {
  222. //Called when the player hits the 'Logout' button. Switches back to Login UI and forgets the player's username and password.
  223. //Note: Database Control doesn't use sessions, so no request to the server is needed here to end a session.
  224. ResetAllUIElements();
  225. UserAccountManager.instance.LogOut();
  226. loginParent.gameObject.SetActive(true);
  227. //SceneManagement.LoadScene(0);
  228. // loggedInParent.gameObject.SetActive(false);
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement