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. ASP.NET车辆管理系统100%源代码

    系统开发环境为VS2010.採用ASP.NET框架.数据库採用SQL Server.系统採用Ajax,具有:GPS导航(实时监控报警).申请审核.流程查看及短信息发送等功能.这个系统界面和功能是我认为 ...

  2. ios 常见错误整理 持续更新

    本文转载至 http://blog.csdn.net/yesjava/article/details/8086185  1. mutating method sent to immutable obj ...

  3. easyui datagrid 加载静态文件中的json数据

    本文主要介绍easyui datagrid 怎么加载静态文件里的json数据,开发环境vs2012, 一.json文件所处的位置 二.json文件内容 {"total":28,&q ...

  4. [数据挖掘课程笔记]Naïve Bayesian Classifier

    朴素贝叶斯模型 1) X:一条未被标记的数据 2) H:一个假设,如H=X属于Ci类 根据贝叶斯公式 把X表示为(x1,x2,....xn) x1,x2,....xn表示X在各个特征上的值. 假设有c ...

  5. Handler向子线程发送数据

    public class MainActivity extends AppCompatActivity { private static final String TAG = "MainAc ...

  6. json Gson

    package com.example.volleylearn; import java.util.ArrayList; import java.util.List; import java.util ...

  7. include vector 编译出错VC++

    error C2665: “operator new” : 5个重载中没有一个可以转换参数1(从“const char [71]”类型)这个错误是怎么回事啊,搜索了整个项目好像没有可疑的new操作阿. ...

  8. 在win7系统下安装把Ubuntu17.04安装在另一个硬盘开机无法进入Ubuntu问题的一种解决办法。【转】

    本文转载自:http://blog.csdn.net/u012879090/article/details/74937762 在win7系统下安装把Ubuntu17.04安装在另一个硬盘开机无法进入U ...

  9. python 实用pickle序列化

    存储数据结构到一个文件中称为序列化.相json这样的格式需要定制的序列化数据的转换器.python提供了pickle模块以特殊的二进制格式保存和恢复数据对象. 还记得json解析datetime对象时 ...

  10. 腾讯Hermes设计概要——数据分析用的是列存储,词典文件前缀压缩,倒排文件递增id、变长压缩、依然是跳表-本质是lucene啊

    转自:http://data.qq.com/article?id=817 三.Hermes设计概要 架构描述 系统核心进程均采用分散化设计,根据业务发展需求,可随意扩缩容机器; 周期性数据直接通过td ...