| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- using UnityEngine.SceneManagement;
- using System;
- public class Login : MonoBehaviour {
- public GameObject username;
- public GameObject password;
- public Button loginButton;
- public Text errorText;
- private string Username;
- private string Password;
- private string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
- private Color errorColor;
- private EventSystem system;
- [Serializable]
- public class User {
- public string userId;
- public string pass;
- public string salt;
- }
- private void Start() {
- loginButton.onClick.AddListener(loginAction);
- errorColor = errorText.color;
- system = EventSystem.current;
- }
- private void Update() {
- Username = username.GetComponent<InputField>().text;
- Password = password.GetComponent<InputField>().text;
- if (Input.GetKeyDown(KeyCode.Tab)) {
- Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
- if (next != null) {
- InputField inputfield = next.GetComponent<InputField>();
- if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
- system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
- }
- }
- }
- void loginAction() {
- string errorMessage = "";
- if (Username == "") {
- errorMessage = "Username is required";
- }
- if (Password == "") {
- if (errorMessage != "") {
- errorMessage += "\n";
- }
- errorMessage += "Password is required";
- }
- if (errorMessage == "") {
- errorColor.a = 0;
- StartCoroutine(loginCall());
- } else {
- errorText.text = errorMessage;
- errorColor.a = 1;
- }
- errorText.color = errorColor;
- }
- IEnumerator loginCall() {
- string postUrl = loginUrl + "name=" + UnityWebRequest.EscapeURL(Username) + "&password=" + UnityWebRequest.EscapeURL(Password);
- UnityWebRequest www = UnityWebRequest.Get(postUrl);
- yield return www.SendWebRequest();
- if (www.error != null) {
- errorText.text = "There was an error logging in " + www.error;
- }
- string result = www.downloadHandler.text;
- User u = new User();
- if (int.TryParse(www.downloadHandler.text, out int userId)) {
- errorColor.a = 0;
- errorText.color = errorColor;
- PlayerPrefs.SetInt("UserId", userId);
- // load next scene
- SceneManager.LoadScene("MainMenu");
- } else {
- errorText.text = "Misslyckades med inloggning";
- errorColor.a = 1;
- errorText.color = errorColor;
- }
- }
- }
|