您的位置:首页 > 移动开发 > Unity3D

Unity基于DFGUI的TreeView设计

2017-03-01 16:54 459 查看
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 = 0; i < ItemList.Count; i++)
{
if (ItemList[i].ParentId == "0")
{
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 == 0)
{
dfPanel NodePanel = child.GetComponent<dfPanel>();
NodePanel.Find("Sprite").GetComponent<dfSprite>().IsVisible = false;
}
}
}

public void AddChildNodes(NodeMessage message)
{
for (int i = 0; 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, 0);
message.NodeList.Add(newNode);
AddChildNodes(newNode);
treeNode.IsVisible = false;
}
}
}

public void tubiaoOnClick(bool isShow, string tag, List<NodeMessage> nodelist)
{
for (int i = 0; i < ItemList.Count; i++)
{
if (ItemList[i].Id == tag)
{
SelectNodeList(isShow, nodelist);
}
}
}

void SelectNodeList(bool isShow, List<NodeMessage> nodelist)
{
for (int j = 0; 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(0).InnerText, xml.Item(1).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>1</id>
<parentid>0</parentid>
</node>
<node name="Node1 - 1">
<id>2</id>
<parentid>1</parentid>
</node>
<node name="Node1 - 1 - 1">
<id>3</id>
<parentid>2</parentid>
</node>
<node name="Node1 - 1 - 2">
<id>4</id>
<parentid>2</parentid>
</node>
<node name="Node1 - 2">
<id>5</id>
<parentid>1</parentid>
</node>
<node name="Node1 - 2 - 1">
<id>6</id>
<parentid>5</parentid>
</node>
<node name="Node1 - 2 - 2">
<id>7</id>
<parentid>5</parentid>
</node>
<node name="Node2">
<id>8</id>
<parentid>0</parentid>
</node>
<node name="Node2 - 1">
<id>9</id>
<parentid>8</parentid>
</node>
<node name="Node2 - 2">
<id>10</id>
<parentid>8</parentid>
</node>
<node name="Node2 - 2 - 1">
<id>11</id>
<parentid>10</parentid>
</node>
<node name="Node2 - 2 - 2">
<id>12</id>
<parentid>10</parentid>
</node>
<node name="Node2 - 3">
<id>13</id>
<parentid>8</parentid>
</node>
<node name="Node3">
<id>14</id>
<parentid>0</parentid>
</node>
<node name="Node3 - 1">
<id>15</id>
<parentid>14</parentid>
</node>
<node name="Node3 - 1 - 1">
<id>16</id>
<parentid>15</parentid>
</node>
<node name="Node3 - 1 - 2">
<id>17</id>
<parentid>15</parentid>
</node>
<node name="Node3 - 2">
<id>18</id>
<parentid>14</parentid>
</node>
<node name="Node3 - 2 - 1">
<id>19</id>
<parentid>18</parentid>
</node>
<node name="Node3 - 2 - 2">
<id>20</id>
<parentid>18</parentid>
</node>
</projects>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: