【问题标题】:Unity 3D C# Strings.XML MethodsUnity 3D C# Strings.XML 方法
【发布时间】:2016-06-26 18:38:00
【问题描述】:

我正在尝试为 Unity3D 创建一个允许我通过 XML 文件填充 UI 的方法。即,与其命名每个按钮和标签,它们可以携带通用名称,如“进度按钮”或“大文本”,然后使用 C# 脚本匹配 XML 文件中的详细名称。

我已经广泛搜索了教程、示例和指南,但我发现的每一个对于我想要完成的任务来说都是多余的。

理想情况下,我想在 XML 文件中使用以下结构提供一个 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<strings>
  <string name="progressBtn">Next</string>
  <string name="reverseBtn">Back</string>
  <string name="largeText">This is Large Text</string>
</strings>

我知道如何通过访问文本对象的属性来统一动态更改文本,所以我不担心这一步。我目前拥有的是这样的:

using UnityEngine;
using System.Collections;
using System.Xml;
using System.IO;

public class textParser : MonoBehaviour
{

public TextAsset targetXMLFile;
public GameObject uiObjectText;
string targetString;

// Use this for initialization
void Start ()
{
    checkFile();//Check for strings file.
    checkTarget(uiObjectText.name);//Check for the object name in the GUI object
}

// Update is called once per frame
void Update ()
{
    //TODO
}

//Check for strings file.
void checkFile()
{
    if (targetXMLFile == null) //If is Null, Log an Error
    {
        print("Error: target text file not loaded!");
    }
    else // If something, log the file name
    {
        print(targetXMLFile.name + " Target text file loaded!");
    }
}

//Check for the object name in the GUI object
void checkTarget(string target)
{
    if (target == null) //If is Null, Log an Error
    {
        print("Error: Unable to extract target ui object name!");
    }
    else// if something, Log the GUI Object name
    {
        print("Found: " + target + " In GUI.");
    }
}
}

显然非常基本,但它确实有效。我知道我需要使用 XML 库来完成我的搜索(我理解字符串匹配)。到达那一步是我难以捉摸的。

我很想看看任何更多关于 XML 使用的教程,或者如果有人可以告诉我我需要访问哪些方法来完成此操作。就个人而言,如果有人可以提供示例代码的链接,我很想了解我正在尝试做的事情背后的详细过程。

提前致谢!

【问题讨论】:

    标签: c# xml unity3d unityscript unity5


    【解决方案1】:

    您可以使用 .NET 中的 Xml.Serialization:

    https://unitygem.wordpress.com/xml-serialisation/

    【讨论】:

    • 嗯,该教程更加清晰,并且指向我发现的内容,我没有序列化我的 XML 文件,但没有填充,我不知道为什么。我将打开另一个更详细的问题。感谢您的帮助!
    【解决方案2】:

    试试这样的

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string header = "<?xml version=\"1.0\" encoding=\"utf-8\"?><strings></strings>";
                XDocument doc = XDocument.Parse(header);
                XElement strings = (XElement)doc.FirstNode;
    
                List<List<string>> buttons = new List<List<string>>() {
                                    new List<string>() {"progressBTn", "Next"},
                                    new List<string>() {"reverseBth", "Back"},
                                    new List<string>() {"largeText", "This is Large Text"}
                };
    
                foreach(List<string> button in buttons)
                {
                    strings.Add(
                        new XElement("string", new object[] {
                            new XAttribute("name", button[0]),
                            button[1]
                        }));
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-25
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多