Hello guys! Watch this series of my videos to see how we can make Unity login, logout and register easily:
And here are the code I wrote:
First, config.php file
<?php
$databasename = "mydatabase";
$host = "localhost";
$dbuser = "root";
$dbpassword = "";
$connection = mysqli_connect($host, $dbuser, $dbpassword, $databasename);
?>
Then the index.php backend file
<?php
include("config.php");
if(isset($_POST["newAccountUsername"])){
$username = mysqli_real_escape_string($connection, $_POST["newAccountUsername"]);
$password = mysqli_real_escape_string($connection, $_POST["newAccountPassword"]);
//Check are they empty?
if($username != "" && $password != ""){
//Check is the username has not taken
if(mysqli_num_rows(mysqli_query($connection, "SELECT * FROM unity_users WHERE username = '$username'")) == 0){
mysqli_query($connection, "INSERT INTO unity_users (username, password) VALUES ('$username', '$password')");
echo "Regitering new account: Username " . $username . " and password: " . $password;
}else{
echo "This Username is not available. Please use another username.";
}
}else{
echo "Both fields are required.";
}
}else if(isset($_POST["loginUsername"])){
$username = mysqli_real_escape_string($connection, $_POST["loginUsername"]);
$password = mysqli_real_escape_string($connection, $_POST["loginPassword"]);
if($username != "" && $password != ""){
//Check are entered username and password matched
$sql = "SELECT * FROM unity_users WHERE username = '$username' AND password = '$password'";
if(mysqli_num_rows(mysqli_query($connection, $sql)) > 0){
echo 1;
}else{
echo 0;
}
}else{
echo "Both fields are required.";
}
}else{
echo "Unity Login Logout and Register";
}
?>
And the last file is UnityLoginLogoutRegister.cs file
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class UnityLoginLogoutRegister : MonoBehaviour
{
public string baseUrl = "http://localhost/ThirteeNov/UnityLoginLogoutRegister/";
public InputField accountUserName;
public InputField accountPassword;
public Text info;
private string currentUsername;
private string ukey = "accountusername";
// Start is called before the first frame update
void Start()
{
currentUsername = "";
if(PlayerPrefs.HasKey(ukey)){
if(PlayerPrefs.GetString(ukey) != ""){
currentUsername = PlayerPrefs.GetString(ukey);
info.text = "You are loged in = " + currentUsername;
}else{
info.text = "You are not loged in.";
}
}else{
info.text = "You are not loged in.";
}
}
// Update is called once per frame
void Update()
{
}
public void AccountLogout(){
currentUsername = "";
PlayerPrefs.SetString(ukey, currentUsername);
info.text = "You are just loged out.";
}
public void AccountRegister()
{
string uName = accountUserName.text;
string pWord = accountPassword.text;
StartCoroutine(RegisterNewAccount(uName, pWord));
}
public void AccountLogin()
{
string uName = accountUserName.text;
string pWord = accountPassword.text;
StartCoroutine(LogInAccount(uName, pWord));
}
IEnumerator RegisterNewAccount(string uName, string pWord)
{
WWWForm form = new WWWForm();
form.AddField("newAccountUsername", uName);
form.AddField("newAccountPassword", pWord);
using (UnityWebRequest www = UnityWebRequest.Post(baseUrl, form))
{
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
string responseText = www.downloadHandler.text;
Debug.Log("Response = " + responseText);
info.text = "Response = " + responseText;
}
}
}
IEnumerator LogInAccount(string uName, string pWord)
{
WWWForm form = new WWWForm();
form.AddField("loginUsername", uName);
form.AddField("loginPassword", pWord);
using (UnityWebRequest www = UnityWebRequest.Post(baseUrl, form))
{
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
string responseText = www.downloadHandler.text;
if(responseText == "1"){
PlayerPrefs.SetString(ukey, uName);
//info.text = "Login Success with username " + uName;
SceneManager.LoadSceneAsync("Home");
}else{
info.text = "Login Failed.";
}
}
}
}
}
In case you have problem seeing above scripts, check out this github link: https://github.com/habibieamrullah/SimpleUnityLoginLogoutRegisterSystem
loading...