简体   繁体   English

如何在Unity中创建登录窗口

[英]How to make a login window in Unity

I am new at C# and can't get my username and password program to work. 我是C#的新手,无法使用我的用户名和密码程序。

I need to get the inputs from the user and check it against the account details which I've stored in a dictionary. 我需要从用户那里获取输入,并根据我存储在字典中的帐户详细信息进行检查。

Note that Console.WriteLine does not work in Unity. 请注意, Console.WriteLine在Unity中不起作用。 You have to use Debug.Log . 您必须使用Debug.Log

I am new at C# and can't get my username and password program to work. 我是C#的新手,无法使用我的用户名和密码程序。

1 .Create two InputFileds . 1.创建两个InputFileds One for username and one for password . 一个用于用户名 ,一个用于密码

You can do that by going to GameObject --> UI --> Input Field 您可以通过转到GameObject - > UI - > 输入字段来实现

2 .Create the Submit Button . 2.创建提交Button

You can do that by going to GameObject --> UI --> Button 您可以通过转到GameObject - > UI - > Button来实现

3 .Subscribe to the submit Button onClick event so that when it it clicked, the adminDetails() function will be called. 3.订阅提交Button onClick事件,这样当它点击它时,将adminDetails()函数。

That's it. 而已。 You can get the input with InputField.text . 您可以使用InputField.text获取输入。

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AdminLogin : MonoBehaviour
{

    public InputField userNameField;
    public InputField passwordField;
    public Button loginButton;

    void Start()
    {
        //Subscribe to onClick event
        loginButton.onClick.AddListener(adminDetails);
    }

    Dictionary<int, string> staffDetails = new Dictionary<int, string>
    {
        {101,"femi1998" },
        {102,"kwaks1999" },
        {103,"eman1999" }
    };


    public void adminDetails()
    {
        //Get Username from Input then convert it to int
        int userName = Convert.ToInt32(userNameField.text);
        //Get Password from Input 
        string password = passwordField.text;

        string foundPassword;
        if (staffDetails.TryGetValue(userName, out foundPassword) && (foundPassword == password))
        {
            Debug.Log("User authenticated");
        }
        else
        {
            Debug.Log("Invalid password");
        }
    }
}

Don't forget to drag both created InputFields and the Button to the userNameField , passwordField and loginButton slots in the Editor. 不要忘记将创建的InputFieldsButton拖到编辑器中的userNameFieldpasswordFieldloginButton插槽中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM