仅适用于2018之前的版本,有UIElements或者UIWidgets的最好用新的

基本实现

树节点

public interface ITreeNode
{
ITreeNode Parent { get; set; }
List<ITreeNode> Children { get; }
bool IsOpen { get; set; }
string DisplayString { get; } void AddChild(ITreeNode node);
void Dispose();
} public class TreeNode : ITreeNode
{
private string m_Message; public ITreeNode Parent { get; set; }
List<ITreeNode> Children { get; private set; }
bool IsOpen { get; set; }
string DisplayString { get { return m_Message; } } public TreeNode(string message)
{
m_Message = message;
} public void AddChild(ITreeNode node)
{
if (Children == null)
{
Children = new List<ITreeNode>();
} node.Parent = this;
Children.Add(node);
} public void Dispose()
{
if (Children = null)
{
return;
} foreach (ITreeNode node in Children)
{
node.Dispose();
} Children.Clear();
Children = null;
}
}

窗口

public class TestWindow : EditorWindow
{
[MenuItem("Tools/Test")]
public static void OpenWindow()
{
GetWindow<TestWindow>();
} private static float s_LineHeight = 20;
private static float s_Indentation = 20;
private static float s_CharacterWidth = 20; private TreeNode m_Root; private void OnEnable()
{
m_Root = new TreeNode("Root");
m_Root.AddChild(new TreeNode("Child1"));
m_Root.AddChild(new TreeNode("Child2"));
var child3 = new TreeNode("Child3");
m_Root.AddChild(child3);
child3.AddChild(new TreeNode("Child3-1"));
} private void OnGUI()
{
if (m_Root == null)
{
return;
} int depth = 0;
int index = 0;
DrawNode(m_Root, ref depth, ref index);
} private void DrawNode(ITreeNode node, ref int depth, ref int index)
{
string content = node.DisplayString;
Rect rect = new Rect(s_Indentation * depth, s_LineHeight * index, content.Length * s_CharacterWidth, s_LineHeight);
node.IsOpen = EditorGUI.Foldout(rect, node.IsOpen, content, true);
index++; if (node.IsOpen && node.Children != null)
{
depth++;
foreach(ITreeNode child in node.Children)
{
DrawNode(child, ref depth, ref index);
}
depth--;
}
}
}

不显示多余的折叠箭头

最简单的方法是改用Button

public class TestWindow : EditorWindow
{
// ...
private GUIStyle m_NormalStyle;
private GUIStyle m_FoldoutStyle; private void OnEnable()
{
//...
m_FoldoutStyle = new GUIStyle("Foldout");
m_NormalStyle = new GUIStyle();
m_NormalStyle.normal.background = null;
m_NormalStyle.normal.textColor = m_FoldoutStyle.normal.textColor;
} private void DrawNode(ITreeNode node, ref int depth, ref int index)
{
//... // node.IsOpen = EditorGUI.Foldout(rect, node.IsOpen, content, true);
bool isOpen = node.IsOpen;
GUIStyle style = node.Children == null ? m_NormalStyle : m_FoldoutStyle;
if (GUI.Button(rect, content, style))
{
node.IsOpen = !isOpen;
node.OnClick();
} //...
}
}

Unity - EditorWindow 折叠树显示(IMGUI)的更多相关文章

  1. unity editor 折叠树

    https://blog.csdn.net/e295166319/article/details/52370575 需要两个类:树节点类和界面实现类 1:树节点类(TreeNode) using Un ...

  2. 帆软报表(finereport) 折叠树

    在进行展现数据时,希望模板的数据是可以动态折叠的,即点击数据前面的加号才展开对应下面的数据,可通过树节点按钮实现折叠树效果 实现思路: 1.这里建立一个内置数据集 添加数据 设置模板样式,添加颜色和对 ...

  3. vue 仿zTree折叠树

    需求: vue实现仿zTree折叠树,此文章仅作为记录文档. 实现: <template> <div class="line-tree"> <div ...

  4. layout折叠后显示标题

    Easyui的layout折叠后显示怎样可以显示标题 //在layout的panle全局配置中,增加一个onCollapse处理title$.extend($.fn.layout.paneldefau ...

  5. web页面显示折叠树菜单笔记

    zTree -- jQuery 树插件 http://pan.baidu.com/s/1skwh94h

  6. Unity编辑器的扩展:IMGUI

    IMGUI 介绍 所有关于 Editor 的相关 UI,包括 Inspector.Hierarchy.Window.Game 视图上动态创建的那些半透明 UI.还有 Scene 视图上可添加的辅助显示 ...

  7. Unity EditorWindow知识记录

    1.创建EditorWindow using UnityEditor; using UnityEngine; public class ZZEditorWindow : EditorWindow { ...

  8. d3.js之树形折叠树

    1.效果 children和_children 2.技术分解 2.1折叠函数 // (1) 递归调用,有子孙的就把children(显示)给_children(不显示)暂存,便于折叠, functio ...

  9. jquery easyui菜单树显示

    目前做了一个easyui项目需要显示多级菜单,菜单配置到数据库中,因此每级菜单都需要到数据库中取,用了jQuery EasyUI方便多了. 效果体验:http://hovertree.com/texi ...

  10. Unity光照图UV显示

    美术的同学觉得 Unity 光照图烘焙的不够美丽,需要在 ps 里修一修,但是不知道每个物体对应的光照图在哪个区域,UV 是如何分布的,于是要求写一个工具显示,于是有了下面这个: 打开场景自动读取当前 ...

随机推荐

  1. 记一次 .NET 在线客服系统同时支持 SQL Server 和 MySQL 没卡死分析

    前段时间我发表了一系列文章,开始介绍基于 .net core 的在线客服系统开发过程. 有很多朋友一直提出希望能够支持 MySQL 数据库,考虑到已经有朋友在用 SQL Server,我在升级的过程中 ...

  2. 根据模板动态生成word(一)使用freemarker生成word

    @ 目录 一.准备模板 1.创建模板文件 2.处理模板 2.1 处理普通文本 2.2 处理表格 2.3 处理图片 二.项目代码 1.引入依赖 2.生成代码 三.验证生成word 一.准备模板 1.创建 ...

  3. .NET周刊【7月第2期 2023-07-09】

    由于这周比较忙,只给出了标题和链接,没有具体的简介. 另外根据粉丝朋友的反馈,".NET周报" 更名为 ".NET周刊",希望大家喜欢 : ) 国内文章 Ava ...

  4. 根据模板动态生成word(三)使用poi-tl生成word

    @ 目录 一.前言 1.什么是poi-tl 2.官方信息 2.1 源码仓库 2.2 中文文档 2.3 开源协议 3.poi-tl的优势 3.1 poi-tl和其他模板引擎的对比 3.2 poi-tl ...

  5. 【Redis】八股文(一)

    什么是Redis 基于key-value存储结构的NoSQL数据库 提供了String, Map, Set, ZSet, List等多种数据类型 功能丰富:支持发布订阅模式,能够为数据设置过期时间,能 ...

  6. Swift函数调用方式浅析

    函数的调用机制   函数的调用机制是在函数调用时通过那种路径走到最终调用函数地址的机制. 在编程语言中,函数的调用机制有三种 1.静态调用:编译期就确定了函数内存地址,执行效率最高,还可以使用编译器优 ...

  7. 好用工具:Save All Resources

    说明 该插件可以下载网页中的所有资源 使用方法

  8. 文件上传的multipart/form-data属性,你理解了吗

    form表单经常用于前端发送请求,比如:用户填写信息.选择数据.上传文件,对于不同的场景,上传数据的格式也会有些区别. action action 表示该请求的 url 地址,定义在form上,请求的 ...

  9. Programming abstractions in C阅读笔记:p84-p87

    <Programming Abstractions In C>学习第43天,p84-p87总结. 一.技术总结 1.record record也称为structure(结构体),是一种数据 ...

  10. 解析PPTX 遇到异常:“\b”(十六进制值 0x08)是无效的字符。

    问题描述: 通过DocumentFormat.OpenXml解析PPTX文件时遇到异常:"\b"(十六进制值 0x08)是无效的字符,查看文件发现存在乱码,乱码的十六进制值刚好时异 ...