当前位置:   article > 正文

Unity实现账号登录,注册功能_unity画布中账号

unity画布中账号

制作了用户登录界面

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

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

默认账号:YSQS/YSQS1

密码:admin/admin1

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

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

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

GameState类

  1. using System;
  2. using Framework.L;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. public class GameState : MonoBehaviour
  7. {
  8. [SerializeField] public Text ID;//账号
  9. [SerializeField] public InputField Cryptogram;//密码
  10. [SerializeField] public Button Login;//登录
  11. [SerializeField] public Button Enroll;//注册
  12. [SerializeField] public GameObject EnrollUI;//注册UI
  13. [SerializeField] public Button ESC;//注册UI关闭
  14. [SerializeField] public InputField InvitationCode;//邀请码
  15. [SerializeField] public Text EnrollID;//注册账号
  16. [SerializeField] public InputField EnrollCryptogram;//注册密码
  17. [SerializeField] public Button Confirm_btn;//注册确定
  18. [SerializeField] public GameObject HintUI;//弹窗
  19. [SerializeField] public Button ESCBUT;
  20. [SerializeField] public Text Time;
  21. /// <summary>
  22. /// 用户登录
  23. /// </summary>
  24. void UserDataRegister()
  25. {
  26. string id = ID.text;
  27. string cryptogram = Cryptogram.text;
  28. if (ID.text != "")
  29. {
  30. if (Cryptogram.text != "")
  31. {
  32. if (UserData.DicAppDataItems.ContainsKey(id))
  33. {
  34. if ( UserData.DicAppDataItems.ContainsValue(cryptogram)&&UserData.DicAppDataItems[id]==cryptogram)
  35. {
  36. User.PopUpWindows(HintUI,true,"登录成功");
  37. }
  38. else
  39. {
  40. User.PopUpWindows(HintUI,true,"密码输入错误!!!请重新输入");
  41. }
  42. }
  43. else
  44. {
  45. User.PopUpWindows(HintUI,true,"该账号不存在,请先注册账号");
  46. }
  47. }
  48. else
  49. {
  50. User.PopUpWindows(HintUI,true,"请检查密码");
  51. }
  52. }
  53. else
  54. {
  55. User.PopUpWindows(HintUI,true,"账号不能为空");
  56. }
  57. }
  58. /// <summary>
  59. /// 用户注册界面
  60. /// </summary>
  61. void UserEnroll()
  62. {
  63. string id = EnrollID.text;
  64. string cryptogram = EnrollCryptogram.text;
  65. if (InvitationCode.text == "LPH")
  66. {
  67. if (EnrollID.text!="")
  68. {
  69. if (EnrollCryptogram.text!="")
  70. {
  71. User.AddData(id,cryptogram);
  72. User.PopUpWindows(HintUI,true,"注册成功");
  73. #if UNITY_EDITOR
  74. UnityEditor.EditorApplication.isPlaying = false;
  75. #endif
  76. Application.Quit();
  77. }
  78. else
  79. {
  80. User.PopUpWindows(HintUI,true,"密码不能为空");
  81. }
  82. }
  83. else
  84. {
  85. User.PopUpWindows(HintUI,true,"账号不能为空");
  86. }
  87. }
  88. else
  89. {
  90. User.PopUpWindows(HintUI,true,"邀请码错误");
  91. Debug.Log("邀请码错误");
  92. }
  93. }
  94. private void Start()
  95. {
  96. Login.onClick.AddListener(UserDataRegister);
  97. Enroll.onClick.AddListener(delegate
  98. {
  99. EnrollUI.gameObject.SetActive(true);
  100. });
  101. ESC.onClick.AddListener(delegate
  102. {
  103. EnrollUI.gameObject.SetActive(false);
  104. });
  105. Confirm_btn.onClick.AddListener(UserEnroll);
  106. ESCBUT.onClick.AddListener(delegate
  107. {
  108. #if UNITY_EDITOR
  109. UnityEditor.EditorApplication.isPlaying = false;
  110. #endif
  111. Application.Quit();
  112. });
  113. }
  114. private void Awake()
  115. {
  116. User.UsersLoginData();
  117. }
  118. private void Update()
  119. {
  120. Time.text = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日" +
  121. DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
  122. }
  123. }
啊,忘了说了还有一个本地时间显示

 

  1. Time.text = DateTime.Now.Year.ToString() + "年" + DateTime.Now.Month.ToString() + "月" + DateTime.Now.Day.ToString() + "日" +
  2. DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();
User类
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using BestHTTP.JSON.LitJson;
  6. using DG.Tweening;
  7. using Framework.L;
  8. using UnityEditor.Timeline.Actions;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. public class User
  12. {
  13. #region 登录
  14. /// <summary>
  15. /// 用户信息集合存储
  16. /// </summary>
  17. public static Root _Root;
  18. /// <summary>
  19. /// 将用户信息存入字典
  20. /// 利用字典来进行判断用户信息
  21. /// </summary>
  22. public static void UsersLoginData()
  23. {
  24. JsonReader js = new JsonReader(UserData.streamreader);//转换成json数据
  25. Root root = JsonMapper.ToObject<Root>(js);
  26. Dictionary<string, string> dicdataitems = new Dictionary<string, string>();//存入字典
  27. for (int i = 0; i < root.Data.Count; i++)
  28. {
  29. DataItem dataItem = new DataItem();
  30. dataItem.ID = root.Data[i].ID;
  31. dataItem.Cryptogram = root.Data[i].Cryptogram;
  32. dicdataitems.Add(dataItem.ID,dataItem.Cryptogram);
  33. }
  34. _Root = root;
  35. UserData.DicAppDataItems = dicdataitems;
  36. UserData.streamreader.Close();//关闭文件
  37. UserData.streamreader.Dispose();//释放内存
  38. }
  39. /// <summary>
  40. /// 判断用户信息"已弃用"
  41. /// </summary>
  42. /// <param name="id">账号</param>
  43. /// <param name="cryptogram">密码</param>
  44. public static void UsersLogin(string id,string cryptogram)
  45. {
  46. if (id != "")
  47. {
  48. if ( UserData.DicAppDataItems.ContainsKey(id))
  49. {
  50. if ( UserData.DicAppDataItems.ContainsValue(cryptogram)&&UserData.DicAppDataItems[id]==cryptogram)
  51. {
  52. Debug.Log("登录成功");
  53. }
  54. else
  55. {
  56. Debug.Log("密码输入错误!!!请重新输入");
  57. }
  58. }
  59. else
  60. {
  61. Debug.Log("该账号不存在,请先注册账号");
  62. }
  63. }
  64. else
  65. {
  66. Debug.Log("账号不能为空");
  67. }
  68. }
  69. #endregion
  70. #region 注册
  71. /// <summary>
  72. /// 增加用户数据
  73. /// </summary>
  74. /// <param name="id">账号</param>
  75. /// <param name="cryptogram">密码</param>
  76. public static void AddData(string id, string cryptogram)
  77. {
  78. DataItem dataItem = new DataItem();
  79. dataItem.ID = id;
  80. dataItem.Cryptogram = cryptogram;
  81. _Root.Data.Add(dataItem);
  82. //注册内容控制台显示
  83. string jsonData = JsonMapper.ToJson(_Root); //将注册信息转换为Json字符串
  84. Debug.Log("内容 " + jsonData);
  85. //存入Json
  86. File.WriteAllText(UserData.path, JsonMapper.ToJson(_Root));
  87. Debug.Log("注册成功");
  88. }
  89. #endregion
  90. #region 弹窗
  91. // 弹窗提示渐隐动画
  92. public static void PopUpWindows(GameObject gameObject, bool state,string text)
  93. {
  94. gameObject.gameObject.SetActive(state);
  95. Sequence popupWindows = DOTween.Sequence();
  96. Sequence popupWindows1 = DOTween.Sequence();
  97. gameObject.transform.GetChild(0).GetComponent<Text>().text = text;
  98. popupWindows1.Append(gameObject.transform.Find("Text").GetComponent<Text>().DOFade(1, 1));
  99. popupWindows.Append(gameObject.transform.GetComponent<Image>().DOFade(1, 1));
  100. popupWindows.Append(gameObject.transform.GetComponent<Image>().DOFade(0, 1F));
  101. popupWindows1.Append(gameObject.transform.Find("Text").GetComponent<Text>().DOFade(0, 1F));
  102. float timeCount = 10f;
  103. DOTween.To(() => timeCount, a => timeCount = a, 1f, 2f).OnComplete(() =>
  104. {
  105. //延时后的操作
  106. gameObject.gameObject.SetActive(false);
  107. });
  108. }
  109. #endregion
  110. }
UserData类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace Framework.L
  6. {
  7. public class UserData
  8. {
  9. /// <summary>
  10. /// 包含所有用户信息的 字典
  11. /// </summary>
  12. public static Dictionary<string, string> DicAppDataItems = new Dictionary<string, string>();
  13. /// <summary>
  14. /// 用户信息路径
  15. /// </summary>
  16. public static string path = Application.streamingAssetsPath + "/Userinfo.json";
  17. /// <summary>
  18. /// 读取数据,转换成数据流
  19. /// </summary>
  20. public static readonly StreamReader streamreader =
  21. new StreamReader(path);
  22. }
  23. [Serializable]
  24. public class DataItem
  25. {
  26. /// <summary>
  27. /// 账号
  28. /// </summary>
  29. public string ID { get; set; }
  30. /// <summary>
  31. /// 密码
  32. /// </summary>
  33. public string Cryptogram { get; set; }
  34. }
  35. public class Root
  36. {
  37. /// <summary>
  38. /// 用户信息集合
  39. /// </summary>
  40. public List <DataItem > Data { get; set; }
  41. }
  42. }

其实想加一个天气预报的,但API.....哎,我加油吧,这代码越看越臭.....哎

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/99839
推荐阅读
相关标签
  

闽ICP备14008679号