banner
YZ

周周的Wiki

种一棵树最好的时间是十年前,其次是现在。
zhihu
github
csdn

Implementation of Login and Registration Functionality Based on Unity + SqlServer

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.

  1. I built a login panel in Unity as shown in the image below:
    Insert image description here
    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).
  2. Built the registration panel:
    Insert image description here
    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.
  3. Preparation before Unity connects to the database - referencing the database DLL files:
    Create a folder named Plugins and add 4 DLL files: Insert image description here
    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:
    Insert image description here
    I encountered a problem where importing DLL files in Unity 2018.3.6f1 resulted in an error:
    Insert image description here
    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, Insert image description here
    Select the corresponding version, and restarting Unity will resolve the issue. Now we can start writing scripts to implement the functionality.
  4. Implementation of the login functionality:
    (1) First, design the content of the database:
    Insert image description here
    (2) Code + visual implementation:
    Create a script named userLogin: ``
    First, we add some references:

Insert image description here
(If the DLL files were not added earlier, they cannot be referenced here, and naturally, the database cannot be connected below)
Insert image description here
Define two public inputField components and one static text
image
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:
Insert image description here
Insert image description here
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.
Insert image description here

(4) Test the login functionality:
No input:
Insert image description here
Account does not exist in the database:
Insert image description here
Incorrect password:
Insert image description here
Account and password match successfully logged in

Insert image description here
5. Implementation of the registration functionality:
(1) Directly add a mouse click event to the registration button, setting the bool setactive property:
Insert image description here
(2) The implementation of registration is to add data to the database:
Insert image description here
Insert image description here
Insert image description here
Insert image description here
(3) Test the registration functionality:
When registering the same account:
Insert image description here
Successfully logged in with a new account:
Insert image description here
Insert image description here
Alright, with that, my small login and registration functionality is finally complete.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.