如何保存和加载游戏进度?

时间:2019-02-02 15:40:48

标签: c# unity3d 2048

我正在开发2048游戏,遇到保存和加载图块位置的问题。大家都知道游戏领域长相如何,有行和列。所以,每瓦片具有行索引(--emailAdapter {"module":"@parse/simple-mailgun-adapter","options":{"fromAddress":"mail@mailgun","domain":"sandbox@mailgun.com","apiKey":"mykey"}} )和列索引(indRow),其设置在该游戏场的瓦片的位置。如何在游戏中保存该索引以在下次启动时加载?
我的GameManager脚本=

indCol

1 个答案:

答案 0 :(得分:0)

.NET Framework包含一些对Serialization有用的类。

一些常见的类是XMLSerializerDataContractJsonSerializerBinaryFormatter

使用XMLSerializer的示例如下所示:

using System.IO;
using System.Xml.Serialization;

namespace Serialization
{
    class Program
    {
        static string file = @"C:\employees.xml"; // Path to our XML file. Could be anything else.
        static void Main(string[] args)
        {
            Person[] people;
            XmlSerializer xmlser = new XmlSerializer(typeof(Person[])); // Our XML serializer!
            if (File.Exists(file)) // If the file exists read it
            {
                using (Stream s = File.OpenRead(file)) // We need a stream for our serializer
                {
                    people = (Person[])xmlser.Deserialize(s); // Don't forget to cast the result.
                }
            }
            else // If the file doesn't exist we generate new data
            {
                people = new Person[] { new Person("John", "Doe"), new Person("Joe", "Swanson") };

                using (Stream s = File.OpenWrite(file)) // And then we save our new data.
                {
                    xmlser.Serialize(s, people);
                }
            }
        }

        // And now that we have something in people you can use it as you wish to.
    }

    public class Person // Our data class. Has to be public.
    {
        public Person() { } // Parameterless constructor used by XmlSerializer. IMPORTANT!!!
        public Person(string firstName, string surName)
        {
            FirstName = firstName;
            SurName = surName;
        }

        public string FirstName { get; set; }
        public string SurName { get; set; }
    }
}