Unity3D实现本地排行榜功能

发表于2018-04-17
评论0 5.5k浏览
不论是网游还是手游,很多游戏在结束的时候会有一个相关的排行榜出现,可能是什么分数排行,玩家排行等等,那么这个排行榜功能是怎么实现的呢?下面就让大家看看效果,没有UI,就是简单的Image与Text

第一个是名次,第二个是名字,第三个是分数,第四个是时间。上面那个是添加新的记录。名字是在前面的场景中拿到的,如果测试,可以在当前的场景中直接把Text改为InputField即可。

下面是我用到的代码。

1. 先搭建基本的UI。

2. 这是基本的声明。
    public InputField inputField;
    public Button button;
    public GameObject PrefabShow;
    public Image ParentsShow;
    public Image ParentsShowUI;
    public Text textName;

3. 在初始化时我们拿到玩家的名字和对事件绑定。就是提交按钮触发的事件。
 void Start ()
 {
     textName.text = Global.PlayerName;
     button.GetComponent<Button>().onClick.AddListener(InputOK);
 }
 void InputOK()
     {
         Global.SkillCount = int.Parse(inputField.text);
        string PlayerResultDate = ResultToJson(Global.PlayerName, Global.SkillCount);
        SaveString(PlayerResultDate);
        DictionarySort(GetJsonDate());  
    }

4. 首先需要知道,既然是排行榜,一定涉及到了数据持久化,那么,在这我用的是Json,对于Json的操作我就不在这里赘述了。我存取的方式是将名字分数以及时间存在本地。

将数据转化成Json
    public string ResultToJson(string Name, int NowCount)
    {
        StringBuilder sb = new StringBuilder();
        JsonWriter WriteDate = new JsonWriter(sb);
        WriteDate.WriteObjectStart();
        WriteDate.WritePropertyName("Name");
        WriteDate.Write(Name + "|" + DateTime.Now.ToShortTimeString());
        WriteDate.WritePropertyName("Count");
        WriteDate.Write(NowCount);
        WriteDate.WriteObjectEnd();
        return sb.ToString();
    }

下面的方法就是保存在本地
    private void SaveString(string str)
    {
        FileInfo fi = new FileInfo(Application.dataPath + "/Resources/Json.txt");
        StreamWriter sw = null;
        if (fi.Exists)
        {
            sw = fi.AppendText();
        }
        else
        {
            sw = fi.CreateText();
        }
        sw.WriteLine(str);
        sw.Close();
    }

下面的方法是获取Json数据
   public Dictionary<string, int> GetJsonDate()
    {
        FileStream fi = new FileStream(Application.dataPath + "/Resources/Json.txt", FileMode.Open);
        Dictionary<string, int> jsonDate = new Dictionary<string, int>();
        if (fi.CanRead)
        {
            StreamReader sw = new StreamReader(fi);
            string jsonStr;
            while ((jsonStr = sw.ReadLine()) != null)
            {
                JsonData data = JsonMapper.ToObject(jsonStr);
                jsonDate.Add(data["Name"].ToString(), int.Parse(data["Count"].ToString()));
            }
        }
        return jsonDate;
    }

这个方法是把获取的Json数据排序并且显示出来
  private void DictionarySort(Dictionary<string, int> dic)
    {
        if (dic.Count > 0)
        {
            List<KeyValuePair<string, int>> lst = new List<KeyValuePair<string, int>>(dic);
            lst.Sort(delegate(KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
            {
                return s2.Value.CompareTo(s1.Value);
            });
            //ParentsShow.rectTransform.sizeDelta = new Vector2(600, lst.Count * 100);
            //ParentsShowUI.GetComponent<Mask>().enabled = true;
            //ParentsShowUI.GetComponentInChildren<Scrollbar>().value = 1;
            dic.Clear();
            float i = 1, r = 1, g = 1, b = 0;
            foreach (KeyValuePair<string, int> kvp in lst)
            {
                if (i <= 3)
                {
                    string[] Key = kvp.Key.Split('|');
                    GameObject ga = Instantiate(PrefabShow, ParentsShow.transform.position, Quaternion.identity) as GameObject;
                    ga.transform.parent = ParentsShow.transform;
                    r -= 0.2f;
                    g -= 0.2f;
                    b -= 0.2f;
                    Debug.Log(r + g + b);
                    Text[] Children = ga.GetComponentsInChildren<Text>();
                    Children[0].color = new Color(r, g, b);
                    Children[1].color = new Color(r, g, b);
                    Children[2].color = new Color(r, g, b);                 
                    Children[3].color = new Color(r, g, b);
                    Children[1].text = Key[0];
                    Children[3].text = kvp.Value.ToString();
                    Children[2].text = Key[1];
                    Children[0].text = (i++).ToString();
                }
                else
                {
                    string[] Key = kvp.Key.Split('|');
                    GameObject ga = Instantiate(PrefabShow, ParentsShow.transform.position, Quaternion.identity) as GameObject;
                    ga.transform.parent = ParentsShow.transform;
                    Text[] Children = ga.GetComponentsInChildren<Text>();
                    Children[1].text = Key[0];
                    Children[3].text = kvp.Value.ToString();
                    Children[2].text = Key[1];
                    Children[0].text = (i++).ToString();
                }       
            }
        }
    }

好了,这个本地排行榜功能就这么被实现出来了,大家也可以自己照着代码自己尝试下。
来自:https://blog.csdn.net/codeksky/article/details/50695515

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论