Username/Password verification system in Unity
Objective: Create a Username/Password verification system using Unity UI.
To create a login/Create Account system, we will need four input fields, four buttons, and on debug text. After creating all of the objects, create two more empty objects. One to hold the login objects and one to hold the create account objects.
I will say a disclaimer, this is for learning on how to use UI and script together to compare text. I would say if you want to create a login system for users, look into more encrypted ways of doing this.
Next change the text on the buttons to match the respective section it is in.
Create a C# script and grab the TextMeshPro library.
Now we need variables for 9 objects, one for the debug text, another 4 for input fields, 2 for the parent objects we created, and 2 strings to hold the created username and password. In start we can clear out the debug text.
We will start with changing between the Login page and Create Account page. We will see if the createAccoutPage is disable, if it is activate it and deactivate the login page. We will do the opposite for going back to the login page.
Back in Unity, drop the script onto the object that will hold the script. You can drop in all the objects onto the script.
Now select the button that will swap pages, in the OnClick() event section add a new event and drop in this object and select the ChangePage method.
Now for creating an account. We will need to create a public method that will attach to a button. In this method we will need to see if the input in input field is equal to any username. If there is a username already, let the user know with the debug text and return so it wont continue on in the method.
Now to make sure there is at least more than 3 characters for username and password.
If all is good set the username and password and call the ChangePage method.
Now we can create an account.
Next to login. We will create a new method. In here we will first check to see if the username and password entered is not the same as the one created. If they don’t match we will change debug text to let them know and clear out the input fields. As well as return to stop the script from going on any farther.
Next is to see if the createduser and createdpass strings are null, if it is let the user know and clear out the input fields again.
The last check that will need to be done is to see if the createduser and createdpass are set as nothing.
If the login passes all these check we will let the user know they were able to log in.
With that we can verify username and passwords while also creating one account.
Thank you for reading! Have a wonderful day!