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; }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

转载于:https://www.cnblogs.com/sunet/p/3851353.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值