In this blog, I'll walk you through the process of integrating databases to manage accounts and implementing a login screen in Realm Rift.
Firstly, I created a new scene in Unity and added two input fields for username and password, along with a button for login.
Next, I set up XAMPP on my device and created a SQL database using PHPMyAdmin.
Then, in my C drive " C:\xampp\htdocs\" , I created a folder by " RealmRift" and added PHP scripts to store user data. Here are the scripts I used:
1. Fetching Users: I used this script to retrieve user data from the database. It displays usernames and their corresponding levels.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "real_rift";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully, now we will show the users.<br><br>";
$sql = "SELECT username FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "username: " . $row["username"]. "<br>";
} else {
echo "0 results";
}
$conn->close();
?>
2. Login Script: This script handles the login process. It verifies the entered username and password against the database and grants access if the credentials match.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "real_rift";
//variables submitted by user
$loginUser = $_POST["loginUser"];
$loginPass = $_POST["loginPass"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT password FROM users WHERE username = '" . $loginUser . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if($row["password"] == $loginPass)
{
echo "Login Success";
}
else {
echo "Wrong Credentials.";
}
}
} else {
echo "Username does not exist";
}
$conn->close();
?>
3. User Registration: This script checks if the entered username is available. If it is, the user is registered in the database with their chosen password.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "real_rift";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// statement to check if username exists
$stmt = $conn->prepare("SELECT username FROM users WHERE username = ?");
$stmt->bind_param("s", $loginUser);
$loginUser = $_POST["loginUser"];
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// Username is already taken
echo "Username is already taken.";
} else {
// statement to insert a new user
$stmt = $conn->prepare("INSERT INTO users (username, password, level, gold) VALUES (?, ?, 1, 0)");
$stmt->bind_param("ss", $loginUser, $loginPass);
$loginPass = $_POST["loginPass"];
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $stmt->error;
}
}
$stmt->close();
$conn->close();
?>
To interact with the server and manage data, I created C# scripts in Unity:
Web Script: This script handles communication with the server. It includes functions to retrieve user data, log in users, and register new users.
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class Web : MonoBehaviour
{
public IEnumerator GetUsers(Action<List<string>> resultCallback)
{
using (UnityWebRequest www = UnityWebRequest.Get("http://localhost/RealmRift/GetUsers.php"))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
resultCallback(null); // Failure in getting the data
}
else
{
List<string> users = new List<string>();
string[] rows = www.downloadHandler.text.Split(new string[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string row in rows)
{
if (!string.IsNullOrWhiteSpace(row) && row.Contains("username:"))
{
users.Add(row.Trim());
}
}
resultCallback(users); // Successfully fetched the data
}
}
}
public IEnumerator Login(string username, string password, Action<bool> resultCallback)
{
WWWForm form = new WWWForm();
form.AddField("loginUser", username);
form.AddField("loginPass", password);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/RealmRift/Login.php", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
resultCallback(false); // Login failed
}
else
{
if (www.downloadHandler.text.Contains("success"))
{
Debug.Log("Login successful");
resultCallback(true); // Login succeeded
}
else
{
Debug.LogError("Login failed: " + www.downloadHandler.text);
resultCallback(false); // Login failed
}
}
}
}
public IEnumerator Register(string username, string password, Action<bool, string> resultCallback)
{
WWWForm form = new WWWForm();
form.AddField("loginUser", username);
form.AddField("loginPass", password);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/RealmRift/Register.php", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.LogError(www.error);
resultCallback(false, www.error); // Registration failed
}
else
{
if (www.downloadHandler.text.Contains("New record created successfully"))
{
Debug.Log("Registration successful");
resultCallback(true, "Registration successful"); // Registration succeeded
}
else
{
Debug.LogError("Registration failed: " + www.downloadHandler.text);
resultCallback(false, www.downloadHandler.text); // Registration failed
}
}
}
}
}
Login Script: This script manages the login process in Unity. It sends user credentials to the server for verification and loads the next scene upon successful login.
User Registration Script: Similar to the login script, this script manages user registration. It sends new user data to the server for validation and displays error messages if registration fails.
In addition to setting up the login system, we've made progress on other fronts:
Realm Maps: Both Light and Dark realm maps are complete, we have added skyboxes to two both our realms to distinguish the map.
UI Updates: Basic UI elements have been added , such as the number of machine parts to be fixed and machine parts collected by players. This information is the key objective for Light realm players. Additionally, a text field has been added to dynamically display the current realm the player is in, updating accordingly when the player switches realms.
Future Plans: We're working on enhancing the UI aesthetics and planning to implement a start screen post login. Additionally, we aim to integrate a shop feature into the start screen, providing players with exciting customization options and in game rewards.
Stay tuned for more updates on Realm Rift's development journey.
Thank you for reading!
Shashank Kumar Sukumar Singh