Unity 2D-通过类变量的值从类列表中获取值

时间:2019-01-09 20:16:02

标签: c# xml list class unity3d

我的Xml加载程序将xml文件中的数据保存到class列表中。对话框是具有2个属性的类:id和text,其中保存xml文件的内容。我想通过id变量的值从DialogContainer.cs中的列表中获取DialogLoader.cs中的“文本”变量值。我怎样才能做到这一点?这可能很简单,但我不知道如何。

dialogs.xml

<?xml version="1.0" encoding="UTF-8"?>
<DialogCollection>
  <Dialogs>
    <Dialog id="1">
      <Text>Hello my friend</Text>
    </Dialog>
    <Dialog id="2">
      <Text>Hello my grandma</Text>
    </Dialog>
    <Dialog id="3">
      <Text>Hello my grandpa</Text>
    </Dialog>
  </Dialogs>
</DialogCollection>

DialogLoader.cs

public class DialogLoader : MonoBehaviour
{

public const string path = "dialogs";

void Start()
{
    DialogContainer dc = DialogContainer.Load(path);
//There I want to get text for example by id 1 (Hello my friend)
    foreach (Dialog item in dc.dialogs)
    {
        print(item.text);
    }
}

DialogContainer.cs

[XmlRoot("DialogCollection")]
public class DialogContainer
{

    [XmlArray("Dialogs")]
    [XmlArrayItem("Dialog")]
    public List<Dialog> dialogs = new List<Dialog>();

    public static DialogContainer Load(string path)
    {
        TextAsset _xml = Resources.Load<TextAsset>(path);

        XmlSerializer serializer = new XmlSerializer(typeof(DialogContainer));

        StringReader reader = new StringReader(_xml.text);

        DialogContainer dialogs = serializer.Deserialize(reader) as DialogContainer;


        reader.Close();

        return dialogs;
    }


}

Dialog.cs

public class Dialog
{

    [XmlAttribute("id")]
    public string id;

    [XmlElement("Text")]
    public string text;


}

2 个答案:

答案 0 :(得分:0)

您可以添加类似的字典

[XmlRoot("DialogCollection")]
public class DialogContainer
{
    [XmlArray("Dialogs")]
    [XmlArrayItem("Dialog")]
    public List<Dialog> dialogs = new List<Dialog>();

    public Dictionary<string, string> IdToText = new Dictionary<string, string>();

    public static DialogContainer Load(string path)
    {
        TextAsset _xml = Resources.Load<TextAsset>(path);

        XmlSerializer serializer = new XmlSerializer(typeof(DialogContainer));

        // use a better variable name since "dialogs" is also a field of it
        DialogContainer dialogContainer;

        // use "using" for disposables
        using(StringReader reader = new StringReader(_xml.text))
        {
            dialogContainer = serializer.Deserialize(reader) as DialogContainer;
        }

        // reset dictionary
        dialogContainer.IdToText.Clear();
        foreach(var entry in dialogContainer.dialogs)
        {
            dialogContainer.IdToText.Add(entry.id, entry.text);
        }

        return dialogContainer;
    }
}

比以后您可以使用的

aDialogContainer.idTotext["1"];

请注意,这将引发异常,因为搜索关键字不在字典中!因此,请参考下面的解决方案。


另一种选择是实现一个getter方法而不是字典,以使用LINQ: FirstOrDefault即时查找正确的文本:

public string IdtoText(string id)
{
    var foundDialog = dialogs.FirstOrDefault(dialog => string.Equals(dialog.id, id));

    //         here you can decide whether to        |
    //         return null or "" if no element found v 
    return foundDialog != null ? foundDialog.text : "";
}

比您将要使用的

aDialogContainer.IdTotext("1");

答案 1 :(得分:0)

您可以使用Linq过滤对话框列表,然后仅返回与您的条件匹配的第一个对话框。

类似这样的东西:

public Dialog GetSingleDialog(string id, List<Dialog> dialogs){

return dialogs.Where(x=>x.id==id).FirstOrDefault();
}

您可以这样使用:

DialogContainer dc = DialogContainer.Load(path);
//Get list of dialogs
var dialogs=dc.dialogs;
var theDialog=GetSingleDialog("1",dialogs);

//获取文字

Debug.Log(theDialog.Text);