首页 > 代码库 > unity3d读写txt
unity3d读写txt
记录一下昨天用到的技术点:基于android平台unity3d读写txt。功能点主要是单机手游的多账号(帐号对应保存游戏数据)的动态创建与删除、排行榜等功能。将联网版改为单机版之后,本应将用户注册排行功能一并去掉才是的。但我有坑哥的策划,唯有一边心中默念草泥马,一边拼命敲代码了。
下面将些关键代码粘贴出来,以后不准还有这样的悲情故事发生。
1、CreateOrOPenFile(Application.persistentDataPath, "Counter.txt", info);
2、GlobalVariable.counterList = LoadFile(Application.persistentDataPath, "Counter.txt");
3、创建或者打开txt
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();
//其实Close方法内部已经实现了Dispose方法
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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。