• [Unity工具]json查看工具01:TreeView


    参考链接:

    https://docs.unity3d.com/Manual/TreeViewAPI.html

    效果如下:

    SimpleTreeView.cs

    在添加TreeViewItem节点时,要注意id要唯一,根节点的depth必须为-1

     1 using System.Collections.Generic;
     2 using UnityEditor.IMGUI.Controls;
     3 
     4 class SimpleTreeView : TreeView
     5 {
     6     public SimpleTreeView(TreeViewState treeViewState)
     7         : base(treeViewState)
     8     {
     9         Reload();
    10     }
    11 
    12     protected override TreeViewItem BuildRoot()
    13     {
    14         // BuildRoot is called every time Reload is called to ensure that TreeViewItems 
    15         // are created from data. Here we create a fixed set of items. In a real world example,
    16         // a data model should be passed into the TreeView and the items created from the model.
    17 
    18         // This section illustrates that IDs should be unique. The root item is required to 
    19         // have a depth of -1, and the rest of the items increment from that.
    20         var root = new TreeViewItem { id = 0, depth = -1, displayName = "Root" };
    21         var allItems = new List<TreeViewItem>
    22         {
    23             new TreeViewItem {id = 1, depth = 0, displayName = "Animals"},
    24             new TreeViewItem {id = 2, depth = 1, displayName = "Mammals"},
    25             new TreeViewItem {id = 3, depth = 2, displayName = "Tiger"},
    26             new TreeViewItem {id = 4, depth = 2, displayName = "Elephant"},
    27             new TreeViewItem {id = 5, depth = 2, displayName = "Okapi"},
    28             new TreeViewItem {id = 6, depth = 2, displayName = "Armadillo"},
    29             new TreeViewItem {id = 7, depth = 1, displayName = "Reptiles"},
    30             new TreeViewItem {id = 8, depth = 2, displayName = "Crocodile"},
    31             new TreeViewItem {id = 9, depth = 2, displayName = "Lizard"},
    32         };
    33 
    34         // Utility method that initializes the TreeViewItem.children and .parent for all items.
    35         SetupParentsAndChildrenFromDepths(root, allItems);
    36 
    37         // Return root of the tree
    38         return root;
    39     }
    40 }

    SimpleTreeViewWindow.cs

     1 using UnityEditor;
     2 using UnityEditor.IMGUI.Controls;
     3 using UnityEngine;
     4 
     5 class SimpleTreeViewWindow : EditorWindow
     6 {
     7     // SerializeField is used to ensure the view state is written to the window 
     8     // layout file. This means that the state survives restarting Unity as long as the window
     9     // is not closed. If the attribute is omitted then the state is still serialized/deserialized.
    10     [SerializeField] TreeViewState m_TreeViewState;
    11 
    12     //The TreeView is not serializable, so it should be reconstructed from the tree data.
    13     SimpleTreeView m_SimpleTreeView;
    14 
    15     void OnEnable()
    16     {
    17         // Check whether there is already a serialized view state (state 
    18         // that survived assembly reloading)
    19         if (m_TreeViewState == null)
    20             m_TreeViewState = new TreeViewState();
    21 
    22         m_SimpleTreeView = new SimpleTreeView(m_TreeViewState);
    23     }
    24 
    25     void OnGUI()
    26     {
    27         m_SimpleTreeView.OnGUI(new Rect(0, 0, position.width, position.height));
    28     }
    29 
    30     // Add menu named "My Window" to the Window menu
    31     [MenuItem("TreeView Examples/Simple Tree Window")]
    32     static void ShowWindow()
    33     {
    34         // Get existing open window or if none, make a new one:
    35         var window = GetWindow<SimpleTreeViewWindow>();
    36         window.titleContent = new GUIContent("My Window");
    37         window.Show();
    38     }
    39 }

    添加节点的另一种方法(其优点是可以先不指定depth):

     1 using System.Collections.Generic;
     2 using UnityEditor.IMGUI.Controls;
     3 
     4 class SimpleTreeView : TreeView
     5 {
     6     public SimpleTreeView(TreeViewState treeViewState)
     7         : base(treeViewState)
     8     {
     9         Reload();
    10     }
    11 
    12     protected override TreeViewItem BuildRoot()
    13     {
    14         var root = new TreeViewItem { id = 0, depth = -1, displayName = "Root" };
    15         var animals = new TreeViewItem { id = 1, displayName = "Animals" };
    16         var mammals = new TreeViewItem { id = 2, displayName = "Mammals" };
    17         var tiger = new TreeViewItem { id = 3, displayName = "Tiger" };
    18         var elephant = new TreeViewItem { id = 4, displayName = "Elephant" };
    19         var okapi = new TreeViewItem { id = 5, displayName = "Okapi" };
    20         var armadillo = new TreeViewItem { id = 6, displayName = "Armadillo" };
    21         var reptiles = new TreeViewItem { id = 7, displayName = "Reptiles" };
    22         var croco = new TreeViewItem { id = 8, displayName = "Crocodile" };
    23         var lizard = new TreeViewItem { id = 9, displayName = "Lizard" };
    24 
    25         root.AddChild(animals);
    26         animals.AddChild(mammals);
    27         animals.AddChild(reptiles);
    28         mammals.AddChild(tiger);
    29         mammals.AddChild(elephant);
    30         mammals.AddChild(okapi);
    31         mammals.AddChild(armadillo);
    32         reptiles.AddChild(croco);
    33         reptiles.AddChild(lizard);
    34 
    35         SetupDepthsFromParentsAndChildren(root);
    36 
    37         return root;
    38     }
    39 }
  • 相关阅读:
    未解之谜!代码中的超自然现象
    「IOI2017」西默夫 / Simurgh
    C++ primer读书笔记
    Vue仿Pc微信客户端
    @datetimeformat 与@JsonFormat的区别使用
    java 判断当前时间在时间范围内方法
    MySQL 索引优化全攻略
    王文 《深水炸弹》(五)关于我纯文学启蒙的一篇文章
    王文 《深水炸弹》(六)关于我纯文学启蒙的一篇文章
    王文 《深水炸弹》(三)关于我纯文学启蒙的一篇文章
  • 原文地址:https://www.cnblogs.com/lyh916/p/11729477.html
Copyright © 2020-2023  润新知