UnityEditor扩展之TreeView

最简单的TreeView使用示例,效果如下:
在这里插入图片描述
包括以下两个文件(完整代码真长,CSDN的markdown支持折叠代码么?怎么搞T.T ):
NetListenerWindow.cs:

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

public class NetListenerWindow : EditorWindow
{
    [SerializeField]
    TreeViewState m_TreeViewState;

    SimpleTreeView m_SimpleTreeView;
    List<string> netDatas = null;//保存接收到的网络消息

    [MenuItem("Tools/NetListener--消息包监听")]
    static void Init()
    {
        NetListenerWindow window = (NetListenerWindow)EditorWindow.GetWindow(typeof(NetListenerWindow), true, "网络监听", true);
        window.Show();
    }

    void OnEnable()
    {
        if (m_TreeViewState == null)
            m_TreeViewState = new TreeViewState();

        m_SimpleTreeView = new SimpleTreeView(m_TreeViewState);
        m_SimpleTreeView.Reload();
    }

    void OnGUI()
    {
        if (GUILayout.Button("清空数据", EditorStyles.toolbarButton, GUILayout.Width(position.width)))
        {
            m_SimpleTreeView.root.children.Clear();
            m_SimpleTreeView.Reload();
        }

        //创建一个横向流动布局,在一行中显示两个按钮
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("展开全部", GUILayout.Width(position.width / 2)))
        {
            m_SimpleTreeView.ExpandAll();
        }
        if (GUILayout.Button("折叠全部", GUILayout.Width(position.width / 2)))
        {
            m_SimpleTreeView.CollapseAll();
        }
        GUILayout.EndHorizontal();

        GetNetDatas();

        GUILayout.BeginScrollView(Vector2.zero, GUILayout.Width(position.width), GUILayout.Height(position.height));
        m_SimpleTreeView.OnGUI(new Rect(0, 0, position.width, position.height-200));
        GUILayout.EndScrollView();
    }


    /// <summary>
    /// 获取网络包数据
    /// </summary>
    void GetNetDatas()
    {
        if (netDatas != null)
        {
            return;
        }
        netDatas = new List<string> { "a", "b", "c", "d" };
        TreeViewItem item;
        int eventid;//消息id
        int retcode;//消息状态
        string content;//消息内容
        int index;

        for (int i = 0; i < 1000; i++)
        {
            eventid = i;
            retcode = i * 10;
            content = "content" + (eventid + retcode);
            index = m_SimpleTreeView.GetIndex();
            item = new TreeViewItem { id = index, depth = 0, displayName = "eventid:" + eventid + " retcode:" + retcode };
            m_SimpleTreeView.root.AddChild(item);
            AddContentItem(item, content, 1);
        }

        m_SimpleTreeView.Reload();
        netDatas.Clear();
    }

    /// <summary>
    /// 获取内容项目
    /// </summary>
    /// <param name="content"></param>
    /// <param name="depth"></param>
    void AddContentItem(TreeViewItem parentItem, string content, int depth)
    {
        TreeViewItem item;
        int index = m_SimpleTreeView.GetIndex();
        int eventid = depth + 1;
        int retcode = depth + 2;
        item = new TreeViewItem { id = index, depth = 0, displayName = "eventid:" + eventid + " retcode:" + retcode };
        parentItem.AddChild(item);
    }

    void OnInspectorUpdate()
    {
        //这里开启窗口的重绘,不然窗口信息不会刷新
        this.Repaint();
    }
}

SimpleTreeView.cs:

using UnityEditor;
using UnityEngine;
using UnityEditor.IMGUI.Controls;
//敲黑板,记得继承TreeView
class SimpleTreeView : TreeView
{
    public TreeViewItem root;//TreeView的根节点
    int itemIndex;//自增节点索引(每一个节点都需要一个id索引值,这里用自增型的)

    //构造函数
    public SimpleTreeView(TreeViewState treeViewState)
        : base(treeViewState)
    {
        itemIndex = 1;

        //TreeView里,depth代表节点的深度,最上层的根节点depth为-1
        root = new TreeViewItem { id = 0, depth = -1, displayName = "Root" };
        //初始化的时候需要在根节点下插入一个子节点,不然会报Children null的错误
        root.AddChild(new TreeViewItem { id = 1, depth = 0, displayName = "网络包接收" });
    }

    /// <summary>
    /// 获取自增的索引值
    /// </summary>
    /// <returns></returns>
    public int GetIndex()
    {
        itemIndex++;
        return itemIndex;
    }

    //重载BuildRoot方法
    protected override TreeViewItem BuildRoot()
    {
        SetupDepthsFromParentsAndChildren(root);
        return root;
    }
}

Ref:
Unity TreeView 网络收包显示工具(Json版)
上面代码是对这个博文的例子的简化
树状图来显示Json很合适

TreeView
Unity官方说明,其中的示例代码很丰富/实用,但有点复杂。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值