Login.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. public class Login : MonoBehaviour {
  7. public GameObject username;
  8. public GameObject password;
  9. public Button loginButton;
  10. public Text errorText;
  11. private string Username;
  12. private string Password;
  13. private string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
  14. private void Start() {
  15. loginButton.onClick.AddListener(loginAction);
  16. }
  17. private void Update() {
  18. Username = username.GetComponent<InputField>().text;
  19. Password = password.GetComponent<InputField>().text;
  20. }
  21. void loginAction() {
  22. string errorMessage = "";
  23. if (Username == "") {
  24. errorMessage = "Username is required";
  25. }
  26. if (Password == "") {
  27. if (errorMessage != "") {
  28. errorMessage += "\n";
  29. }
  30. errorMessage += "Password is required";
  31. }
  32. Color errorColor = errorText.color;
  33. if (errorMessage == "") {
  34. errorColor.a = 0;
  35. StartCoroutine(loginCall());
  36. } else {
  37. errorText.text = errorMessage;
  38. errorColor.a = 1;
  39. }
  40. errorText.color = errorColor;
  41. }
  42. IEnumerator loginCall() {
  43. string postUrl = loginUrl + "name=" + UnityWebRequest.EscapeURL(Username) + "&password=" + UnityWebRequest.EscapeURL(Password);
  44. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  45. yield return www.SendWebRequest();
  46. if (www.error != null) {
  47. errorText.text = "There was an error logging in " + www.error;
  48. }
  49. }
  50. }