Unity 遍历列表以按数字值排序

Unity Iterate over a list to sort by the number value

我有一个名为 highscore.txt 的文本文件,我知道进入该文件的数据格式如下:

Username:score

然后我需要搜索整个文件以查看需要添加新分数的位置,以便文件按分数从大到小的顺序排列

    List<string> tempoaryList = new List<string>();
    List<int> valueList = new List<int>();
    string lineValues = "";

    using (StreamReader sr = new StreamReader(finalPath))
    {
        while (sr.EndOfStream == false)
        {
            lineValues = sr.ReadLine();
            tempoaryList.Add(lineValues);
            int data = Convert.ToInt32(lineValues.Substring(lineValues.LastIndexOf(':') + 1));
            valueList.Add(data);
        }

    }
    valueList.Sort();
    for(int i =0; i > valueList.Count; i++)
    {
        if(valueList[i] <= playerScore)
        {
            tempoaryList.Insert(i - 1, playerScore.ToString());
        }
    }
    using (StreamWriter sw = new StreamWriter(finalPath, false))
    {
        for (int i = 0; i < tempoaryList.Count; i++)
        {
            sw.WriteLine(tempoaryList[i]);
            Debug.Log(tempoaryList[i]);
        }
    }

以上只是简单地不更改文本文件并保持原样,我做错了什么?

您应该将代码更改为:

ScoreList.cs

using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;

public class ScoreList : MonoBehaviour {

    private void Start() {
        GameOver.GameOverEvent += UpdateScoreBoardFile;
    }

    private void UpdateScoreBoardFile(string playerName, int playerScore) {
        string lineValues;
        string finalPath = "[ScoreBoard File Location]";
        List<string> tempoaryList = new List<string>();
        List<int> valueList = new List<int>();
        bool isScoreLowest = true;

        using (StreamReader sr = new StreamReader(finalPath)) {
            while (sr.EndOfStream == false) {
                lineValues = sr.ReadLine();
                tempoaryList.Add(lineValues);
                int data = Convert.ToInt32(lineValues.Substring(lineValues.LastIndexOf(':') + 1));
                valueList.Add(data);
            }
        }

        if (tempoaryList != null) {
            for (int i = 0; i < valueList.Count; i++) {
                if (valueList[i] <= playerScore) {
                    tempoaryList.Insert(i, playerName + ":" + playerScore.ToString());
                    isScoreLowest = false;
                    break;
                }
            }
        }

        if (isScoreLowest) {
            tempoaryList.Add(playerName + ":" + playerScore.ToString());
        }

        using (StreamWriter sw = new StreamWriter(finalPath, false)) {
            for (int i = 0; i < tempoaryList.Count; i++) {
                sw.WriteLine(tempoaryList[i]);
                Debug.Log(tempoaryList[i]);
            }
        }
    }
}

GameOver.cs(或任何你管理游戏结束条件的地方):

using UnityEngine;
using System;

public class GameOver : MonoBehaviour {

    public static event Action<string, int> GameOverEvent;
    string playerName;
    int finalScore;

    private void GameOver() {
        //Do other stuff when the game is over
        GameOverEvent(playerName, finalScore);
    }
}

变更说明:

isScoreLowest 需要 Add 列表末尾的新最低分数,因为 for 循环不会自行添加它。

完成 if (tempoaryList != null) 检查以在计分板为空时跳过 for 循环。

并且您免于做 Sort,这总是一件好事。 :)