예제 #1
0
        public static DataPlayer Load(DataUnits dataUnits, DataPlayer reference)
        {
            string path = $"{Application.persistentDataPath}/{SaveName}";

            if (File.Exists(path))
            {
                Debug.Log($"Load successfully {path}");
                BinaryFormatter formatter = new BinaryFormatter();
                FileStream      stream    = new FileStream(path, FileMode.Open);
                saveFile = formatter.Deserialize(stream) as Dictionary <string, object>;
                DataPlayer data = Convert(saveFile, dataUnits, reference);

                return(data);
            }
            Debug.Log($"Save file not found in {path}");
            return(null);
        }
예제 #2
0
        public static DataPlayer Convert(Dictionary <string, object> data, DataUnits dataUnits, DataPlayer reference)
        {
            DataPlayer dataPlayer = DataPlayer.Copy(reference);

            dataPlayer.Gold.SetValueAndForceNotify((int)data[Gold]);


            var unitStats = data[PlayerUnits] as Stats[];

            var playerUnits = new List <DataUnit>();
            int i           = 0;

            foreach (var item in dataUnits.units)
            {
                var unit = DataUnit.Copy(item);
                if (i < unitStats.Length)
                {
                    unit.stats = unitStats[i];
                }
                playerUnits.Add(unit);
                i++;
            }
            dataPlayer.playerUnits = playerUnits;



            var playerSquadNumbers = data[PlayerSquad] as int[];
            var playerSquad        = new List <DataUnit>();

            foreach (var item in playerSquadNumbers)
            {
                playerSquad.Add(playerUnits[item]);
            }
            dataPlayer.squad = playerSquad;


            var towerStats   = data[PlayerTowers] as Stats[];
            var playerTowers = new List <DataUnit>();

            i = 0;
            foreach (var item in dataUnits.towers)
            {
                var tower = DataUnit.Copy(item);

                if (i < towerStats.Length)
                {
                    tower.stats = towerStats[i];
                }
                playerTowers.Add(tower);
                i++;
            }
            dataPlayer.playerTowers = playerTowers;

            var activeTowerNumber = (int)data[PlayerActiveTower];

            if (activeTowerNumber >= 0 && playerTowers.Count > activeTowerNumber)
            {
                dataPlayer.dataTower = playerTowers[activeTowerNumber];
            }
            else
            {
                dataPlayer.dataTower = playerTowers[0];
            }

            return(dataPlayer);
        }