Unity编辑器 - TreeView控件笔记

用起来有一些规则,写个简单的案例以备查阅:

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

public class MyTreeView : TreeView {
    //搜索控件
    SearchField _searchField = new SearchField();

    List<object> allData = new List<object>();

    public MyTreeView(TreeViewState state) : base(state) {
        //构造函数, 需要一个TreeViewState
        // 对TreeView进行一些设置
        rowHeight = 20;
        showBorder = true;

        Reload();
    }

    // 绘制树,外部调用
    public override void OnGUI(Rect rect) {
        Rect srect = rect;
        srect.height = 18f;
        searchString = _searchField.OnGUI(rect, searchString);

        rect.y += 18f;

        base.OnGUI(rect);
    }

    protected override TreeViewItem BuildRoot() {
        //这里创建根节点, depth=-1时表示不可见
        return new TreeViewItem { id = 0, depth = -1, displayName = "root" };
    }

    protected override IList<TreeViewItem> BuildRows(TreeViewItem root) {
        //这里基于root节点建立树

        //清空原来的行
        IList<TreeViewItem> rows = GetRows();
        rows = new List<TreeViewItem>();


        if (!string.IsNullOrEmpty(searchString)) {
            //如果搜索控件有输入内容,则要根据搜索字符串来建立所有的行
            //自己根据 searchString 写搜索结果
        }
        else {
            //建立所有的行
            foreach (var data in allData) {
                TreeViewItem item = new TreeViewItem();
                //  item.id = data.。。。
                root.AddChild(item);
            }
        }
        //根据父子关系建立深度
        SetupDepthsFromParentsAndChildren(root);
        return base.BuildRows(root);
    }

    //可以写个公开办法从外部调用来建立树
    public void FetchData(List<object> alldata) {
        allData = alldata;
        //接着
        BuildRows(rootItem);
        //最后
        Reload();
    }
}

官方资料:

TreeView Examples project: here
TreeView Manual: here
TreeView API Documentation: here
MultiColumnHeader API Documentation: here

转载于:https://www.cnblogs.com/CloudLiu/p/10746054.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值