unity 读取TXT

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class ReadText : MonoBehaviour {

    // Use this for initialization
    void Start() {
    }
	// Update is called once per frame
	void Update () {
       

    }

   public void WirteText()
    {

        string thePathUrl = System.Environment.CurrentDirectory;
        thePathUrl = thePathUrl + "//score.txt";
       // Debug.Log(thePathUrl);

        string[] theAllDate = File.ReadAllLines(thePathUrl);

        string name = "哥";
        int theScore = 100;
        name = alldate.theplayname;
        theScore = alldate.thescore;
        if (theAllDate.Length == 0)
        {
            string[] thename1 = new string[1];
            thename1[0] = name + "," + theScore.ToString();

            File.WriteAllLines(thePathUrl, thename1);
        }
        else
        {
            string[] thename1 = new string[theAllDate.Length + 1];
            bool m_binsert = false;
            int j = 0;
            for (int i = 0; i < theAllDate.Length;)
            {
                Debug.Log(thename1[i]);
                string[] thevalue = theAllDate[i].Split(',');

                int theScoreAgo = int.Parse(thevalue[1]);//前面的 的分数

                if (m_binsert == false)
                {
                    if (theScoreAgo > theScore)
                    {
                        thename1[j] = thevalue[0] + "," + thevalue[1];
                        j++;
                        i++;
                    }
                    else
                    {
                        thename1[j] = name + "," + theScore.ToString();
                        m_binsert = true;
                        j++;
                    }
                }
                else
                {
                    thename1[j] = thevalue[0] + "," + thevalue[1];
                    j++;
                    i++;
                }
            }
            if (m_binsert == false)
            {
                thename1[theAllDate.Length] = name + "," + theScore.ToString();
            }

            File.WriteAllLines(thePathUrl, thename1);
        }
    }
}

 

你可能感兴趣的:(unity 读取TXT)