Ios保存路径

ios路径 FileStream fileStream = File.Create(Application.persistentDataPath + “/save.txt”);

class GameContriler : MonoBehaviour
{
 private void OnEnable()
    {
        Invoke("GetTheDate",0.1f);
      
    }
 private void OnDestroy()
    {
        SaveTheGame();
    }
    public void SaveTheGame()
    {
        Save save = new Save();//save对象已经船舰成功
        save.SoliderTypeDictionary = DataSave._instance.SoliderTypeDictionary;
        save.cityIsPass = DataSave._instance.cityIsPass;
        save.gold  = DataSave._instance.gold;
        save.weiwang = DataSave._instance.weiwang;
        save.tongyu = DataSave._instance.tongyu;
        save.junbei = DataSave._instance.junbei;
        save.theJiJians = DataSave._instance.theJiJians;
        save.theSkillButtonIsPass = DataSave._instance.theSkillButtonIsPass;
        save.isOnceShow = DataSave._instance.isOnceShow;
        save.once = DataSave._instance.once;

        BinaryFormatter bf = new BinaryFormatter();

        // FileStream fileStream = File.Create(Application.dataPath+"/StreamingFile"+"/save.txt");
        FileStream fileStream = File.Create(Application.persistentDataPath + "/save.txt");
        bf.Serialize(fileStream,save);
        fileStream.Close();
        ShowTIP._instance.ShowTips("数据已经成功保存");
    }//保存游戏的方法 不知道在哪里调用

    public  void  GetTheDate()  // 读取游戏的方法 不知道在哪里调用
    {
       string data= Application.persistentDataPath + "/save.txt";
      
       // if (File.Exists(Application.dataPath + "/StreamingFile" + "/save.txt"))
       if(File.Exists(data))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream filestream = File.Open(data, FileMode.Open);
            Save save = (Save)bf.Deserialize(filestream);
            filestream.Close();
            RecoverTheData(save);//还原数据的方法
            DataSave._instance.RecverTheDate();//还原各种界面以及bg 基建的方法
            ShowTIP._instance.ShowTips("The data has been successfully restored!");
        }
        else { ShowTIP._instance.ShowTips("This is the first time you have logged in to the game and no game data has been saved!"); }
    }

    void RecoverTheData(Save save)//还原数据子方法 
    {
          DataSave._instance.SoliderTypeDictionary=save.SoliderTypeDictionary ;
          DataSave._instance.cityIsPass= save.cityIsPass;
          DataSave._instance.gold = save.gold;
          DataSave._instance.weiwang = save.weiwang;
          DataSave._instance.tongyu= save.tongyu;
          DataSave._instance.junbei= save.junbei;
          DataSave._instance.theJiJians= save.theJiJians;
          DataSave._instance.theSkillButtonIsPass= save.theSkillButtonIsPass;
          DataSave._instance.isOnceShow = save.isOnceShow;
          DataSave._instance.once= save.once ;
    }
}

datasave类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DataSave : MonoBehaviour
{
    public static DataSave _instance;
    //这个类用来专门存储数据
    
    /*
     1 存储 成就的12个bool值{在Achiement里边}
     2 存储 兵种的4个值 打算用dictionary存储<string name ,bool isunlocked>
     3 存储 城池+金矿 的通关状态 5+1个bool值(同时根据它换游戏背景)
     4 存储 4种货币 威望 金币 统御 军备
     5 存储 基建4个的建造情况 准备一个字典 key是基建位置 主城?左上 右上 左下 右下
            而 value代表 未建造?金矿?补给点?

     */
     //1 存储 成就的12个bool值{在Achiement里边}
    public bool[] achievementIsPass = new bool[12];//成就的12个bool值
    //如果达成条件 直接通过单例模式修改false为trues


    // 2 存储 兵种的4个值 打算用dictionary存储<string name ,bool isunlocked>
    public Dictionary<string, bool> SoliderTypeDictionary = new Dictionary<string, bool>();//存储4个兵种的解锁状态



   // 3 存储 城池+金矿 的通关状态 5+1个bool值
   public bool[] cityIsPass = new bool[6];// 0第一关  1金矿  2 第二关  3第三关   4第四关   5第五关

    //4 存储 4种货币 威望 金币 统御 军备
    public int gold = 10000;//四种货币
    public int weiwang = 10000;
    public int tongyu = 10000;
    public int junbei = 10000;
    //5 存储 基建4个的建造情况 准备一个字典 key是基建位置 主城?左上 右上 左下 右下
    //                                      value代表 未建造?金矿?补给点?
    public Dictionary<string, string> theJiJians = new Dictionary<string, string>();
    //6 存储 12个skill里边的button的状态 第0 4 8个bool 分别是战斗界面的技能的1 2 3 的解锁状态
    public bool[] theSkillButtonIsPass=new bool[12];

    //7 存储一个只有第一次见到才会出来的提示框
    public bool isOnceShow = false;
    public  bool once = true;

    private void Awake()
    {
        _instance = this;
            
            
            
          
        if(once)
        {  SoliderTypeDictionary.Add("PangDe",false );
        SoliderTypeDictionary.Add("CaoHong", false);
        SoliderTypeDictionary.Add("XiaHouYuan", false);
        SoliderTypeDictionary.Add("XuChu", false);

        theJiJians.Add("zhucheng","1");//建造之后有没有等级区分?
        theJiJians.Add("zuoshang", "未建造");
        theJiJians.Add("youshang", "未建造");
        theJiJians.Add("zuoxia", "未建造");
        theJiJians.Add("youxia", "未建造");
          //  print("初始化了datasave里边的数据");
            once = false;
      }
    }
    public void UnLockSoliderType(string name)//解锁兵种的方法的实现
    { 
        for (int i=0;i< SoliderTypeDictionary.Count;i++)
        {
            SoliderTypeDictionary[name] = true;//把这个key的value变成true
        }
    }
    public  void    ShowTheDictionary()
    {
        foreach (var temp in SoliderTypeDictionary)
        {
            print(temp.Key + "的解锁状态:" + temp.Value);
        }
    }
    public void RecverTheDate()
    { /*
        1 还原背景
        2 还原基建
        3 还原通关状态
        
         */
        GameContriler._instance.UpdateMainBG();//还原背景成功

        //2 还原基建
        if (theJiJians["zuoshang"] != "未建造")
        { GameContriler._instance.BuildJiJian("zuoshang", theJiJians["zuoshang"]); }
        if (theJiJians["youshang"] != "未建造")
        { GameContriler._instance.BuildJiJian("youshang", theJiJians["youshang"]); }
        if (theJiJians["zuoxia"] != "未建造")
        { GameContriler._instance.BuildJiJian("zuoxia", theJiJians["zuoxia"]); }
        if (theJiJians["youxia"] != "未建造")
        { GameContriler._instance.BuildJiJian("youxia", theJiJians["youxia"]); }
        //2.5 还原主城 好像不用 等级自己存储了

        for (int i = 0; i < 6; i++)
        { GameContriler._instance.citys[i].GetComponent<City>().isPass = cityIsPass[i]; }
    }
}

save类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Save 
{
    public bool[] achievementIsPass = new bool[12];

    public Dictionary<string, bool> SoliderTypeDictionary = new Dictionary<string, bool>();

    public bool[] cityIsPass = new bool[6];

    public int gold;
    public int weiwang;
    public int tongyu;
    public int junbei;

    public Dictionary<string, string> theJiJians = new Dictionary<string, string>();

    public bool[] theSkillButtonIsPass = new bool[12];

    public bool isOnceShow;
    public bool once;


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值