Просмотр исходного кода

Small fix for login and register, switching scenes

Axel Nordh 7 лет назад
Родитель
Сommit
218b7dbc38

+ 30 - 1
Assets/Scripts/Login/Login.cs

@@ -3,6 +3,8 @@ using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 using UnityEngine.UI;
+using UnityEngine.EventSystems;
+using UnityEngine.SceneManagement;
 
 public class Login : MonoBehaviour {
 
@@ -16,14 +18,30 @@ public class Login : MonoBehaviour {
     private string Username;
     private string Password;
     private string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
+    private Color errorColor;
+    private EventSystem system;
 
     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() {
@@ -37,7 +55,7 @@ public class Login : MonoBehaviour {
             }
             errorMessage += "Password is required";
         }
-        Color errorColor = errorText.color;
+
         if (errorMessage == "") {
             errorColor.a = 0;
             StartCoroutine(loginCall());
@@ -57,5 +75,16 @@ public class Login : MonoBehaviour {
         if (www.error != null) {
             errorText.text = "There was an error logging in " + www.error;
         }
+        string result = www.downloadHandler.text;
+        if (result.Equals("Success")) {
+            errorColor.a = 0;
+            errorText.color = errorColor;
+            // load next scene
+            SceneManager.LoadScene("MainMenu");
+        } else {
+            errorText.text = "Misslyckades med inloggning";
+            errorColor.a = 1;
+            errorText.color = errorColor;
+        }
     }
 }

+ 33 - 2
Assets/Scripts/Login/Register.cs

@@ -2,6 +2,8 @@
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Networking;
+using UnityEngine.EventSystems;
+using UnityEngine.SceneManagement;
 
 public class Register : MonoBehaviour {
     public GameObject username;
@@ -18,9 +20,15 @@ public class Register : MonoBehaviour {
 
     private string registerUserUrl = "http://nordh.xyz/narKampen/dbFiles/Register.php?";
 
+    private Color errorColor;
+
+    private EventSystem system;
+
     // Start is called before the first frame update
     void Start() {
         registerButton.onClick.AddListener(RegisterAction);
+        system = EventSystem.current;
+        errorColor = errorText.color;
     }
 
     // Update is called once per frame
@@ -28,6 +36,18 @@ public class Register : MonoBehaviour {
         Username = username.GetComponent<InputField>().text;
         Password = password.GetComponent<InputField>().text;
         Email = email.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 RegisterAction() {
@@ -48,7 +68,7 @@ public class Register : MonoBehaviour {
             errorMessage += "Email is obligatory";
         }
 
-        Color errorColor = errorText.color;
+
         if (errorMessage != "") {
             errorText.text = errorMessage;
             errorColor.a = 1;
@@ -63,7 +83,7 @@ public class Register : MonoBehaviour {
     IEnumerator RegisterUser() {
         // register user at server, check for already in use for username and email.
         // TODO - hash+salt password
-        BCrypt.Net.BCrypt
+        
         string postUrl = registerUserUrl + "name=" + UnityWebRequest.EscapeURL(Username) + "&password=" + UnityWebRequest.EscapeURL(Password) + "&email=" + UnityWebRequest.EscapeURL(Email);
 
         UnityWebRequest www = UnityWebRequest.Get(postUrl);
@@ -72,5 +92,16 @@ public class Register : MonoBehaviour {
         if (www.error != null) {
             errorText.text = "There was an error registering this user, try again later";
         }
+
+        string result = www.downloadHandler.text;
+        if (result == "Success") {
+            errorColor.a = 0;
+            errorText.color = errorColor;
+            SceneManager.LoadScene("MainMenu");
+        } else {
+            errorText.text = "Failed to register, username/email already exists"; // be more helpful, get message from sever if it is username or email 
+            errorColor.a = 1;
+            errorText.color = errorColor;
+        }
     }
 }

+ 10 - 1
Assets/Scripts/MainMenu.cs

@@ -1,4 +1,5 @@
-using System.Collections;
+using System;
+using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
@@ -9,4 +10,12 @@ public class MainMenu : MonoBehaviour
     public Button startGameButton;
     public Button quitButton;
 
+    private void Start() {
+        startGameButton.onClick.AddListener(startGameAction);
+        quitButton.onClick.AddListener(() => { Application.Quit();} );
+    }
+
+    private void startGameAction() {
+        throw new NotImplementedException();
+    }
 }

+ 10 - 8
Assets/User.unity

@@ -601,7 +601,7 @@ MonoBehaviour:
   m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
   m_CustomCaretColor: 0
   m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
-  m_Text: Password
+  m_Text: 
   m_CaretBlinkRate: 0.85
   m_CaretWidth: 1
   m_ReadOnly: 0
@@ -864,7 +864,7 @@ MonoBehaviour:
   m_PrefabInstance: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 321705903}
-  m_Enabled: 0
+  m_Enabled: 1
   m_EditorHideFlags: 0
   m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3}
   m_Name: 
@@ -890,7 +890,7 @@ MonoBehaviour:
     m_HorizontalOverflow: 0
     m_VerticalOverflow: 0
     m_LineSpacing: 1
-  m_Text: Enter text...
+  m_Text: '* Username'
 --- !u!222 &321705906
 CanvasRenderer:
   m_ObjectHideFlags: 0
@@ -1500,7 +1500,7 @@ MonoBehaviour:
     m_HorizontalOverflow: 1
     m_VerticalOverflow: 0
     m_LineSpacing: 1
-  m_Text: '********'
+  m_Text: 
 --- !u!222 &762506837
 CanvasRenderer:
   m_ObjectHideFlags: 0
@@ -1905,6 +1905,8 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   username: {fileID: 1437897725}
   password: {fileID: 231128496}
+  loginButton: {fileID: 1421835109}
+  errorText: {fileID: 1197443347}
 --- !u!1 &1197443345
 GameObject:
   m_ObjectHideFlags: 0
@@ -2173,7 +2175,7 @@ MonoBehaviour:
   m_PrefabInstance: {fileID: 0}
   m_PrefabAsset: {fileID: 0}
   m_GameObject: {fileID: 1420357677}
-  m_Enabled: 0
+  m_Enabled: 1
   m_EditorHideFlags: 0
   m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3}
   m_Name: 
@@ -2199,7 +2201,7 @@ MonoBehaviour:
     m_HorizontalOverflow: 0
     m_VerticalOverflow: 0
     m_LineSpacing: 1
-  m_Text: Enter text...
+  m_Text: '* Password'
 --- !u!222 &1420357680
 CanvasRenderer:
   m_ObjectHideFlags: 0
@@ -2426,7 +2428,7 @@ MonoBehaviour:
   m_CaretColor: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
   m_CustomCaretColor: 0
   m_SelectionColor: {r: 0.65882355, g: 0.80784315, b: 1, a: 0.7529412}
-  m_Text: Username
+  m_Text: 
   m_CaretBlinkRate: 0.85
   m_CaretWidth: 1
   m_ReadOnly: 0
@@ -2948,7 +2950,7 @@ MonoBehaviour:
     m_HorizontalOverflow: 1
     m_VerticalOverflow: 0
     m_LineSpacing: 1
-  m_Text: Username
+  m_Text: 
 --- !u!222 &2050609599
 CanvasRenderer:
   m_ObjectHideFlags: 0

+ 6 - 0
ProjectSettings/EditorBuildSettings.asset

@@ -8,4 +8,10 @@ EditorBuildSettings:
   - enabled: 1
     path: Assets/narKampen.unity
     guid: 173419871896d2742a84e462fa3af7df
+  - enabled: 1
+    path: Assets/MainMenu.unity
+    guid: 663bfb586ff76f445950dd5434725919
+  - enabled: 1
+    path: Assets/User.unity
+    guid: 38925ed866f076e4cb6d819774839d4a
   m_configObjects: {}

+ 2 - 2
dbFiles/Login.php

@@ -13,14 +13,14 @@
 	$username = $conn->real_escape_string($_GET['name']);
 	$password = $conn->real_escape_string($_GET['password']);
 	
-	if ($newUsername == '' || $newPassword == '') {
+	if ($username == '' || $password == '') {
 		echo "Supply values for all required fields";
 		return;
 	}
 	
 	$sql = "SELECT id FROM users WHERE username = '$username' AND password = '$password'";
 	$result = $conn->query($sql);
-	if ($result->mysqli_num_rows == 1) {
+	if ($result->num_rows == 1) {
 		echo "Success";
 	} else {
 		echo "Error while logging in user " . $conn->error;