制作了用户登录界面
关于弹窗使用了DOTween插件,实现渐隐渐显效果。
关于账号使用了本地Json读取,
默认账号:YSQS/YSQS1
密码:admin/admin1
注册功能其实应该重构的因为有二次读流的问题存在。
账号注册加入了邀请码(其实就一个if)
接下来就是我那臭死了的源码。
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();
}
}
啊,忘了说了还有一个本地时间显示
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.....哎,我加油吧,这代码越看越臭.....哎