Login.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.SceneManagement;
  8. using System;
  9. public class Login : MonoBehaviour {
  10. public GameObject username;
  11. public GameObject password;
  12. public Button loginButton;
  13. public Text errorText;
  14. private string Username;
  15. private string Password;
  16. private string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
  17. private Color errorColor;
  18. private EventSystem system;
  19. [Serializable]
  20. public class User {
  21. public string userId;
  22. public string pass;
  23. public string salt;
  24. }
  25. private void Start() {
  26. loginButton.onClick.AddListener(loginAction);
  27. errorColor = errorText.color;
  28. system = EventSystem.current;
  29. }
  30. private void Update() {
  31. Username = username.GetComponent<InputField>().text;
  32. Password = password.GetComponent<InputField>().text;
  33. if (Input.GetKeyDown(KeyCode.Tab)) {
  34. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  35. if (next != null) {
  36. InputField inputfield = next.GetComponent<InputField>();
  37. if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
  38. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  39. }
  40. }
  41. }
  42. void loginAction() {
  43. string errorMessage = "";
  44. if (Username == "") {
  45. errorMessage = "Username is required";
  46. }
  47. if (Password == "") {
  48. if (errorMessage != "") {
  49. errorMessage += "\n";
  50. }
  51. errorMessage += "Password is required";
  52. }
  53. if (errorMessage == "") {
  54. errorColor.a = 0;
  55. StartCoroutine(loginCall());
  56. } else {
  57. errorText.text = errorMessage;
  58. errorColor.a = 1;
  59. }
  60. errorText.color = errorColor;
  61. }
  62. IEnumerator loginCall() {
  63. string postUrl = loginUrl + "name=" + UnityWebRequest.EscapeURL(Username) + "&password=" + UnityWebRequest.EscapeURL(Password);
  64. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  65. yield return www.SendWebRequest();
  66. if (www.error != null) {
  67. errorText.text = "There was an error logging in " + www.error;
  68. }
  69. string result = www.downloadHandler.text;
  70. User u = new User();
  71. if (int.TryParse(www.downloadHandler.text, out int userId)) {
  72. errorColor.a = 0;
  73. errorText.color = errorColor;
  74. PlayerPrefs.SetInt("UserId", userId);
  75. // load next scene
  76. SceneManager.LoadScene("MainMenu");
  77. } else {
  78. errorText.text = "Misslyckades med inloggning";
  79. errorColor.a = 1;
  80. errorText.color = errorColor;
  81. }
  82. }
  83. }