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;
}
												

unity3d读写txt的更多相关文章

  1. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  2. C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  3. WPF 读写TxT文件

    原文:WPF 读写TxT文件 文/嶽永鹏 WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件. 首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的 ...

  4. java指定编码的按行读写txt文件(几种读写方式的比较)

    转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976  版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  5. python操作txt文件中数据教程[1]-使用python读写txt文件

    python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...

  6. C#读写txt文件的两种方法介绍[转]

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  7. C#读写txt文件的两种方法介绍 v

    C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...

  8. UNICODE环境下读写txt文件操作

    内容转载自http://blog.sina.com.cn/s/blog_5d2bad130100t0x9.html UNICODE环境下读写txt文件操作 (2011-07-26 17:40:05) ...

  9. MFC读写.txt文件时进度条显示实时进度

    整体实现方式:先获得文件长度,然后用每次读取的长度,计算出完成的百分比,用百分比的值设置进度条. 一.MFC进度条 Progress Control 相关函数 1. create() --创建Prog ...

随机推荐

  1. 在zend framework框架中try{}catch(Exception e){}的跳转问题

    请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 首先我先说明我遇到的问题 try{ //导入学生信息 $ModelStudent->insert($dat ...

  2. 虚拟机和主机ping不通,SQL Server无法远程连接的解决方法

    一.虚拟机网络的配置 这里只列一下自己的配置: 1.编辑---虚拟网络编辑器 进行设置 2.设置对应系统 3.还是Ping不通,最后关闭 虚机内的Windows防火墙,可以Ping通,看来Net模式下 ...

  3. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. EasyDarwin开源流媒体服务器提供的TS切片/HLS直播打包库

    EasyHLS  Github:https://github.com/EasyDarwin/EasyHLS EasyHLS是什么? EasyHLS是EasyDarwin开源流媒体社区开发的一款HLS打 ...

  5. vue web开发

    https://www.szzhdj.gov.cn/js/pagejs/assemblyHall_dzs1.js https://www.szzhdj.gov.cn/js/pagejs/assembl ...

  6. Oracle 11gR2 使用RMAN Duplicate复制数据库

    Oracle 11gR2 使用RMAN Duplicate复制数据库     前言:     上周刚做完一个项目,用户要求RAC的数据库可以自己主动备份到另外一个单节点上,单节点可以正常拿起来就能用. ...

  7. CORS 理解(不要那么多术语)

    摘要 谈到跨域,不论前端还是后端,多少有点谈虎色变,面试中也常会问到这些问题,浏览器和服务器端到底怎么做才能跨域,他们都做了什么? 同源 vs 跨域 同源,字面意义是相同的源头,即同一个web服务器( ...

  8. webpack v3 结合 react-router v4 做 dynamic import — 按需加载(懒加载)

    为什么要做dynamic import? dynamic import不知道为什么有很多叫法,什么按需加载,懒加载,Code Splitting,代码分页等.总之,就是在SPA,把JS代码分成N个页面 ...

  9. Ubuntu 14.04 下 android studio 安装 和 配置【转】

    本文转载自:http://blog.csdn.net/xueshanfeihu0/article/details/52979717 Ubuntu 14.04 下 android studio 安装 和 ...

  10. python+Django实现Nagios自动化添加监控项目

    最近机房刚上了一批机器(有100台左右),需要使用Nagios对这一批机器进行监控.领导要求两天时间完成所有主机的监控.从原来的经验来看,两天时间肯定完成不了.那怎么办?按照之前的想法,肯定是在nag ...