【问题标题】:How would you store this data你将如何存储这些数据
【发布时间】:2019-09-27 22:23:09
【问题描述】:

我有一个游戏,我需要存储这些数据:

  • 国家
  • 城市
  • 等级

有 5 个国家,每个国家 5 个城市,每个城市有 x 个级别。

存储这些数据的最佳方式是什么,我希望存储关卡详细信息,例如完成、花费的时间等

然后我希望通过 Leveldata[countryindex, cityindex] 访问级别数据。

我想到了多维列表或字典,但想知道你们认为最佳做法是什么?

我还需要以 JSON 格式保存这些数据。

谢谢

【问题讨论】:

    标签: c# json unity3d save load


    【解决方案1】:

    创建一个合适的数据模型,例如:

    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; }
    }
    

    然后您可以简单地使用JSON.NET 对 JSON 进行序列化/反序列化,例如:

    string json = JsonConvert.SerializeObject(country);
    

    【讨论】:

    • 然后我希望通过 Leveldata[countryindex, cityindex] 访问级别数据,这一切都分开了吗?
    • 整个事情可以包装到一个包含国家列表的数据类中。在每个级别(数据、国家、城市),您都有一个字典字符串、对象来快速轻松地检索内容。然后在数据级别,您可以有一个方法,该方法接受两个参数并首先获取国家/地区,然后从该国家/地区获取城市并返回级别列表。该方法还将执行必要的检查以确保参数正确。
    【解决方案2】:

    Kirill Polishchuk 提到的类结构,标记为可序列化,但带有一些数组运算符重载可以满足您的需要。

    然后您可以使用 Unity 的内置 JsonUtility 序列化为 json 并写入磁盘(或 PlayerPrefs 作为字符串)。在下面的代码中,我向 LevelData 类添加了一个 Save 和 Load 方法来为您执行此操作。

    [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();
            }
        }
    

    您可能需要添加设置器来创建国家和城市对象。或者,如果您将 LevelData 作为公共变量添加到脚本中,此结构将在 Unity 编辑器中可见。

    以及添加和保存级别:

    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);
    

    【讨论】:

    • 那么在完成关卡时,我将如何将其添加到数据结构中?
    • LevelData 的[,] 运算符返回对List&lt;Level&gt; 的引用,您只需在此列表中调用Add 即可添加您的新关卡。我已经更新了答案以显示这一点。
    • 所以,为了第一次填充关卡列表,我尝试了:
    • LevelData 数据;公共列表 级别;如果(_FirstPlay == 0){ _FirstPlay = 1; for (int i = 0; i
    • 但是我将如何加载和保存关卡列表?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2013-10-28
    相关资源
    最近更新 更多