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. npm ERR! Unexpected end of JSON input while parsing near '...inimist":"^1.2.0"}

    简介 在项目中执行npm install安装依赖包的时候.出现npm ERR! Unexpected end of JSON input while parsing near '...inimist& ...

  2. aop学习总结二------使用cglib动态代理简单实现aop功能

    aop学习总结二------使用cglib动态代理简单实现aop功能 模拟业务需求: 1.拦截所有业务方法 2.判断用户是否有权限,有权限就允许用户执行业务方法,无权限不允许用户执行业务方法 (判断是 ...

  3. 5.JavaScript改变样式,验证用户输入

    ① x=document.getElementById("demo") //找到元素 x.style.color="#ff0000"; //改变样式 ② if ...

  4. iOS 第三方登录之 QQ登录

    一. 首先需要下载腾讯qq登录所需的库,下载地址是http://open.qq.com/ . 需要用到的有TencentOpenAPI.framework 和TencentOpenApi_IOS_Bu ...

  5. RAC 单节点实例异常关闭,关键报错ORA--29770

    监控系统监控到RAC 的一个实例异常关闭 ,时间是凌晨1点多,还好没有影响到业务 之后就是分析原因 这套RAC搭建在虚拟化环境OS SUSE11 查看oracel alert log信息 Mon :: ...

  6. Eclipse jar打包详解

    通过Eclipse下的演示工程,介绍如何打包这样的项目:要导出的类里边用到了别的jar包. 方法/步骤     1. Eclipse下的演示工程结构如下图所示,其中Task.java是当前工程运行的M ...

  7. html5--3.6 input元素(5)

    html5--3.6 input元素(5) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...

  8. LoadRunner打开WebTours只显示头部解决办法

    LoadRunner打开WebTours只显示头部解决办法   1.遇到这种情况,先查看一下路径HP\LoadRunner\WebTours下的cgierr日志中是否有错误,比如Can't open ...

  9. js获取浏览器宽高、网页宽高、屏幕宽高、鼠标位置等(带图片说明)

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;(点击查看大图) 网页可见区域宽: document.bo ...

  10. all.css

    @charset "utf-8"*{-webkit-tap-highlight-color:rgba(0,0,0,0); padding:0; margin:0;}body{ fo ...