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 FileInfonew FileInfo(path"//" + if (!fi.Exists) { sw } else { sw } sw.WriteLine(info); sw.Close();<br> //其实Close方法内部已经实现了Dispose方法<br> } |
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的更多相关文章
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- WPF 读写TxT文件
原文:WPF 读写TxT文件 文/嶽永鹏 WPF 中读取和写入TxT 是经常性的操作,本篇将从详细演示WPF如何读取和写入TxT文件. 首先,TxT文件希望逐行读取,并将每行读取到的数据作为一个数组的 ...
- java指定编码的按行读写txt文件(几种读写方式的比较)
转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976 版权声明:本文为博主原创文章,未经博主允许不得转载. ...
- python操作txt文件中数据教程[1]-使用python读写txt文件
python操作txt文件中数据教程[1]-使用python读写txt文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原始txt文件 程序实现后结果 程序实现 filename = '. ...
- C#读写txt文件的两种方法介绍[转]
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- C#读写txt文件的两种方法介绍 v
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
- UNICODE环境下读写txt文件操作
内容转载自http://blog.sina.com.cn/s/blog_5d2bad130100t0x9.html UNICODE环境下读写txt文件操作 (2011-07-26 17:40:05) ...
- MFC读写.txt文件时进度条显示实时进度
整体实现方式:先获得文件长度,然后用每次读取的长度,计算出完成的百分比,用百分比的值设置进度条. 一.MFC进度条 Progress Control 相关函数 1. create() --创建Prog ...
随机推荐
- Aspose.cells 读取Excel表中的图片问题
一.说明 本文主要是讲解,怎么使用aspose.cells读取Excel表中的图片,并把图片转换成流或是image对象. 二.开发环境说明 开发工具vs2012,c#语言, 三.Aspose.cell ...
- Maximum Memory and CPU Limitations for Linux Server
grep NR_CPUS /boot/config-`uname -r` [webdev@test apache-flume-1.8.0-bin]$ grep NR_CPUS /boot/config ...
- windows搭建FTP服务器实战
第一步:创建用户名密码(ftp使用) 1.1.点击“开始”菜单,选择“控制面板”. 1.2.选择“管理工具”—>“计算机管理” 1.3. 选择“本地用户和组”下的用户,右键选择“新用户” 输入用 ...
- WIN7系统设置wifi
*&->20170302 112700 WIN7系统设置wifi, 开启win7的隐藏功能,即虚拟wifi功能和虚拟无线AP功能,即可实现将电脑变成wifi 供无线上网, 1.开始-命令 ...
- APP上线审核注意事项
基本要点 · 不能导致手机故障(比如崩溃或屏幕问题) · 长时间/过度使用之后反应仍然很快 · 应用内的所有价格信息中不能用固定值代替可变变量 · ...
- animation steps属性实现帧动画
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta na ...
- (转)如何使用Java、Servlet创建二维码
归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...
- hdu 1004 Let the Balloon Rise 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 用STL 中的 Map 写的 #include <iostream> #includ ...
- gitblit安装使用
1.下载地址 http://www.gitblit.com/ 2.安装jdk(自行安装) 3.解压gitblit # tar -zxvf gitblit-1.8.0.tar.gz 4.配置# cd g ...
- 动态链接库的ELF头分析
ELF(Executable and Linking Format)用于存储Linux程序. ELF文件分三种类型: 1.目标文件(通常是.o); 2.可执行文件(我们的运行文件) 3.动态库(. ...