Login and registration functionality implemented based on Unity 2018.3.6 + SQL Server 2014.
I created a simple login and registration feature for my VR system.
- I built a login panel in Unity as shown in the image below:
I added 3 Text elements (for login information prompts and account/password input prompts), 3 buttons (to mount login and registration events, exit the system), and two input fields (for entering account and password). - Built the registration panel:
Just create game objects like the login panel; the difference is that I added a confirm password input box here, but I didn't add any code logic for the confirm password, it's just decorative and optional, it can be added later when improving the functionality. - Preparation before Unity connects to the database - referencing the database DLL files:
Create a folder named Plugins and add 4 DLL files:
Note: The DLL files added for connecting different databases are different. Here we are connecting to SQL Server, and these four are sufficient. These DLLs can be found online or in your Unity installation directory:
I encountered a problem where importing DLL files in Unity 2018.3.6f1 resulted in an error:
I found a solution online, which mainly stated that the .NET version of the DLL must match the script runtime version. We need to make a change here - go to File/Build Settings/Player Settings/Other Settings/Configuration,
Select the corresponding version, and restarting Unity will resolve the issue. Now we can start writing scripts to implement the functionality. - Implementation of the login functionality:
(1) First, design the content of the database:
(2) Code + visual implementation:
Create a script named userLogin: ``
First, we add some references:
(If the DLL files were not added earlier, they cannot be referenced here, and naturally, the database cannot be connected below)
Define two public inputField components and one static text
Here, we need to get the login prompt text component at the beginning; in the view, give this Text component a label LoginMessage.
(3) Next, write a login method to connect to the database, access data, and compare it with the input data:
server="computer IP address/computer username, default 127.0.0.1 for local address, can also use localhost", database="the name of the database you created", uid=database user sa, pwd=database password, customizable. Other SQL statements can be learned independently.
(4) Test the login functionality:
No input:
Account does not exist in the database:
Incorrect password:
Account and password match successfully logged in
5. Implementation of the registration functionality:
(1) Directly add a mouse click event to the registration button, setting the bool setactive property:
(2) The implementation of registration is to add data to the database:
(3) Test the registration functionality:
When registering the same account:
Successfully logged in with a new account:
Alright, with that, my small login and registration functionality is finally complete.