Unity——读取xml文件生成一级树形菜单
2017-04-24 10:31
330 查看
从毕业到工作,经过多个项目的历练,我的编程能力可以说已经经历了从菜鸟到小有所成的状态。在这个互联网的时代,在网上常常能够看到各种编程大牛写的一些关于自己编程中的经验知识点。
很多时候,看着他们的文章,总会觉得,自己也成为一名程序员许多年了,就算没有一些特别新颖,高超的技术能力,但是在做过的项目中,也是常常会遇到各式各样的问题,或者一些比较实用的方法之类的东西。所以在此,我会记录下来自己在编程岁月里的点点滴滴,也让自己觉得多年的工作还是有所收获的。
今天,我想介绍一下在做Unity项目中所使用的一个树形菜单。 树形菜单可以说是一个比较常用的导航形式,无论是windows中的文件夹、或者是一些视频网站、社交网站中都会使用到这种菜单形式。
首先,因为我希望树形菜单能够编辑的更加灵活,所以我将树形菜单的内容都存储在xml文件内,通过读取xml里的内容,来动态创建树形菜单的各个节点。
我将xml文件放在Resources目录下,所以取文件的路径我定义为:
private string path = UnityEngine.Application.dataPath + "/Resources/";
注:需要注意的是,通过Resources读取文件的时候是不需要加后缀名的。
xml文件内容如下:
这里,我使用的NGUI插件,先创建树节点预置物。
最后,编写好创建树的代码,将其挂在主相机上,并运行,就能生成树形菜单。
using UnityEngine;
using System.Collections;
using System.Xml;
public class main : MonoBehaviour {
public string filename;
private string path = UnityEngine.Application.dataPath + "/Resources/";
public GameObject UIRoot;
// Use this for initialization
void Start () {
LoadXmlCreateTree("tree");
}
// Update is called once per frame
void Update () {
}
private void LoadXmlCreateTree(string name)
{
#region //读取xml文件
XmlDocument treeXml = new XmlDocument();
TextAsset textAsset = (TextAsset)Resources.Load(path+name, typeof(TextAsset));
if (textAsset == null)
Debug.Log(path + name);
treeXml.LoadXml(textAsset.text);
#endregion
#region //生成树
GameObject treeNode = (GameObject)Resources.Load("node");
treeNode = Instantiate(treeNode);
XmlNodeList xmlNodeList = treeXml.SelectSingleNode("root").ChildNodes;
foreach (XmlElement xl1 in xmlNodeList)
{
treeNode.transform.GetChild(0).GetComponent<UILabel>().text = xl1.GetAttribute("name");
treeNode.transform.parent = UIRoot.transform;
treeNode.transform.localPosition = new Vector3(-300, 200, 0);
treeNode.transform.localScale = new Vector3(1, 1, 1);
if (xl1.HasChildNodes)
{
int i = 1;
foreach (XmlElement xl2 in xl1)
{
GameObject childNode = (GameObject)Resources.Load("node");
childNode = Instantiate(childNode);
childNode.transform.GetChild(0).GetComponent<UILabel>().text = xl2.GetAttribute("name");
childNode.transform.parent = treeNode.transform;
childNode.transform.localPosition = new Vector3(50, -50*i, 0);
childNode.transform.localScale = new Vector3(1, 1, 1);
i++;
}
}
}
#endregion
}
}
效果展示:
很多时候,看着他们的文章,总会觉得,自己也成为一名程序员许多年了,就算没有一些特别新颖,高超的技术能力,但是在做过的项目中,也是常常会遇到各式各样的问题,或者一些比较实用的方法之类的东西。所以在此,我会记录下来自己在编程岁月里的点点滴滴,也让自己觉得多年的工作还是有所收获的。
今天,我想介绍一下在做Unity项目中所使用的一个树形菜单。 树形菜单可以说是一个比较常用的导航形式,无论是windows中的文件夹、或者是一些视频网站、社交网站中都会使用到这种菜单形式。
首先,因为我希望树形菜单能够编辑的更加灵活,所以我将树形菜单的内容都存储在xml文件内,通过读取xml里的内容,来动态创建树形菜单的各个节点。
我将xml文件放在Resources目录下,所以取文件的路径我定义为:
private string path = UnityEngine.Application.dataPath + "/Resources/";
注:需要注意的是,通过Resources读取文件的时候是不需要加后缀名的。
xml文件内容如下:
这里,我使用的NGUI插件,先创建树节点预置物。
最后,编写好创建树的代码,将其挂在主相机上,并运行,就能生成树形菜单。
using UnityEngine;
using System.Collections;
using System.Xml;
public class main : MonoBehaviour {
public string filename;
private string path = UnityEngine.Application.dataPath + "/Resources/";
public GameObject UIRoot;
// Use this for initialization
void Start () {
LoadXmlCreateTree("tree");
}
// Update is called once per frame
void Update () {
}
private void LoadXmlCreateTree(string name)
{
#region //读取xml文件
XmlDocument treeXml = new XmlDocument();
TextAsset textAsset = (TextAsset)Resources.Load(path+name, typeof(TextAsset));
if (textAsset == null)
Debug.Log(path + name);
treeXml.LoadXml(textAsset.text);
#endregion
#region //生成树
GameObject treeNode = (GameObject)Resources.Load("node");
treeNode = Instantiate(treeNode);
XmlNodeList xmlNodeList = treeXml.SelectSingleNode("root").ChildNodes;
foreach (XmlElement xl1 in xmlNodeList)
{
treeNode.transform.GetChild(0).GetComponent<UILabel>().text = xl1.GetAttribute("name");
treeNode.transform.parent = UIRoot.transform;
treeNode.transform.localPosition = new Vector3(-300, 200, 0);
treeNode.transform.localScale = new Vector3(1, 1, 1);
if (xl1.HasChildNodes)
{
int i = 1;
foreach (XmlElement xl2 in xl1)
{
GameObject childNode = (GameObject)Resources.Load("node");
childNode = Instantiate(childNode);
childNode.transform.GetChild(0).GetComponent<UILabel>().text = xl2.GetAttribute("name");
childNode.transform.parent = treeNode.transform;
childNode.transform.localPosition = new Vector3(50, -50*i, 0);
childNode.transform.localScale = new Vector3(1, 1, 1);
i++;
}
}
}
#endregion
}
}
效果展示:
相关文章推荐
- c#从数据库读取数据动态生成树形菜单
- 从数据库读取数据动态生成树形菜单示例
- C# 中读取xml 并 生成菜单项 树形菜单
- 使用sax读取xml文件生成txt文件。
- 关于android开发添加菜单XML文件之后无法在R.java中生成ID的问题
- 使用Pull解析器读取XML文件和生成XML文件
- Python读取yaml生成菜单
- 在asp.net里怎么跟据用户权限来生成树形菜单
- JQuery实现动态生成树形菜单
- unity 生成webgl读取assetBundle包
- easyui扁平Json生成树形菜单
- 通过xStream读取xml文件生成Java对象和Java对象生成xml文件
- php小代码----树形菜单生成
- 使用Pull解析器生成XML文件和读取xml文件
- 读取INI文件调用DLL动态生成菜单
- C# 编写代码生成XML文件,并从XML中读取出数据
- SolpartMenu的使用:(一)、在ASP.NET页面中使用SolpartMenu控件之使用XML文件生成菜单
- JavaScript动态生成树形菜单创建表格
- 移动项目开发笔记(动态生成xml文件生成导航菜单(续))