zoukankan      html  css  js  c++  java
  • unity3d读写txt

    http://www.cnblogs.com/sunet/p/3851353.html?utm_source=tuicool

    记录一下昨天用到的技术点:基于android平台unity3d读写txt。功能点主要是单机手游的多账号(帐号对应保存游戏数据)的动态创建与删除、排行榜等功能。将联网版改为单机版之后,本应将用户注册排行功能一并去掉才是的。但我有坑哥的策划,唯有一边心中默念草泥马,一边拼命敲代码了。

           下面将些关键代码粘贴出来,以后不准还有这样的悲情故事发生。

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

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

        3、创建或者打开txt          

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    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();
    <br>     //其实Close方法内部已经实现了Dispose方法<br>        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; }
    复制代码
  • 相关阅读:
    != 比 & 的优先级高
    VC++ 报错:Heap corruption detected
    sqlite 查询数据库中所有的表名,判断某表是否存在,将某列所有数值去重后获得数量
    Unicode与UTF-8,UTF-16
    各种格式的压缩包解压,7zip 命令行
    bat批处理文件运行时隐藏cmd窗口
    标准库中 vector list等排序
    duilib 实现列表头任意拖动
    duilib 实现 XML重用(item完全重合的CList)
    C++-POJ2503-Babelfish[hash]
  • 原文地址:https://www.cnblogs.com/nafio/p/9137517.html
Copyright © 2011-2022 走看看