unity 登录和注册账号

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
 
public class AccountManager : MonoBehaviour {
 
    private InputField userName;
    private InputField passWord;
    Dictionary<string, string> registerMsg;
    public Text showHintMsgText;
 
    void Awake()
    {
        userName = transform.Find("UserName/UserNameInputField").GetComponent<InputField>();
        passWord = transform.Find("PassWord/PassWordInputField").GetComponent<InputField>();     
    }
    
    void Start()
    {
        registerMsg = new Dictionary<string, string>();
        showHintMsgText.text = string.Empty;
    }
 
    public void OnClickOkBtn()
    {
        if (PlayerPrefs.GetString(userName.text) != "" && PlayerPrefs.GetString(userName.text)==passWord.text)
        {
            SceneManager.LoadSceneAsync("A");
            Debug.Log("登录成功!");
        }
        else if(!PlayerPrefs.HasKey(userName.text)&&userName.text!="")
        {
            ShowHintText("该用户名不存在!");
        }
        else if(PlayerPrefs.HasKey(userName.text)&& PlayerPrefs.GetString(userName.text) != passWord.text)
        {
            ShowHintText("密码错误!");
        }
        else
        {
            ShowHintText("用户名或密码不能为空!");
        }
    }
 
    public void OnClickRegisterBtn()
    {
        if(userName.text != "" && passWord.text != "")
        {
            if (!PlayerPrefs.HasKey(userName.text))
            {
                registerMsg.Add(userName.text, passWord.text);
                DataMsgSave();
                ShowHintText("注册成功!");
                passWord.text = string.Empty;
            }
            else
            {
                ShowHintText("该账号已注册!");
            }
        }
        else
        {
            ShowHintText("账号或密码不能为空!");
        }
    }
 
    private void DataMsgSave()
    {
        foreach (KeyValuePair<string, string> pair in registerMsg)
        {
            PlayerPrefs.SetString(pair.Key, pair.Value);
            Debug.Log(pair.Key + "    " + pair.Value);
        }
    }
 
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            PlayerPrefs.DeleteAll();
            Debug.Log("信息清空!");
        }
    }
 
    private void ShowHintText(string Str)
    {
        showHintMsgText.text = Str;
        StartCoroutine("HideHintText");
    }
 
    IEnumerator HideHintText()
    {
        yield return new WaitForSeconds(1f);
        showHintMsgText.text = string.Empty;
    }
}


 ———————————————— 
版权声明:本文为CSDN博主「ff_0528」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ff_0528/article/details/70228621

发布了112 篇原创文章 · 获赞 40 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/chh19941125/article/details/100041936