Username/Password Verification System in Unity

Creating a secure and efficient username/password verification system is crucial for any application that deals with user accounts. Unity, often used for game development, provides a variety of UI components that make building a login system relatively straightforward. In this article, we’ll go step-by-step through the process of creating a simple username/password verification system in Unity using C#.

Requirements - Unity Editor (Version 2019.4 or later recommended) - Basic understanding of C# programming and Unity’s UI system

Getting Started

Setting Up the Unity Project 1. Open Unity Hub and create a new 2D or 3D project. 2. Import the TextMesh Pro package by going to `Window -> TextMesh Pro -> Import TMP Essential Resources`.

Creating the UI 1. Right-click on the Hierarchy panel and create an empty GameObject. Name it `UserAccountManager`. 2. Create two panels: One for login and another for account creation. Name them `LoginPanel` and `CreatePanel`. 3. Inside each panel, add the TextMesh Pro InputFields for username and password and two buttons for the respective actions (Login/Create).

Writing the Code

Create a new C# script called UserAccount and open it in your preferred editor. Paste the following code:

using UnityEngine;

using UnityEngine.UI;

using TMPro;

public class UserAccount : MonoBehaviour

{

    [SerializeField] private TMP_InputField[] _userInputFields;

    [SerializeField] private Button[] _buttons;

    [SerializeField] private GameObject _login;

    [SerializeField] private GameObject _create;

    [SerializeField] private TMP_Text _debugText;

    [SerializeField] private string _username;

    [SerializeField] private string _password;

    private void Start()

    {

        _debugText.text = "";

    }

    public void LoginAcct()

    {

        if (string.IsNullOrEmpty(_userInputFields[2].text) || string.IsNullOrEmpty(_userInputFields[3].text))

        {

            _debugText.text = "Input fields cannot be empty";

            return;

        }

        if (string.IsNullOrEmpty(_username) || string.IsNullOrEmpty(_password))

        {

            _debugText.text = "No Account Created, try again";

            return;

        }

        if (_userInputFields[2].text == _username && _userInputFields[3].text == _password)

        {

            _debugText.text = "Login Success";

            return;

        }

        else

        {

            _debugText.text = "Wrong Username or Password, try again";

            return;

        }

    }

    public void CreateAcct()

    {

        if (_userInputFields[0].text.Length > 3 && _userInputFields[1].text.Length > 3)

        {

            _username = _userInputFields[0].text;

            _password = _userInputFields[1].text;

            _debugText.text = "Success Account Created";

            OpenLoginAcctMenu();

        }

        else

        {

            _debugText.text = "Username and Password must be at least 4 characters";

        }

    }

    public void OpenCreateAcctMenu()

    {

        _login.SetActive(false);

        _create.SetActive(true);

    }

    public void OpenLoginAcctMenu()

    {

        _login.SetActive(true);

        _create.SetActive(false);

    }

}

Connecting UI to Code

1. Attach the `UserAccount` script to the `UserAccountManager` GameObject. 2. Assign the UI elements to their respective fields in the Inspector.

Testing the Application

1. Run the Unity scene and open the Create Account panel. 2. Enter a username and password (at least 4 characters) and click the “Create” button. 3. You should see the message “Success Account Created”. 4. Now, go to the Login panel, enter the same username and password, and click “Login”. 5. You should see the message “Login Success”.

Conclusion

This article provides a basic template for a username/password verification system in Unity. This example is quite simple and not suitable for production, as it doesn’t incorporate encryption or database storage, but it serves as a foundational step towards creating a more comprehensive system.

Leave a comment

Log in with itch.io to leave a comment.