Unity实现账号登录,注册功能

这篇具有很好参考价值的文章主要介绍了Unity实现账号登录,注册功能。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

制作了用户登录界面Unity实现账号登录,注册功能

 关于弹窗使用了DOTween插件,实现渐隐渐显效果。

关于账号使用了本地Json读取,

Unity实现账号登录,注册功能

默认账号:YSQS/YSQS1

密码:admin/admin1

注册功能其实应该重构的因为有二次读流的问题存在。

账号注册加入了邀请码(其实就一个if)

Unity实现账号登录,注册功能

 接下来就是我那臭死了的源码。

GameState类

using System;
using Framework.L;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameState : MonoBehaviour
{
   [SerializeField] public Text ID;//账号

   [SerializeField] public InputField Cryptogram;//密码

   [SerializeField] public Button Login;//登录

   [SerializeField] public Button Enroll;//注册
   
   [SerializeField] public GameObject EnrollUI;//注册UI

   [SerializeField] public Button ESC;//注册UI关闭

   [SerializeField] public InputField InvitationCode;//邀请码
   
   [SerializeField] public Text EnrollID;//注册账号
   
   [SerializeField] public InputField EnrollCryptogram;//注册密码

   [SerializeField] public Button Confirm_btn;//注册确定

   [SerializeField] public GameObject HintUI;//弹窗
   
   [SerializeField] public Button ESCBUT;
   
   [SerializeField] public Text Time;
   /// <summary>
   /// 用户登录
   /// </summary>
   void UserDataRegister()
   {
      string id = ID.text;
      string cryptogram = Cryptogram.text;
      if (ID.text != "")
      {
         if (Cryptogram.text != "")
         {
            if (UserData.DicAppDataItems.ContainsKey(id))
            {
               
               if ( UserData.DicAppDataItems.ContainsValue(cryptogram)&&UserData.DicAppDataItems[id]==cryptogram)
               {   
                  User.PopUpWindows(HintUI,true,"登录成功");
               }
               else
               {
                  User.PopUpWindows(HintUI,true,"密码输入错误!!!请重新输入");
               }
            }
            else
            {
               User.PopUpWindows(HintUI,true,"该账号不存在,请先注册账号");
            }
         }
         else
         {
            User.PopUpWindows(HintUI,true,"请检查密码");
         }
      }
      else
      {
         User.PopUpWindows(HintUI,true,"账号不能为空");
      }
     
   }
   /// <summary>
   /// 用户注册界面
   /// </summary>
   void UserEnroll()
   {
      string id = EnrollID.text;
      string cryptogram = EnrollCryptogram.text;
      if (InvitationCode.text == "LPH")
      {
         if (EnrollID.text!="")
         {
            if (EnrollCryptogram.text!="")
            {
               User.AddData(id,cryptogram);
               User.PopUpWindows(HintUI,true,"注册成功");
#if UNITY_EDITOR
               UnityEditor.EditorApplication.isPlaying = false;
#endif
               Application.Quit();
            }
            else
            {
               User.PopUpWindows(HintUI,true,"密码不能为空");
            }
         }
         else
         {
            User.PopUpWindows(HintUI,true,"账号不能为空");
         }
        
      }
      else
      {
         User.PopUpWindows(HintUI,true,"邀请码错误");
         Debug.Log("邀请码错误");
      }
   }
   private void Start()
   {
      Login.onClick.AddListener(UserDataRegister);
      Enroll.onClick.AddListener(delegate
      {
         EnrollUI.gameObject.SetActive(true);
      });
      ESC.onClick.AddListener(delegate
      {
         EnrollUI.gameObject.SetActive(false);
      });
      Confirm_btn.onClick.AddListener(UserEnroll);
      ESCBUT.onClick.AddListener(delegate
      {
#if UNITY_EDITOR
         UnityEditor.EditorApplication.isPlaying = false;
#endif
         Application.Quit();
      });
   }
   private void Awake()
   {
      User.UsersLoginData();
   }

   private void Update()
   {
      Time.text = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日" +
                  DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
   }
}
啊,忘了说了还有一个本地时间显示

Unity实现账号登录,注册功能

 

Time.text = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日" +
                  DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
User类
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using BestHTTP.JSON.LitJson;
using DG.Tweening;
using Framework.L;
using UnityEditor.Timeline.Actions;
using UnityEngine;
using UnityEngine.UI;

public class User
{  
    
    #region 登录
    /// <summary>
    /// 用户信息集合存储
    /// </summary>
    public static Root _Root;
    /// <summary>
    /// 将用户信息存入字典
    /// 利用字典来进行判断用户信息
    /// </summary>
    public static void UsersLoginData()
    {
        JsonReader js = new JsonReader(UserData.streamreader);//转换成json数据
        Root root = JsonMapper.ToObject<Root>(js);
        Dictionary<string, string> dicdataitems = new Dictionary<string, string>();//存入字典
        
        for (int i = 0; i < root.Data.Count; i++)
        {
            DataItem dataItem = new DataItem();
            dataItem.ID = root.Data[i].ID;
            dataItem.Cryptogram = root.Data[i].Cryptogram;
            dicdataitems.Add(dataItem.ID,dataItem.Cryptogram);
        }
        _Root = root;
        UserData.DicAppDataItems = dicdataitems;
        UserData.streamreader.Close();//关闭文件
        UserData.streamreader.Dispose();//释放内存
    }
    
    
    /// <summary>
    /// 判断用户信息"已弃用"
    /// </summary>
    /// <param name="id">账号</param>
    /// <param name="cryptogram">密码</param>
    public static void UsersLogin(string id,string cryptogram)
    {
        if (id != "")
        {
            if ( UserData.DicAppDataItems.ContainsKey(id))
            {
                if ( UserData.DicAppDataItems.ContainsValue(cryptogram)&&UserData.DicAppDataItems[id]==cryptogram)
                {   
                    Debug.Log("登录成功");
                }
                else
                {
                    Debug.Log("密码输入错误!!!请重新输入");
                }
            }
            else
            {
                Debug.Log("该账号不存在,请先注册账号");
            }
        }
        else
        {
            Debug.Log("账号不能为空");
        }
    }
    #endregion

    #region 注册

    /// <summary>
    /// 增加用户数据
    /// </summary>
    /// <param name="id">账号</param>
    /// <param name="cryptogram">密码</param>
    public static void AddData(string id, string cryptogram)
    {
        DataItem dataItem = new DataItem();
        dataItem.ID = id;
        dataItem.Cryptogram = cryptogram;
        _Root.Data.Add(dataItem);
        //注册内容控制台显示
        string jsonData = JsonMapper.ToJson(_Root); //将注册信息转换为Json字符串
        Debug.Log("内容 " + jsonData);
        //存入Json
        File.WriteAllText(UserData.path, JsonMapper.ToJson(_Root));
        Debug.Log("注册成功");

    }

    #endregion
   
    #region 弹窗
   
    // 弹窗提示渐隐动画
    public static void PopUpWindows(GameObject gameObject, bool state,string text)
    {
        gameObject.gameObject.SetActive(state);
        Sequence popupWindows = DOTween.Sequence();
        Sequence popupWindows1 = DOTween.Sequence();
        gameObject.transform.GetChild(0).GetComponent<Text>().text = text;
        popupWindows1.Append(gameObject.transform.Find("Text").GetComponent<Text>().DOFade(1, 1));
        popupWindows.Append(gameObject.transform.GetComponent<Image>().DOFade(1, 1));
        popupWindows.Append(gameObject.transform.GetComponent<Image>().DOFade(0, 1F));
        popupWindows1.Append(gameObject.transform.Find("Text").GetComponent<Text>().DOFade(0, 1F));
        float timeCount = 10f;
        DOTween.To(() => timeCount, a => timeCount = a, 1f, 2f).OnComplete(() =>
        {
            //延时后的操作
            gameObject.gameObject.SetActive(false);
        
        });
    }
    #endregion
    
}

UserData类
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Framework.L
{   
    public class UserData
    {
        /// <summary>
        /// 包含所有用户信息的 字典
        /// </summary>
        public static Dictionary<string, string> DicAppDataItems = new Dictionary<string, string>();
        /// <summary>
        /// 用户信息路径
        /// </summary>
        public static string path = Application.streamingAssetsPath + "/Userinfo.json";
        /// <summary>
        /// 读取数据,转换成数据流
        /// </summary>
        public static readonly StreamReader streamreader =
            new StreamReader(path); 
    }
    [Serializable]
    public class DataItem
    {
        /// <summary>
        /// 账号
        /// </summary>
        public string ID { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string Cryptogram { get; set; }
        
    }
    public class Root
    {
        /// <summary>
        /// 用户信息集合
        /// </summary>
        public List <DataItem > Data { get; set; }
    }
}

其实想加一个天气预报的,但API.....哎,我加油吧,这代码越看越臭.....哎文章来源地址https://www.toymoban.com/news/detail-510930.html

到了这里,关于Unity实现账号登录,注册功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包