UnityEditor扩展之TreeView

最简单的TreeView使用示例,效果如下:
UnityEditor扩展之TreeView_第1张图片
包括以下两个文件(完整代码真长,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();
    }


    /// 
    /// 获取网络包数据
    /// 
    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();
    }

    /// 
    /// 获取内容项目
    /// 
    /// 
    /// 
    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 = "网络包接收" });
    }

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

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

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

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

你可能感兴趣的:(Unity)