简体   繁体   English

您将如何存储这些数据

[英]How would you store this data

I have a game and I need to store this data: 我有一个游戏,我需要存储以下数据:

  • Country 国家
  • City
  • Level 水平

There are 5 countries, 5 cities per country, and x levels per city. 有5个国家/地区,每个国家5个城市,每个城市x个等级。

What would be the best way to store this data, i'm looking to store the level details, such as completed, time taken, etc 什么是存储此数据的最佳方法,我正在寻找存储级别的详细信息,例如完成,花费的时间等

Then i'm looking to access the level data by Leveldata[countryindex, cityindex]. 然后,我希望通过Leveldata [countryindex,cityindex]访问级别数据。

I thought of multidimensional lists, or dictionaries, but wondered what you guys think is best practice? 我想到了多维列表或字典,但想知道你们认为最佳实践是什么?

I'd also need to save this data in JSON. 我还需要将此数据保存为JSON。

Thanks 谢谢

A structure of classes mentioned by Kirill Polishchuk, marked as Serializable, but with some array operator overloads will do what you need. Kirill Polishchuk提到的类的结构,标记为Serializable,但是带有一些数组运算符的重载将满足您的需求。

Then you can use Unity's built-in JsonUtility to serialise to json and write to the disk (or PlayerPrefs as a string). 然后,您可以使用Unity内置的JsonUtility序列化为json并写入磁盘(或PlayerPrefs作为字符串)。 In the following code, I add a Save and Load method to the LevelData class that does this for you. 在下面的代码中,我将一个Save and Load方法添加到为您执行此操作的LevelData类中。

[System.Serializable]
public class Level
{
    public int Score;
    // ...
}

[System.Serializable]
public class City
{
    public List<Level> Levels = new List<Level>();
}

[System.Serializable]
public class Country
{
    public List<City> Cities = new List<City>();

    public City this[int cityIndex]
    {
        get
        {
            if (cityIndex < 0 || cityIndex >= Cities.Count)
            {
                return null;
            }
            else
            {
                return Cities[cityIndex];
            }
        }
    }
}

[System.Serializable]
public class LevelData
{
    public List<Country> Countries = new List<Country>();

    public List<Level> this[int countryIndex, int cityIndex]
    {
        get
        {
            if (countryIndex < 0 || countryIndex >= Countries.Count)
            {
                return null;
            }
            else
            {
                City city = Countries[countryIndex][cityIndex];
                if (city != null)
                {
                    return city.Levels;
                }
                else
                {
                    return null;
                }
            }

        }
    }

    public void Save(string path)
    {
        string json = JsonUtility.ToJson(this);

        // Note: add IO exception handling here!
        System.IO.File.WriteAllText(path, json);
    }

    public static LevelData Load(string path)
    {
        // Note: add check that the path exists, and also a try/catch for parse errors
        LevelData data = JsonUtility.FromJson<LevelData>(path);

        if (data != null)
        {
            return data;
        }
        else
        {
            return new LevelData();
        }
    }

You may need to add setters for creating the country and city objects. 您可能需要添加设置器以创建国家和城市对象。 Or, if you add LevelData as a public variable to a script, this structure will be visible in the Unity editor. 或者,如果您将LevelData作为公共变量添加到脚本中,则此结构将在Unity编辑器中可见。

And to add and save level: 并添加和保存级别:

LevelData data = LevelData.Load(path);
// Here I assume your countries and cities already exist in the structure
List<Level> levels = data[1,2];
// todo: check that levels is not null! 

Level l = new Level();  
// add all info to l
levels.Add(l);

data.Save(path);

Create a proper data-model, eg: 创建一个适当的数据模型,例如:

public class Level
{
    public TimeSpan TimeTaken { get; set; }
    // level specific data
}

public class City
{
    public IList<Level> Levels { get; set; }
}

public class Country
{
    public IList<City> Cities { get; set; }
}

Then you can simply use JSON.NET to serialize/deserialize to/from JSON, eg: 然后,您可以简单地使用JSON.NET对JSON进行序列化/反序列化,例如:

string json = JsonConvert.SerializeObject(country);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM