C# 您将如何存储这些数据

C# 您将如何存储这些数据,c#,json,unity3d,save,load,C#,Json,Unity3d,Save,Load,我有一个游戏,我需要存储以下数据: 国家 城市 水平仪 有5个国家,每个国家5个城市,每个城市x个级别 存储这些数据的最佳方式是什么?我希望存储级别的详细信息,例如已完成的、花费的时间等 然后我希望通过Leveldata[countryindex,cityindex]访问级别数据 我想到了多维列表或字典,但想知道你们认为什么是最佳实践 我还需要将这些数据保存在JSON中 谢谢创建一个合适的数据模型,例如: public class Level { public TimeSpan Ti

我有一个游戏,我需要存储以下数据:

  • 国家
  • 城市
  • 水平仪
有5个国家,每个国家5个城市,每个城市x个级别

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


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

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

我还需要将这些数据保存在JSON中


谢谢

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

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

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();
        }
    }
[System.Serializable]
公营班级
{
公众智力得分;
// ...
}
[系统可序列化]
公营城市
{
公共列表级别=新列表();
}
[系统可序列化]
公营国家
{
公共列表城市=新列表();
公共城市本[int cityIndex]
{
得到
{
如果(城市指数<0 | |城市指数>=Cities.Count)
{
返回null;
}
其他的
{
返回城市[城市索引];
}
}
}
}
[系统可序列化]
公共类级别数据
{
公共列表国家=新列表();
公开列出此[int countryIndex,int cityIndex]
{
得到
{
如果(countryIndex<0 | | countryIndex>=Countries.Count)
{
返回null;
}
其他的
{
城市=国家[国家指数][城市指数];
如果(城市!=null)
{
返回城市。级别;
}
其他的
{
返回null;
}
}
}
}
公共void保存(字符串路径)
{
字符串json=JsonUtility.ToJson(this);
//注意:在此处添加IO异常处理!
System.IO.File.WriteAllText(路径,json);
}
公共静态级别数据加载(字符串路径)
{
//注意:添加检查路径是否存在,以及尝试/捕获解析错误
LevelData data=JsonUtility.FromJson(路径);
如果(数据!=null)
{
返回数据;
}
其他的
{
返回新的LevelData();
}
}
您可能需要添加setter来创建country和city对象。或者,如果将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 data=LevelData.Load(路径);
//这里我假设你们的国家和城市已经存在于这个结构中
列表级别=数据[1,2];
//todo:检查级别是否不为空!
级别l=新级别();
//将所有信息添加到l
增加(l);
数据保存(路径);

然后我希望通过Leveldata[countryindex,cityindex]访问级别数据,这将所有内容都分开?添加一个自定义索引器:整个内容可以包装到包含国家列表的数据类中。在每个级别(数据、国家、城市),您有一个字典字符串object,可以快速轻松地检索内容。然后在数据级别,您可以有一个方法,该方法采用两个参数,首先获取国家,然后从该国家获取城市,并返回一个级别列表。该方法还将执行必要的检查,以确保参数正确。因此,在级别上例如,我将如何将其添加到数据结构中?参见
[,]
LevelData运算符返回对
列表的引用
,您只需在此列表上调用
Add
,即可添加新的级别。我已更新了答案以显示这一点。因此,为了第一次填充级别列表,我尝试了:LevelData;public List levels;if(_FirstPlay==0){{u FirstPlay=1;for(int i=0;i<\u CountrySize;++i){for(int j=0;j<\u montersize;++j){for(int k=0;k<\u LevelSize[i,j];++k){Level l=new Level();//将所有信息添加到l个级别。添加(l);}}}//save}else//loadbut如何加载和保存级别列表?
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);