using UnityEngine;
using System.Collections; public class Item
{ public string Id; public string Name; public string ParentId; public Item(string name, string id, string parentid)
{
this.Name = name;
this.Id = id;
this.ParentId = parentid;
} }
using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class NodeMessage : MonoBehaviour
{ public string Tag; public List<NodeMessage> NodeList; }
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml; public class TreeNode : MonoBehaviour
{
public GameObject Panel;
public List<Item> ItemList;
dfScrollPanel scrollpanel; void Start()
{
scrollpanel = this.GetComponent<dfScrollPanel>();
ReadXML();
AddNodes();
} void AddNodes()
{
for (int i = ; i < ItemList.Count; i++)
{
if (ItemList[i].ParentId == "")
{
var treeNode = scrollpanel.AddPrefab(Panel);
var newNode = treeNode.GetComponent<NodeMessage>();
newNode.Tag = ItemList[i].Id;
treeNode.Find("Sprite").GetComponent<dfSprite>().SpriteName = "ArrowNormal";
treeNode.Find("Node").GetComponent<dfButton>().Text = ItemList[i].Name;
AddChildNodes(newNode);
}
}
foreach (Transform child in scrollpanel.transform)
{
var TreeNode = child.GetComponent<NodeMessage>();
if (TreeNode.NodeList.Count == )
{
dfPanel NodePanel = child.GetComponent<dfPanel>();
NodePanel.Find("Sprite").GetComponent<dfSprite>().IsVisible = false;
}
}
} public void AddChildNodes(NodeMessage message)
{
for (int i = ; i < ItemList.Count; i++)
{
if (ItemList[i].ParentId == message.Tag)
{
var treeNode = scrollpanel.AddPrefab(Panel);
var newNode = treeNode.GetComponent<NodeMessage>();
newNode.Tag = ItemList[i].Id;
treeNode.Find("Node").GetComponent<dfButton>().Text = ItemList[i].Name;
treeNode.Find("Sprite").GetComponent<dfSprite>().SpriteName = "ArrowNormal";
var parentTransform = message.GetComponent<dfPanel>().Find("Panel").transform;
treeNode.Find("Panel").transform.localPosition = new Vector2(parentTransform.localPosition.x + 0.1f, );
message.NodeList.Add(newNode);
AddChildNodes(newNode);
treeNode.IsVisible = false;
}
}
} public void tubiaoOnClick(bool isShow, string tag, List<NodeMessage> nodelist)
{
for (int i = ; i < ItemList.Count; i++)
{
if (ItemList[i].Id == tag)
{
SelectNodeList(isShow, nodelist);
}
}
} void SelectNodeList(bool isShow, List<NodeMessage> nodelist)
{
for (int j = ; j < nodelist.Count; j++)
{
dfPanel NodePanel = nodelist[j].GetComponent<dfPanel>();
var node = NodePanel.GetComponent<NodeMessage>();
if (isShow == true)
{
NodePanel.IsVisible = true;
}
else
{
tubiaoOnClick(isShow, node.Tag, node.NodeList);
NodePanel.IsVisible = false;
NodePanel.Find("Sprite").GetComponent<dfSprite>().SpriteName = "ArrowNormal";
NodePanel.Find("tubiao").GetComponent<TuBiao>().isShow = false; }
}
} void ReadXML()
{
XmlDocument doc = new XmlDocument();
doc.Load(Application.dataPath + @"\TreeView.xml");
XmlElement rootElem = doc.DocumentElement;
XmlNodeList itemlist = rootElem.GetElementsByTagName("node"); ItemList = new List<Item>();
foreach (XmlNode xn1 in itemlist)
{
XmlElement xe = (XmlElement)xn1;
XmlNodeList xml = xe.ChildNodes;
Item item = new Item(xe.GetAttribute("name").ToString(), xml.Item().InnerText, xml.Item().InnerText);
ItemList.Add(item);
} } }
using UnityEngine;
using System.Collections; public class TuBiao : MonoBehaviour
{ private TreeNode TreeNode;
dfSprite Sprite;
NodeMessage Message;
public bool isShow = false; void Start()
{
Sprite = this.transform.FindChild("Sprite").GetComponent<dfSprite>();
TreeNode = this.transform.parent.parent.parent.GetComponent<TreeNode>();
Message = this.transform.parent.parent.GetComponent<NodeMessage>();
} void OnClick()
{
if (Sprite.SpriteName == "ArrowNormal")
Sprite.SpriteName = "ArrowUnfold";
else
Sprite.SpriteName = "ArrowNormal";
isShow = !isShow;
TreeNode.tubiaoOnClick(isShow, Message.Tag, Message.NodeList); } }
XML如下:
<?xml version="1.0" encoding="utf-8"?>
<projects>
<node name="Node1">
<id></id>
<parentid></parentid>
</node>
<node name="Node1 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node1 - 1 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node1 - 1 - 2">
<id></id>
<parentid></parentid>
</node>
<node name="Node1 - 2">
<id></id>
<parentid></parentid>
</node>
<node name="Node1 - 2 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node1 - 2 - 2">
<id></id>
<parentid></parentid>
</node>
<node name="Node2">
<id></id>
<parentid></parentid>
</node>
<node name="Node2 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node2 - 2">
<id></id>
<parentid></parentid>
</node>
<node name="Node2 - 2 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node2 - 2 - 2">
<id></id>
<parentid></parentid>
</node>
<node name="Node2 - 3">
<id></id>
<parentid></parentid>
</node>
<node name="Node3">
<id></id>
<parentid></parentid>
</node>
<node name="Node3 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node3 - 1 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node3 - 1 - 2">
<id></id>
<parentid></parentid>
</node>
<node name="Node3 - 2">
<id></id>
<parentid></parentid>
</node>
<node name="Node3 - 2 - 1">
<id></id>
<parentid></parentid>
</node>
<node name="Node3 - 2 - 2">
<id></id>
<parentid></parentid>
</node>
</projects>