unity3d讀寫txt

來源:互聯網
上載者:User

標籤:android   style   blog   color   os   資料   

      記錄一下昨天用到的技術點:基於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; }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.