Exemplo n.º 1
0
        public static bool Load(out GameObject playerTrain, out Player player)
        {
            string dir = Application.persistentDataPath + "/saves";

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            DirectoryInfo dir_i = new DirectoryInfo(dir);

            FileInfo[] info = dir_i.GetFiles("*.save");
            foreach (FileInfo f in info)
            {
                Debug.Log(dir);
            }

            if (info.Length >= 1)
            {
                BinaryFormatter bf        = new BinaryFormatter();
                var             createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
                FileStream      stream    = new FileStream(dir + "/player" + (info.Length) + ".save", FileMode.Open);
                stream.Position = 0;
                SerializedPlayerAndTrain sp = bf.Deserialize(stream) as SerializedPlayerAndTrain;
                stream.Close();
                playerTrain = sp.CreatePlayerTrain();
                player      = sp.player;
                Debug.Log($"Player credit = {player.Credit}");
                return(playerTrain != null);
            }
            else
            {
                Debug.Log("LOAD FROM RESOURCES. SAVE IS NOT FOUND.");
                string path = @"PlayerTrain";
                playerTrain =
                    GameObject.Instantiate(Resources.Load(path, typeof(GameObject)), new Vector3(-4.945748f, 0.3745093f, -0.979275f),
                                           Quaternion.LookRotation(Vector3.forward, Vector3.up)) as GameObject;
                player = new Player(2000);
                return(playerTrain != null);
            }
        }
Exemplo n.º 2
0
        public static void SavePlayer(GameObject playerTrain, Player player)
        {
            SerializedPlayerAndTrain sp = new SerializedPlayerAndTrain(playerTrain, player);
            BinaryFormatter          bf = new BinaryFormatter();
            var    createdir            = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
            string dir = Application.persistentDataPath + "/saves";

            DirectoryInfo dir_i = new DirectoryInfo(dir);

            FileInfo[] info = dir_i.GetFiles("*.save");
            foreach (FileInfo f in info)
            {
                Debug.Log(f);
            }

            FileStream stream = new FileStream(dir + "/player" + (info.Length + 1) + ".save", FileMode.Create);

            stream.Position = 0;
            bf.Serialize(stream, sp);
            stream.Close();
        }