通过XML文件保存账号密码,存储到本地,不连接数据库的简易登录系统。
1.创建一个XML文件,设置一个初始的账号密码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public void Creat() { localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml" ; if (!File.Exists(localPath)) { XmlDocument xml = new XmlDocument(); XmlDeclaration xmldecl = xml.CreateXmlDeclaration( "1.0" , "UTF-8" , "" ); //设置xml文件编码格式为UTF-8 XmlElement root = xml.CreateElement( "Data" ); //创建根节点 XmlElement info = xml.CreateElement( "Info" ); //创建子节点 info.SetAttribute( "账号" , "admin" ); //创建子节点属性名和属性值 info.SetAttribute( "密码" , "123123" ); root.AppendChild(info); //将子节点按照创建顺序,添加到xml xml.AppendChild(root); xml.Save(localPath); //保存xml到路径位置 Debug.Log( "创建XML成功!" ); } } |
2.读取XML文件,将账号密码存储到列表数组中。每次读取的时候需要把index的值设置成0,重新录入账号密码,不然会出现一个账号密码保存两次的问题。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
void ReadXML() { localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml" ; if (File.Exists(localPath)) { XmlDocument xml = new XmlDocument(); xml.Load(localPath); //加载xml文件 XmlNodeList nodeList = xml.SelectSingleNode( "Data" ).ChildNodes; MimaZu = nodeList.Count; foreach (XmlElement xe in nodeList) { //遍历所以子节点 if (xe.Name == "Info" ) { Debug.Log(xe.GetAttribute( "账号" )); //获取Name属性值 Debug.Log(xe.GetAttribute( "密码" )); messages[index].vs.Add(xe.GetAttribute( "账号" )); messages[index].vs.Add(xe.GetAttribute( "密码" )); index += 1; //messages.Add(new string[] { xe.GetAttribute("账号"), xe.GetAttribute("密码") }); //Debug.Log(vs); } } Debug.Log( "读取XML成功!" + xml.OuterXml); } } |
3.列表数组的创建
1
2
3
4
5
6
7
|
public Message[] messages; //这个放在外面 [System.Serializable] public class Message { public List< string > vs = new List< string >(); } |
4.添加账号密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public void AddXML( string zhanghao, string mima) { localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml" ; if (File.Exists(localPath)) { XmlDocument xml = new XmlDocument(); xml.Load(localPath); //加载xml文件 XmlNode root = xml.SelectSingleNode( "Data" ); //获取根节点 XmlElement info = xml.CreateElement( "Info" ); //创建新的子节点 info.SetAttribute( "账号" , zhanghao); //创建新子节点属性名和属性值 info.SetAttribute( "密码" , mima); root.AppendChild(info); //将子节点按照创建顺序,添加到xml xml.AppendChild(root); xml.Save(localPath); //保存xml到路径位置 Debug.Log( "添加XML成功!" ); } } |
5.判断账号密码是否正确,登录的时候判断IsLogin的值,如果IsLogin的值为false就是登录失败,如果值为True就是登录成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public bool IsLogin; public Message[] messages; /// <summary> /// 账号密码个数 /// </summary> /// <summary> /// 判断账号密码是否正确 /// </summary> public void Login() { for ( int i = 0; i < MimaZu; i++) { int index = i; if (ZhangHao.text == messages[index].vs[0] && Mima.text == messages[index].vs[1]) { if (messages[index].vs[0] == null ) return ; if (messages[index].vs[1] == null ) return ; IsLogin = true ; return ; } else if (ZhangHao.text != messages[index].vs[0] && Mima.text != messages[index].vs[1]) { IsLogin = false ; } } } |
到此这篇关于Unity利用XML制作一个简易的登录系统的文章就介绍到这了,更多相关Unity XML登录系统内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Chinesettt/article/details/123543207