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 registerMsg;
    public Text showHintMsgText;

    void Awake()
    {
        userName = transform.Find("UserName/UserNameInputField").GetComponent();
        passWord = transform.Find("PassWord/PassWordInputField").GetComponent();     
    }
	
    void Start()
    {
        registerMsg = new Dictionary();
        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 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;
    }
}

你可能感兴趣的:(unity 登录和注册账号)