首先定义一个数据类Account:
//*****************************************************类脚本************************************************
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//数据类
public class Account{
public string Name; //用户名public string Password; //密码
}
public class AccountData
{
public List<Account> accountModel = new List<Account>(); //用户数据链表
}
public static class SavaData
{
public static AccountData saveList; //写一个静态类,作用类似于new了一下上面AccountData里的链表,在别的脚本里可 以直接调用(个人理解)
}
//*************************************************登录注册脚本*************************************************
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.IO;
using LitJson;
using UnityEditor;
public class Login : MonoBehaviour {
//..........................定义Login页面的输入框.......................
private InputField nameInputField, passwordInputField;
//..........................定义Register页面的输入框...................
private InputField nameField, passwordField, secondPassword;
//..........................Message面板的文本.....................
private Text messageText;
//..........................定义了三个页面Panel.......................
public Transform LoginPanel;
public Transform RegisterPanel;
public Transform MessagePanel;
//在Awake()里最先读取文件里的数据并解读到链表里
void Awake()
{
GetData();
}
public void GetData()
{
TextAsset a = Resources.Load<TextAsset>("Data/account"); //读取文件
if (!a)
{
return;
}
string json = a.text;
SavaData.saveList = JsonMapper.ToObject<AccountData>(json); //把读出来的文件文本转成对象存到链表里
}
void Start ()
{
//......................Login页面定义的输入框的初始化..........................
nameInputField = GameObject.Find("BG").transform.FindChild("Login").GetChild(2).GetComponent<InputField>();
passwordInputField = GameObject.Find("BG").transform.FindChild("Login").GetChild(3).GetComponent<InputField>();
//......................Register页面定义的输入框的初始化.......................
nameField = GameObject.Find("BG").transform.FindChild("Register").GetChild(3).GetComponent<InputField>();
passwordField = GameObject.Find("BG").transform.FindChild("Register").GetChild(4).GetComponent<InputField>();
secondPassword = GameObject.Find("BG").transform.FindChild("Register").GetChild(5).GetComponent<InputField>();
//......................MessagePanel页面定义的文本的初始化....................
messageText = GameObject.Find("BG").transform.FindChild("messagePanel").GetChild(0).GetComponent<Text>();
}
void Update ()
{
}
//登录按钮(不要忘记绑定)
public void LoginClick(){
string name = nameInputField.text;
string password = passwordField.text;
if (name == "") //如果用户名为空
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "用户名不存在!";
return;
}
if (password == "") //如果密码为空
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "密码不存在!";
return;
}
//如果比对失败
bool isRight = false;
for (int i = 0; i < SavaData.saveList.accountModel.Count; i++)
{
if (SavaData.saveList.accountModel[i].Name == name && SavaData.saveList.accountModel[i].Password == password)
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "登录成功!!!";
isRight = true;
break;
}
}
if (!isRight)
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "登录失败!!!";
return;
}
}
//注册按钮(不要忘记绑定)
public void RegisterClick()
{
string name = nameField.text;
string password = passwordField.text;
string SecondPassword = secondPassword.text;
if (name == "") //如果用户名为空
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "用户名不存在";
return;
}
if (password == "") //如果密码为空
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "密码不存在";
return;
}
if (SecondPassword == "") //如果确认密码为空
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "确认密码不存在";
return;
}
Account a = new Account(); //new一个Account类里的对象并赋值
a.Name = name;
a.Password = password;
SaveDataAccount(a); //把注册账号方法单独封装成一个,并用传参的方式进行数据传递
}
//存注册的账号
public void SaveDataAccount(Account use)
{
//1.创建一个account文件夹
string filePath = Application.dataPath + @"/Resources/Data/account.txt";
//比对用户名,然后跳出用户名已存在
if (!File.Exists(filePath)) //如果文件不存在
{
SavaData.saveList = new AccountData();
SavaData.saveList.accountModel.Add(use);
}
else //一开始account就存在情况
{
bool isExists = false; //假设一开始不存在
for (int i = 0; i < SavaData.saveList.accountModel.Count; i++) //遍历链表比对链表里的用户名和传入的用户名是否一致
{
if (SavaData.saveList.accountModel[i].Name == use.Name)
{
MessagePanel.gameObject.SetActive(true);
messageText.text = "用户名已存在!!!";
isExists = true;
return; //一定要加return
}
}
if (!isExists) //一开始不存在就跟上面的if作用是一样的
{
//SavaData.saveList = new AccountData();
//把注册的账号存起来
SavaData.saveList.accountModel.Add(use);
MessagePanel.gameObject.SetActive(true);
messageText.text = "注册成功";
}
}
//把对象转成json,这里用到了数据流,由于小编没学过数据流,这里是询问的别人的方法
string json = JsonMapper.ToJson(SavaData.saveList);FileInfo file = new FileInfo(filePath);
StreamWriter stream = file.CreateText();
stream.WriteLine(json);
stream.Close();
stream.Dispose();
AssetDatabase.Refresh();
}
//以下三个方法都是界面的切换,就不加备注了(要记得绑定按钮哦!!!)
public void MessagePanelButton()
{
MessagePanel.gameObject.SetActive(false);
}
public void LoginPanelRegister()
{
LoginPanel.gameObject.SetActive(false);
RegisterPanel.gameObject.SetActive(true);
}
public void RegisterPanelReturn()
{
RegisterPanel.gameObject.SetActive(false);
LoginPanel.gameObject.SetActive(true);
}
}
刚开始一直搞不清楚,现在搞明白了才发现其实很简单就是需要多加练习,好多代码对于学习能力较差的我来说,一段时间不写一遍就会忘记!还是需要多加练习才行。
个人总结了一下:最开始写Register()里面的逻辑,最重要的是SaveDataAccount()方法里的逻辑,然后就需要写Awake()里的方法,首先需要立马把数据读出来转成对象,不然在运行中很容易出错,最后写Login()即可。好了,今天就说这么多,希望能够帮助同样是小白的程序员们。