游戏结束分数排名当前高能显示 (原创教程)

一般游戏结束后都会有个分数排名板。

接下来让分析这功能。1.游戏结束后显示高分排列,当前玩家分数高能显示。(如果能进入排名板)2.数据必须持久化,切换场景,关闭开启游戏都要能用。。。流程:游戏结束后,调出排名板。1.取得上次的所有排名数据保存到list泛型,如上图只有5个。2.把当前玩家数据添加到list里,然后根据分数降序再删除最后一个。(最后一名)3.删除替换更新原来的排名数据。接下来让我们用Linq To XML 来实现。

1.XML 格式,这里我使用这样的。目录为 /VC/my.xml

<?xml version="1.0" encoding="utf-8"?><VC> <Group><Item><ID>1</ID><Score>39</Score><Time>04月19日</Time></Item><Item><ID>2</ID><Score>39</Score><Time>04月19日</Time></Item><Item><ID>3</ID><Score>38</Score><Time>04月19日</Time></Item><Item><ID>4</ID><Score>29</Score><Time>04月19日</Time></Item><Item><ID>5</ID><Score>28</Score><Time>04月19日</Time></Item> </Group></VC>

2.根据XML格式新建一个model,取名GameInfoModel和XML的元素一一对应。

using UnityEngine;using System.Collections;using System;public class GameInfoModel{public int Id { set; get; }public int Score { set; get; }public string Time { set; get; }}3.编写核心代码 HelperXML类using UnityEngine;using System.Collections;using System.Xml.Linq;using System.Xml;using System.IO;using System;using System.Collections.Generic;using System.Linq;/// <summary>/// By:略知CSharp </strong></span><span style="font-family:Microsoft YaHei;font-size:14px;color:#006600;"><strong>/// </summary>public class HelperXML : MonoBehaviour{string filePath = string.Empty;void Awake(){//此属性用于返回程序的数据文件所在文件夹的路径。例如在Editor中就是Assets了。filePath = Application.dataPath + @"/VC/my.xml";}/// <summary>/// 1.遍历读取XML/// </summary>/// <returns></returns>public List<GameInfoModel> VCLoadXMLManage(){XElement root = XElement.Load(filePath);List<GameInfoModel> list = new List<GameInfoModel>();foreach (var item in root.Element("Group").Elements("Item")) //遍历XElement{list.Add(new GameInfoModel{Id = Convert.ToInt32(item.Element("ID").Value),Score = Convert.ToInt32(item.Element("Score").Value),Time = item.Element("Time").Value});}return list;}/// <summary>/// 2.删除第三级所有节点/// </summary>public void VCRemoveXmlElement(){XElement root = XElement.Load(filePath);XElement node = root.Descendants().Where(p => p.Name == "Group").Last();node.Elements().Remove();root.Save(filePath);}/// <summary>/// 3.添加第三级/// </summary>/// <param name="list"></param>public void VCAddXml(List<GameInfoModel> list){XElement root = XElement.Load(filePath);XElement node = root.Descendants().Where(p => p.Name == "Group").Last();for (int i = 0, count = list.Count; i < count; i++){XElement child = new XElement("Item",new XElement("ID", i + 1),new XElement("Score", list[i].Score),new XElement("Time", list[i].Time));node.Add(child);}root.Save(filePath);}}

4.实现主逻辑,,主要就是游戏结束后调用这个类,这里继承了上面的类。

using UnityEngine;using System.Collections;using UnityEngine.UI;using System.Collections.Generic;using System;using System.Linq;using System.Text;/// <summary>/// UI管理器/// </summary>public class UICon : HelperXML{public GameObject UI_Info; //游戏结束图片//计分板public Text scoreText;//得分版public Text countText;//计分板int scoreCount = 0;/// <summary>/// 游戏结束显示相关UI/// </summary>public void UIGameVoer(){//显示记分板UI_Info.SetActive(true);//遍历获取XMLList<GameInfoModel> list = base.VCLoadXMLManage();//当前分数int nowScore = scoreCount;//添加当前分数信息var mod = new GameInfoModel {Score = nowScore,Time = DateTime.Now.ToString("MM月dd日")};list.Add(mod);//倒序list = list.OrderByDescending(p => p.Score).ToList();//删除最后一个最少的if(list.Count>5)list.RemoveAt(list.Count – 1);//显示最新排名版StringBuilder sb = new StringBuilder();int index = 1;list.ForEach(p=> sb.Append(p.Score==mod.Score?("<color=#ff0000ff>" + index++ + " " + p.Score + "米 " + p.Time + "</color>\n"):( index++ + " " + p.Score + "米 " + p.Time + "\n")));countText.text = sb.ToString();//更新XMLbase.VCRemoveXmlElement();base.VCAddXml(list);}}

每次游戏结束都会更新排名板,利用XML持久化数据能实现大部分游戏的记录功能。

如场景信息、关卡信息、人物坐标、任务系统、NPC对话等等。。。。

于是夜莺会在黎明到来之前勇敢的将胸膛顶住蔷薇的刺,

游戏结束分数排名当前高能显示 (原创教程)

相关文章:

你感兴趣的文章:

标签云: