Unity 3D C#Strings.XML方法
我正在尝试为Unity3D创建一个方法,该方法允许我通过XML文件填充UI。i、 e.它们不必为每个按钮和标签命名,而是可以携带诸如“进度按钮”或“大文本”之类的通用名称,然后使用C#脚本与XML文件中的详细名称进行匹配 我已经广泛地搜索了教程、示例和指南,但我所找到的每一个都超出了我要完成的任务 理想情况下,我希望在XML文件中使用以下结构提供一个XML文件:Unity 3D C#Strings.XML方法,c#,xml,unity3d,unityscript,unity5,C#,Xml,Unity3d,Unityscript,Unity5,我正在尝试为Unity3D创建一个方法,该方法允许我通过XML文件填充UI。i、 e.它们不必为每个按钮和标签命名,而是可以携带诸如“进度按钮”或“大文本”之类的通用名称,然后使用C#脚本与XML文件中的详细名称进行匹配 我已经广泛地搜索了教程、示例和指南,但我所找到的每一个都超出了我要完成的任务 理想情况下,我希望在XML文件中使用以下结构提供一个XML文件: <?xml version="1.0" encoding="utf-8"?> <strings> <
<?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>
显然非常基本,但它可以工作。我知道我需要使用XML库来完成搜索(我理解字符串匹配)。我一直没有走到那一步
我很想看看任何关于XML使用的教程,或者如果有人能告诉我实现这一点需要使用哪些方法。就我个人而言,如果有人能提供示例代码的链接,我很想了解我试图做的事情背后的详细过程
提前谢谢 您可以使用.NET中的Xml.Serialization:
试试这样的方法
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]
}));
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串标题=”;
XDocument doc=XDocument.Parse(标题);
XElement字符串=(XElement)doc.FirstNode;
列表按钮=新列表(){
新列表(){“progressBTn”,“Next”},
新列表(){“reverseBth”,“Back”},
新列表(){“largeText”,“这是大文本”}
};
foreach(按钮中的列表按钮)
{
字符串。添加(
新元素(“字符串”,新对象[]{
新XAttribute(“名称”,按钮[0]),
按钮[1]
}));
}
}
}
}
该教程更加清晰,我发现,我的XML文件没有序列化,但没有填充,我不知道为什么。我将更详细地提出另一个问题。谢谢你的帮助!
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]
}));
}
}
}
}