1. 程式人生 > >Unity3D跑酷遊戲開發-遊戲結束分數排名當前高能顯示 (原創教程)

Unity3D跑酷遊戲開發-遊戲結束分數排名當前高能顯示 (原創教程)

一般遊戲結束後都會有個分數排名板。

接下來讓分析這功能。

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);
        //遍歷獲取XML
        List<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();
        //更新XML
        base.VCRemoveXmlElement();
        base.VCAddXml(list);
    }
}

每次遊戲結束都會更新排名板,利用XML持久化資料能實現大部分遊戲的記錄功能。

如場景資訊、關卡資訊、人物座標、任務系統、NPC對話等等。。。。

專案小結:Linq 、lambda糖果是個好東西.foreach啥的以後少用了,官方歧視這些高能貨。。

此遊戲教程本來去年就要寫完的,到後面發現做成視訊教程都要好幾個小時,何況全程高能紅箭頭標識的圖文教程。要嘮叨的東東太多了,還請大家見諒。

By:略知CSharp

2015年04月20日 00:00:23