簡體   English   中英

從文件讀取配置信息

[英]Reading configuration info from a file

首先,我在代碼中有一個“項目”類,它具有不同的字段,例如“ id”,“ name”,“ isItem”,int,string和boolean。 我想創建一個函數,該函數根據給定的ID創建一個“項目”。 我希望此函數在Excel或易於手動編輯的數據庫中獲取對象。

就像: 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);

但這使我Cannot deserialize JSON to new instances of type 'ItemsContainer.' 我不知道為什么 Json是記事本中“輸出”的精確副本...

JSON看起來像

{
  "items": [
{
  "id": 1,
  "Name": "Wood",
  "isItem": true
},
{
  "id": 2,
  "Name": "Stone",
  "isItem": true
}
]
}

我的物品類別定義為:

public class item : MonoBehaviour {

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

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

像這樣使用配置數據時,最好的方法是將其另存為.json文件(僅是文本文件),並使用JsonUtility

如果設置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);

}

這將創建類似於以下內容的json文本

{
  "items": [
    {
      "Id": 1,
      "Name": "Wood",
      "isItem": true
    },
    {
      "Id": 2,
      "Name": "Stone",
      "isItem": true
    }
  ]
}

您可以編輯並添加更多項目,然后只需取回即可

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

我設法擁有json文本,但是如何例如調用id 45並根據其規格創建對象?

為此,您將需要將數據加載到以該項目的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()
    {
       //...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM