unity3d讀寫txt


      記錄一下昨天用到的技術點:基於android平台unity3d讀寫txt。功能點主要是單機手游的多賬號(帳號對應保存游戲數據)的動態創建與刪除、排行榜等功能。將聯網版改為單機版之后,本應將用戶注冊排行功能一並去掉才是的。但我有坑哥的策划,唯有一邊心中默念草泥馬,一邊拼命敲代碼了。

       下面將些關鍵代碼粘貼出來,以后不准還有這樣的悲情故事發生。

    1、CreateOrOPenFile(Application.persistentDataPath, "Counter.txt", info);

           2、GlobalVariable.counterList = LoadFile(Application.persistentDataPath, "Counter.txt");

    3、創建或者打開txt          

   void CreateOrOPenFile(string path, string name, string info)
    {

        StreamWriter sw;
        FileInfo fi = new FileInfo(path + "//" + name);
        if (!fi.Exists)
        {
            sw = fi.CreateText();
        }
        else
        {
            sw = fi.AppendText();
        }
        sw.WriteLine(info);
        sw.Close();
  
     //其實Close方法內部已經實現了Dispose方法
sw.Dispose(); }

     4、讀取txt文本

   List<string> LoadFile(string path, string name)
    {
        StreamReader sr = null;
        try
        {
            sr = File.OpenText(path + "//" + name);
        }
        catch
        {
            return null;
        }
        string lineInfo;
        List<string> lineInfoList = new List<string>();
        while ((lineInfo = sr.ReadLine()) != null)
        {
            lineInfoList.Add(lineInfo);
        }
        sr.Close();
        return lineInfoList;
    }

    

    5、修改txt中某一行,覺得自己用這個方法不好,有大牛給點建議?

 

    void ConfirmDel(GameObject goName)
    {
        string name = goName.name;
        switch (name)
        {
             //取消刪除
            case "CancelButton":
                goDialogPanel.transform.localPosition = new Vector3(1000f, -40f, 0);
                goUIPanel.SetActive(true);
                break;

             //確認刪除
case "ConfirmButton": GlobalVariable.counterList.Clear(); GlobalVariable.counterList = LoadFile(Application.persistentDataPath, "Counter.txt"); string nickName = ""; string parName = GlobalVariable.strName; switch (parName) { case "C0": nickName = GlobalVariable.counterList[0].Split(',')[0]; GlobalVariable.counterList.RemoveAt(0); break; case "C1":                //加if判斷的原因(其實要結合項目才能理解的):若刪除排列中間的某一個,string類型的泛型集合元素的下標已經改變了,但是游戲對象.name還是不變的。所以gameObject對應的原集合的前一個下標即是它現下標
if (GlobalVariable.counterList.Count < 2) { GlobalVariable.counterList.RemoveAt(GlobalVariable.counterList.Count - 1); } else { GlobalVariable.counterList.RemoveAt(1); } nickName = GlobalVariable.counterList[0].Split(',')[0]; break; case "C2": if (GlobalVariable.counterList.Count < 3) { GlobalVariable.counterList.RemoveAt(GlobalVariable.counterList.Count - 1); } else

                 GlobalVariable.counterList.RemoveAt(
2);               }
nickName = GlobalVariable.counterList[0].Split(',')[0]; break; case "C3": if (GlobalVariable.counterList.Count < 4) { GlobalVariable.counterList.RemoveAt(GlobalVariable.counterList.Count - 1); } else GlobalVariable.counterList.RemoveAt(3); nickName = GlobalVariable.counterList[0].Split(',')[0]; break; case "C4": if (GlobalVariable.counterList.Count < 5) { GlobalVariable.counterList.RemoveAt(GlobalVariable.counterList.Count - 1); } else GlobalVariable.counterList.RemoveAt(4); nickName = GlobalVariable.counterList[0].Split(',')[0]; break; default: break; } File.Delete(Application.persistentDataPath + "//" + "Counter.txt"); if (GlobalVariable.counterList.Count != 0) { for (int i = 0; i < GlobalVariable.counterList.Count; i++) {   //重寫txt
CreateOrOPenFile(Application.persistentDataPath,
"Counter.txt", GlobalVariable.counterList[i]); } } goDialogPanel.transform.localPosition = new Vector3(1000f, -40f, 0); go.transform.FindChild(GlobalVariable.strName).gameObject.SetActive(false); break; default: break; } if (GlobalVariable.counterList.Count < 5) { goNewCounter.SetActive(true); } else { goNewCounter.SetActive(false); } if (GlobalVariable.counterList == null) { PlayerPrefs.DeleteKey("v"); PlayerPrefs.Save(); } goUIPanel.SetActive(true);
//重新排列
go.GetComponent<UIGrid>().repositionNow = true; }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM