• 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; }
    复制代码
  • 相关阅读:
    layui 表格分页
    MVC View 获取 控制器返回的ViewData和viewBag
    [转]C#编码规范
    [转]11-为什么局部变量是线程安全的?
    [转]C#进程间通讯--共享内存篇
    [转]C# DataGridView绑定数据源
    [转]PropertyGrid自定义控件
    [转]谁说.NET不适合搞大数据,机器学习、人工智能
    [转]Halcon的C#二次开发及经验分享
    [转]图像获取与采集及图像格式与Region介绍——第2讲
  • 原文地址:https://www.cnblogs.com/nafio/p/9137517.html
Copyright © 2020-2023  润新知