Unity 折叠树 EditorGUI.Foldout

需要两个类:树节点类和界面实现类


1:树节点类(TreeNode)

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TreeNode {

	public enum TreeNodeType
	{
		Item,
		Switch
	}

	public string name;
	public TreeNodeType nodeType = TreeNodeType.Item;
	public TreeNode parent;
	public List<TreeNode> children = null;
	public bool isOpen = false;
	public static TreeNode _instance = null;

	public static TreeNode Get()
	{
		if (_instance == null) 
		{
			_instance = new TreeNode ();
		}
		return _instance;
	}

	public void InsertNode(TreeNode node)
	{
		if (this.children == null)
		{
			this.children = new List<TreeNode> ();
		}
		children.Add (node);
		node.parent = this;
	}

	public void OpenAllNode(TreeNode node)
	{
		node.isOpen = true;
		if (node.children != null && node.children.Count > 0) 
		{
			for (int i = 0; i < node.children.Count; i++) 
			{
				OpenAllNode (node.children[i]);
			}
		}
	}

	public TreeNode GenerateFileTree(List<string> list)
	{
		TreeNode root = new TreeNode ();
		root = GenerateFileNode ("", "生物/", list);
		OpenAllNode (root);
		return root;
	}

	public TreeNode GenerateFileNode(string parentFullPath,string path,List<string> list)
	{
		TreeNode node = new TreeNode ();
		string[] segment = path.Split ('/'); 
		if (segment.Length > 1)
		{
			string name = segment[0];
			node.name = name;
			node.nodeType = TreeNodeType.Switch;
			string fullPath = parentFullPath + name+"/";
			List<string> allChildrenPath = list.FindAll (s=>
				{
					if (s.StartsWith(fullPath) && s!=fullPath)
					{
						return true;
					}
					return false;
				}
			);
			List<string> dirList = new List<string> ();
			for (int i = 0; i < allChildrenPath.Count; i++) 
			{
				string childPath = allChildrenPath [i].Remove (0, fullPath.Length);
				string[] childPathSegment = childPath.Split('/');
				if (childPathSegment.Length > 1) {
					string childDirPath = childPathSegment [0];
					if (!dirList.Contains (childDirPath)) {
						dirList.Add (childDirPath);
						TreeNode childNode = GenerateFileNode (fullPath, childDirPath + "/", list);
						node.InsertNode (childNode);
					}
				}
				else
				{
					TreeNode childNode = GenerateFileNode (fullPath, childPath, list);
					node.InsertNode (childNode);
				}
			}
		}
		else
		{
			node.name = path;
			node.nodeType = TreeNodeType.Item;
			list.Remove (path);
		}
		return node;
	}
		
}


2:界面实现类(CreateTreeList)

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class CreateTreeList:EditorWindow  {

	private List<string> list = new List<string> ();
	private static TreeNode root = null; 
	private TreeNode currentNode;
	private static CreateTreeList _instance = new CreateTreeList();
	private int treeIndex = 0;
	private static CreateTreeList window;     // 自定义窗体

	[MenuItem("H3D/构建树视图")]

	static void Init(){
		window = EditorWindow.GetWindow<CreateTreeList>();   // 创建自定义窗体
		window.titleContent = new GUIContent("构建树视图");         // 窗口的标题
		window.Show();
		_instance.GetAssets ();
		_instance.CreateTree ();
              // 创建树
	}

//	void Awake()
//	{
//		Debug.Log ("Awake");
//	}

	void Start()
	{
		Debug.Log ("Start");
	}

//	void Update()
//	{
//		Debug.Log ("Update");
//	}


	private void GetAssets()
	{
		list.Clear ();
		list.Add ("生物/动物");
		list.Add ("生物/动物/宠物/猫");
		list.Add ("生物/动物/宠物/狗");
//		list.Add ("生物/动物/野生/老虎");
//		list.Add ("生物/动物/野生/狮子");

		list.Add ("生物/植物");
		list.Add ("生物/植物/蔬菜/白菜");
		list.Add ("生物/植物/蔬菜/萝卜");
//		list.Add ("生物/植物/水果/苹果");
//		list.Add ("生物/植物/水果/橘子");
	
		Debug.Log ("获取数据完成");
	}

	private void CreateTree()
	{
		root = TreeNode.Get ().GenerateFileTree (list);
		Debug.Log ("生成文件树完成");
//		ShowFileTree (root, 0);
//		Debug.Log ("显示文件树完成");
	}


	private void ShowFileTree(TreeNode node, int level)
	{
		string prefix = "";
		for (int i = 0; i < level; i++)
		{
			prefix += "~";
		}
		Debug.Log (prefix + node.name);
		if (node == null || node.children == null) 
		{
			return;
		}
		for (int i = 0; i < node.children.Count; i++) 
		{
			ShowFileTree (node.children[i], level+1);
		}
	}


	private void DrawFileTree(TreeNode node, int level)
	{
		if (node == null) 
		{
			return;
		}
		GUIStyle style = new GUIStyle();
		style.normal.background = null;
		style.normal.textColor = Color.white;
		if (node == currentNode) 
		{
			style.normal.textColor = Color.red;
		}

		Rect rect = new Rect(5+20*level, 5+20*treeIndex, node.name.Length*25, 20);
		treeIndex++;

		if (node.nodeType == TreeNode.TreeNodeType.Switch) {
			node.isOpen = EditorGUI.Foldout (rect, node.isOpen, node.name, true);
		}
		else
		{
			if (GUI.Button (rect, node.name, style)) 
			{
				Debug.Log (node.name);
				currentNode = node;
			}
		}
	
		if (node==null || !node.isOpen || node.children == null) 
		{
			return;
		}
		for (int i = 0; i < node.children.Count; i++) 
		{
			DrawFileTree (node.children[i], level+1);
		}
	}

	void OnGUI()
	{
		treeIndex = 0;
		DrawFileTree (root, 0);
	}
}

效果图:


(点击后,被点击的节点红色显示,并在控制台输出被点击的节点的名字)


欢迎转载,转载请注明出处:http://blog.csdn.net/e295166319

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值