Deparei-me com este erro ao tentar dar Load a um ficheiro XML através de três scripts: Um chamado Item, outro ItemCollection e o script para carregar o ficheiro XML.
Item:
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using UnityEngine;
public class Item : MonoBehaviour {
[XmlAttribute("id")]
public string id;
[XmlAttribute("Nome")]
public string Nome;
[XmlAttribute("Preco")]
public float Preco;
}
ItemCollection
using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;
using System.IO;
[XmlRoot("DataBase")]
public class ItemCollection : MonoBehaviour {
[XmlArray("Produtos")]
[XmlArrayItem("Produto")]
public List<Item> Produtos = new List<Item>();
public static ItemCollection Load(string path)
{
TextAsset _xml_ = Resources.Load <TextAsset>(path);
XmlSerializer serializer = new XmlSerializer(typeof(ItemCollection));
StringReader reader = new StringReader(_xml_.text);
ItemCollection Produtos = serializer.Deserialize(reader) as ItemCollection;
reader.Close();
return Produtos;
}
}
Loader:
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
public class LoadXml : MonoBehaviour {
public const string path = "Nomes_E_Precos";
void Start () {
ItemCollection itemCollection = ItemCollection.Load(path);
foreach (Item Produto in itemCollection.Produtos)
{
Debug.Log(Produto.Nome + Produto.id + Produto.Preco);
}
}
}
No entanto, dá-me o erro(na engine) no título:
To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object)
Alguma solução?