test.xml内容如下
<?xml version="1.0" encoding="utf-8" ?>
<config>
<Item1 name="custom"> //Item1指的是节点,可以通过判断 node.Name是否是等于“Item1”来判断取它的子元素的值
<ID id="0">美丽环境0</ID> //XmlElement el = (XmlElement)node3;是获得元素,获得xml中ID的属性值是通过 el. GetAttribute("id") 获得它的value通过el .InnerText
<ID id="1">美丽环境1</ID>
<ID id="2">美丽环境2</ID>
<ID id="3">美丽环境3</ID>
<ID id="4">美丽环境4</ID>
<ID id="5">美丽环境5</ID>
<ID id="6">美丽环境6</ID>
<ID id="7">美丽环境7</ID>
<ID id="8">美丽环境8</ID>
<ID id="9">美丽环境9</ID>
<ID id="10">美丽环境10</ID>
<ID id="11">美丽环境11</ID>
</Item1>
<Item2 name="material1">
<ID id="20">可爱动物20</ID>
<ID id="21">可爱动物21</ID>
<ID id="22">可爱动物22</ID>
<ID id="23">可爱动物23</ID>
<ID id="24">可爱动物24</ID>
<ID id="25">可爱动物25</ID>
<ID id="26">可爱动物26</ID>
<ID id="27">可爱动物27</ID>
<ID id="28">可爱动物28</ID>
<ID id="29">可爱动物29</ID>
</Item2>
<Item3 name="material2">
<ID id="30">精彩视频30</ID>
<ID id="31">精彩视频31</ID>
<ID id="32">精彩视频32</ID>
</Item3>
</config>
用脚本读取
using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
public class MyTest : MonoBehaviour {
public GameObject rotateBtn1;
public GameObject imageBtn;
public GameObject scrollView;
public GameObject scrollView1;
public GameObject scrollView2;
public GameObject btn1;
public GameObject btn2;
public GameObject btn3;
private string id;
private string name;
public struct Test //定义一个结构体
{
public string id;
}
List<Test> testlist = new List<Test>();
List<Test> testlist1 = new List<Test>();
List<Test> testlist2 = new List<Test>();
void Start()
{
UIEventListener.Get(rotateBtn1).onClick = RotateWin1;
UIEventListener.Get(btn1).onClick = OnClickWin1Btn1;
UIEventListener.Get(btn2).onClick = OnClickWin1Btn2;
UIEventListener.Get(btn3).onClick = OnClickWin1Btn3;
LoadXml();
}
void LoadXml()
{
XmlDocument testdoc = new XmlDocument();
testdoc.Load(Application.dataPath + "/test.xml");
XmlNode node = testdoc.SelectSingleNode("config");
foreach (XmlNode node2 in node)
{
if (node2.Name == "Item1")
{
foreach (XmlNode node3 in node2)
{
Test test;
XmlElement el = (XmlElement)node3;
test.id = el.GetAttribute("id");
testlist.Add(test);
}
}
if(node2.Name=="Item2")
{
foreach (XmlNode node4 in node2)
{
Test test;
XmlElement el = (XmlElement)node4;
test.id = el.GetAttribute("id");
testlist1.Add(test);
}
}
if(node2.Name=="Item3")
{
foreach (XmlNode node5 in node2)
{
Test test;
XmlElement el = (XmlElement)node5;
test.id = el.GetAttribute("id");
testlist2.Add(test);
}
}
}
ShowImageList(scrollView,testlist);
ShowImageList(scrollView1, testlist1);
ShowImageList(scrollView2, testlist2);
scrollView1.SetActive(false);
scrollView2.SetActive(false);
}
void ShowImageList(GameObject scrollView,List<Test> list)
{
int column = 3;
int row;
if (list.Count % 3 == 0)
{
row = list.Count / 3;
}
else
{
row = list.Count / 3 + 1;
}
Texture2D[] textures = new Texture2D[list.Count];
for (int i = 0; i < list.Count; i++)
{
id = list[i].id;
textures[i] = Resources.Load(id) as Texture2D;
}
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
GameObject go = NGUITools.AddChild(scrollView, imageBtn);
go.transform.localPosition = new Vector3(-93 + 82 * j + 3, 61 - i * 54 + 3, 0);
go.AddComponent<BoxCollider>();
go.AddComponent<UIDragScrollView>();
go.AddComponent<UIEventListener>();
go.AddComponent<UIButtonMessage>();
go.transform.GetChild(0).GetComponent<UITexture>().mainTexture = textures[j + i * 3];
if((j + i * 3 == list.Count-1)) return;
}
}
}
void RotateWin1(GameObject btn)
{
rotateBtn1.transform.parent.eulerAngles += new Vector3(0, 0, 180);
}
void OnClickWin1Btn1(GameObject btn)
{
//Debug.Log("window1btn");
scrollView.SetActive(true);
scrollView1.SetActive(false);
scrollView2.SetActive(false);
}
void OnClickWin1Btn2(GameObject btn)
{
scrollView1.SetActive(true);
scrollView2.SetActive(false);
scrollView.SetActive(false);
}
void OnClickWin1Btn3(GameObject btn)
{
scrollView2.SetActive(true);
scrollView.SetActive(false);
scrollView1.SetActive(false);
}
}