Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文件中读取配置信息_C#_Unity3d - Fatal编程技术网

C# 从文件中读取配置信息

C# 从文件中读取配置信息,c#,unity3d,C#,Unity3d,首先,我的代码中有一个类'item',它分别有'id','name','isItem',int,string和boolean等不同的字段。我想创建一个函数,从给定的id创建一个“项”。我想要这个函数在数据库中获取对象,比如Excel或其他易于手动编辑的东西 就像: 好的,然后我尝试创建一个ItemsContainer类,如: public class ItemsContainer : MonoBehaviour { public item[] items; } 然后我尝试解析如下:

首先,我的代码中有一个类'item',它分别有'id','name','isItem',int,string和boolean等不同的字段。我想创建一个函数,从给定的id创建一个“项”。我想要这个函数在数据库中获取对象,比如Excel或其他易于手动编辑的东西

就像:

好的,然后我尝试创建一个ItemsContainer类,如:

public class ItemsContainer : MonoBehaviour {
    public item[] items;
}
然后我尝试解析如下:

var json = System.IO.File.ReadAllText (Application.dataPath + "\\" + "json.txt");
ItemsContainer Items = JsonUtility.FromJson<ItemsContainer> (json);

Dictionary<int, item> its = new Dictionary<int, item> ();
its = Items.items.ToDictionary (x => x.id);
我的项目类定义为:

public class item : MonoBehaviour {

public int id;
public string Name;
public bool isItem;

public item () {
    Name = "Nothing";
    isItem = false;
    }
}

当处理这样的配置数据时,最好的方法是将其保存为.json文件(它只是一个文本文件)并使用

如果设置
JsonUtility.ToJson(yourClass,true)
第二个参数将打开“prettyPrint”,这使得在记事本之类的东西中编辑更容易。只需通过代码创建一些“占位符”项,并调用
.ToJson()
获取初始文件,然后您就可以编辑它并添加其余的内容

[Serializable]
public class MyItem
{
    public int Id;
    public string Name;
    public bool isItem;
}

[Serializable]
public class ItemsContainer
{
    public MyItem[] items;
}

//elsewhere
public void SaveTemplate()
{
    List<MyItem> itemList = new List<MyItem>();
    itemList.Add(new MyItem { Id = 1, Name = "Wood", isItem = true });
    itemList.Add(new MyItem { Id = 2, Name = "Stone", isItem = true });

    ItemsContainer itemsContainer= new ItemsContainer ();
    itemsContainer.items = itemList.ToArray();

    string json = JsonUtility.ToJson(itemsContainer, true);
    YourFunctionToSaveTheTextSomewhere(json);

}
你可以编辑和添加更多的项目,然后得到回来你只要做

public MyItem[] GetItems()
{
    string json = GetJsonTextSomehow();
    ItemsContainer container = JsonUtility.FromJson<ItemsContainer>(json);
    return container.items;
}
publicMyItem[]GetItems()
{
字符串json=getjsontext();
ItemsContainer容器=JsonUtility.FromJson(json);
归还集装箱物品;
}
我成功地获得了json文本,但是如何调用ID45,并根据其规范创建一个对象呢

为此,您需要将数据加载到由项的id键入的字典中,您的代码可以如下所示

public class ItemManager : MonoBehavior
{
    private Dictionary<int, MyItem> _itemDict;

    public void Awake()
    {
        var items = GetItems();

        //Makes a new dictionary from the array using the field Id as the key.
        _itemDict = items.ToDictionary(x => x.Id); 
    }

    public MyItem GetItem(int itemId)
    {
        return _itemDict[itemId];
    }

    private MyItem[] GetItems()
    {
        string json = GetJsonTextSomehow();
        ItemsContainer container = JsonUtility.FromJson<ItemsContainer>(json);
        return container.items;
    }

    private string GetJsonTextSomehow()
    {
       //...
    }
}
公共类ItemManager:MonoBehavior
{
私人词典;
公共图书馆
{
var items=GetItems();
//使用字段Id作为键从数组生成新字典。
_itemDict=items.ToDictionary(x=>x.Id);
}
公共MyItem GetItem(int itemId)
{
返回_itemDict[itemId];
}
私有MyItem[]GetItems()
{
字符串json=getjsontext();
ItemsContainer容器=JsonUtility.FromJson(json);
归还集装箱物品;
}
私有字符串getJSontextName()
{
//...
}
}

听起来不错。你听说过实体框架吗?从来没有,我应该看一下吗?我不知道实体框架是否能在Unity3d中工作。您是否考虑过将其保存为.json文件(它只是一个文本文件)并使用?这正是我想要的,但似乎没有。。。我怎样才能通过它?Excel不是一个数据库。我不能让它工作,我很难过,“无法将JSON反序列化为'ItemsContainer'类型的新实例”。@Ay0m3使用您使用的代码和您试图反馈给它的JSON更新您的问题。请参阅我对该问题的评论,另外,请删除我的答案中以前的注释以进行清理。在编辑的问题中,在这两个类上方没有
[Serializable]
,是否存在于真实的代码中?如果是,请更新您的问题。另外,类
不应派生自
单行为
,从
单行为
派生的类不能从json序列化。
public MyItem[] GetItems()
{
    string json = GetJsonTextSomehow();
    ItemsContainer container = JsonUtility.FromJson<ItemsContainer>(json);
    return container.items;
}
public class ItemManager : MonoBehavior
{
    private Dictionary<int, MyItem> _itemDict;

    public void Awake()
    {
        var items = GetItems();

        //Makes a new dictionary from the array using the field Id as the key.
        _itemDict = items.ToDictionary(x => x.Id); 
    }

    public MyItem GetItem(int itemId)
    {
        return _itemDict[itemId];
    }

    private MyItem[] GetItems()
    {
        string json = GetJsonTextSomehow();
        ItemsContainer container = JsonUtility.FromJson<ItemsContainer>(json);
        return container.items;
    }

    private string GetJsonTextSomehow()
    {
       //...
    }
}