Unity 实现使用邮箱登陆注册功能

有一下功能
1、账号密码本地存储
2、支持忘记密码 修改密码
3、通过邮箱验证码 注册
4、注册码有时效限制

这只是个小demo,只注重实现功能
下面是代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.UI;
using Random = System.Random;


public class Eamile : MonoBehaviour
{
    
    

    //注册界面
    public GameObject Register;
    //注册邮箱的输入框
    public InputField RegisterEamil;
    //验证码的输入框
    public InputField CodeInput;
    //密码输入框
    public InputField Password;
    //发送验证码的按钮
    public Button SendCode;
    //确认注册按钮
    public Button Confirm;
    //返回按钮
    public Button Return;
    
    //登陆界面
    public GameObject Longin;
    //登陆邮箱的输入框
    public InputField LonginEamil;
    //登陆密码的输入框
    public InputField LonginPassWord;
    //登陆按钮
    public Button LonginBut;
    //注册按钮
    public Button RegisterBtu;
    //忘记密码按钮
    public Button ForgotPasswordBut;
    
    
    //验证码
    string a = "0123456789";

    /// <summary>
    /// 随机出来的验证码
    /// </summary>
    private string code;

    /// <summary>
    /// 倒计时
    /// </summary>
    private float time;

  //是否点击了忘记密码
    private bool isForgotPassword;
    /// <summary>
    /// 存储账号
    /// </summary>
    //private Dictionary<string, string> SaveAccount = new Dictionary<string, string>();

    // Start is called before the first frame update
    void Start()
    {
    
    
        SendCode.onClick.AddListener(SendEamil);
        Confirm.onClick.AddListener(ConfirmOnClick);
        LonginBut.onClick.AddListener(LonginOnClick);
        RegisterBtu.onClick.AddListener(RegisterOnClick);
        ForgotPasswordBut.onClick.AddListener(ForgotPassword);
        Return.onClick.AddListener(ReturnOnClick );

    }

    // Update is called once per frame
    void Update()
    {
    
    
        if (time > 0)
        {
    
    
            time -= Time.deltaTime;
            if (time <= 0)
            {
    
    
                code = "";
                SendCode.enabled = true ;
            }
        }
    }

    /// <summary>
    /// 发送验证码
    /// </summary>
    public void SendEamil()
    {
    
    
        
        if (PlayerPrefs .HasKey(RegisterEamil.text)&&!isForgotPassword)
        {
    
    
            Debug.Log("账号已注册");
            return;
        }
        try
        {
    
    
            //实例化一个随机数
            Random num = new Random();

            for (int i = 0; i < 4; i++)
            {
    
    
                code = code + a.Substring(num.Next(0, a.Length), 1);
            }

            Debug.Log("验证码" + code);
            SmtpClient mailClient = new SmtpClient("smtp.qq.com");//使用的是qq邮箱,
            mailClient.EnableSsl = true;
            mailClient.Credentials = new NetworkCredential("邮箱", "邮箱的授权码");//不知道授权码的  自行百度 
            Debug.Log(RegisterEamil.text);
            //发验证码用的邮箱,注册账号的邮箱
            MailMessage message = new MailMessage(new MailAddress("邮箱"), new MailAddress(RegisterEamil.text));
            message.Body = "您的验证码为" + code + ",请在30分钟内验证,系统邮件请勿回复!";
            message.Subject = "验证码";
            mailClient.Credentials = mailClient.Credentials;
           // mailClient.Send(message);
           mailClient.SendCompleted += new SendCompletedEventHandler((sender, arg) =>
           {
    
    
               bool isResult = arg.Error == null;
               if (!isResult)
               {
    
    
                   //发送失败
                   Debug.LogError(arg.Error);
               }
               else
               {
    
    
                   //发送成功
               }
           });
            mailClient.SendAsync(message, null);
            //发送验证码 按钮失效
            SendCode.enabled = false;
            //倒计时  30分钟 1800秒
            time = 1800;
        }
        catch (Exception e)
        {
    
    
            Debug.Log("邮箱格式不正确");
        }
    }

    /// <summary>
    /// 判断验证码是否正确
    /// </summary>
    public bool  CodeBool()
    {
    
    
        if (code.Equals(CodeInput.text))
        {
    
    
            Debug.Log("验证码正确");
            return true;
        }
        else
        {
    
    
            Debug.Log("验证码错误");
            return false;
        }
    }


    /// <summary>
    /// 登陆按钮
    /// </summary>
    public void LonginOnClick()
    {
    
    
       
        //判断是否有这个邮箱账号
        if (PlayerPrefs .HasKey(LonginEamil.text))
        {
    
    
            //判断密码是否正确
            if (PlayerPrefs.GetString( LonginEamil.text).Equals( LonginPassWord.text ))
            {
    
    
                Debug.Log($"登陆成功  账号{
      
      LonginEamil.text}   密码{
      
      PlayerPrefs.GetString(LonginEamil.text)}");
                
            }
            else
            {
    
    
                Debug.Log($"邮箱或账号不正确 账号{
      
      LonginEamil.text}   密码{
      
      PlayerPrefs.GetString(LonginEamil.text)}");
            }
        }
        else
        {
    
    
            Debug.Log($"邮箱或账号不正确   账号{
      
      LonginEamil.text}   密码{
      
      PlayerPrefs.GetString(LonginEamil.text)}");
        }
    }

    /// <summary>
    /// 注册按钮
    /// </summary>
    public void RegisterOnClick()
    {
    
    
        Longin.gameObject.SetActive(false);
        Register.gameObject.SetActive(true);
        SendCode.enabled = true ;
        Debug.Log("注册账号'");
        LonginEamil.text = "";
        LonginPassWord.text = "";
        code = "";
    }

    /// <summary>
    /// 忘记密码
    /// </summary>
    public void ForgotPassword()
    {
    
    
        RegisterOnClick();
        Debug.Log("忘记密码");
        isForgotPassword = true;
    }

    /// <summary>
    /// 确认按钮
    /// </summary>
    public void ConfirmOnClick()
    {
    
    
        //验证码时间超出现在    验证码错误
        if (time <= 0&&!CodeBool())
        {
    
    
            Debug.Log("验证码已过期  请重新获取验证码");
            SendCode.enabled = true ;
            return;
        }
        //判断字典是否有这个账号   如果有(说明是要修改密码)  反之注册新的账号
        if (PlayerPrefs .HasKey(RegisterEamil.text))
        {
    
    
            //修改密码
            PlayerPrefs.SetString(RegisterEamil.text, Password.text);
            Debug.Log("修改密码成功");
            Debug.Log($"  账号{
      
      RegisterEamil.text}   密码{
      
      PlayerPrefs.GetString(RegisterEamil.text)}");
        }
        else
        {
    
    
                //注册账号
                PlayerPrefs.SetString(RegisterEamil.text, Password.text);
                Debug.Log("注册新账号成功");
                Debug.Log($"  账号{
      
      RegisterEamil.text}   密码{
      
      PlayerPrefs.GetString(RegisterEamil.text)}");

        }

        ReturnOnClick();
    }

    //返回按钮
    public void ReturnOnClick()
    {
    
    
        Longin.gameObject.SetActive(true );
        Register.gameObject.SetActive(false );
        RegisterEamil.text = "";
        Password.text = "";
        CodeInput.text = "";
    }
}

猜你喜欢

转载自blog.csdn.net/o_ojjj/article/details/120218136
今日推荐