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 FileInfo new 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 ...
随机推荐
- TEA对称加密算法
今天在看<Distributed Systems Concepts and Design>这本书的时候,在讲到分布式系统的安全性的时候,给出了TEA算法,书本上有现成的代码,所以摘录下来以 ...
- js 链接传入中文参数乱码解决
传入时,可能出现中文的参数用encodeURI进行两次转码,如: lethref="http://www.zzdblog.cn?keyword='+encodeURI(encodeURI(k ...
- java中设计模式详解
一.设计模式的分类 总体来说设计模式分为三大类: (1)创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. (2)结构型模式,共七种:适配器模式.装饰器模式.代理模式.外 ...
- codeforces 509 B题 Painting Pebbles
转载地址:http://blog.csdn.net/nike0good/article/details/43449739 B. Painting Pebbles time limit per test ...
- Codeforces Round #222 (Div. 1) Maze —— dfs(连通块)
题目链接:http://codeforces.com/problemset/problem/377/A 题解: 有tot个空格(输入时统计),把其中k个空格变为wall,问怎么变才能使得剩下的空格依然 ...
- Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stoc
今天在使用yum安装文件时,出现了以下问题: root@localhost opt]# yum update Loaded plugins: fastestmirror Could not retri ...
- 驻守深寒:寻找那些有效地关键K线
K线是组成投机市场的基本符号,也是技术分析的基本工具.可是面对浩如烟海的杂乱K线,特别是市场盘整时,经常使人们的判断发生混乱.支撑之下有支撑,阻力之上有阻力. 前人总结了大量的K线组合和由K线组成的技 ...
- codeforces B. Roma and Changing Signs 解题报告
题目链接:http://codeforces.com/problemset/problem/262/B 题目意思:给出 n 个数和恰好一共要做的操作总数k.通过对n个数进行k次操作,每次操作可以把a[ ...
- codeforces 701B B. Cells Not Under Attack(水题)
题目链接: B. Cells Not Under Attack 题意: n*n的棋盘,现在放m个棋子,放一个棋子这一行和这一列就不会under attack了,每次放棋子回答有多少点还可能under ...
- Mybatis一二级缓存的理解
频繁的数据库操作是非常耗费性能的(主要是因为对于DB而言,数据是持久化在磁盘中的,因此查询操作需要通过IO,IO操作速度相比内存操作速度慢了好几个量级),尤其是对于一些相同的查询语句,完全可以 ...