Esempio n. 1
0
        IList <ITreeViewItem> GetTreeItemsFromVisualTreeIncludingParent(VisualElement parent, ref int nextId)
        {
            if (parent == null)
            {
                return(null);
            }

            var items = new List <ITreeViewItem>();
            var id    = nextId;

            nextId++;

            var item = new TreeViewItem <VisualElement>(id, parent);

            items.Add(item);

            var childItems = GetTreeItemsFromVisualTree(parent, ref nextId);

            if (childItems == null)
            {
                return(items);
            }

            item.AddChildren(childItems);

            return(items);
        }
Esempio n. 2
0
        private void AddTreeItemsForElement(IList <ITreeViewItem> items, VisualElement ve, ref int nextId)
        {
            if (ve == null)
            {
                return;
            }

            int count = ve.hierarchy.childCount;

            if (count == 0)
            {
                return;
            }

            for (int i = 0; i < count; i++)
            {
                var child = ve.hierarchy[i];

                var treeItem = new TreeViewItem <VisualElement>(nextId, child);
                items.Add(treeItem);
                nextId++;

                var childItems = new List <ITreeViewItem>();
                AddTreeItemsForElement(childItems, child, ref nextId);
                if (childItems.Count == 0)
                {
                    continue;
                }

                treeItem.AddChildren(childItems);
            }
        }
Esempio n. 3
0
        IList <ITreeViewItem> GetTreeItemsFromVisualTree(VisualElement parent, ref int nextId)
        {
            List <ITreeViewItem> items = null;

            if (parent == null)
            {
                return(null);
            }

            int count = parent.hierarchy.childCount;

            if (count == 0)
            {
                return(null);
            }

            for (int i = 0; i < count; i++)
            {
                var element = parent.hierarchy[i];

                if (element.name == BuilderConstants.SpecialVisualElementInitialMinSizeName)
                {
                    continue;
                }

                if (items == null)
                {
                    items = new List <ITreeViewItem>();
                }

                var id          = 0;
                var linkedAsset = element.GetVisualElementAsset();
                if (linkedAsset != null)
                {
                    id = linkedAsset.id;
                }
                else
                {
                    id = nextId;
                    nextId++;
                }

                var item = new TreeViewItem <VisualElement>(id, element);
                items.Add(item);

                var childItems = GetTreeItemsFromVisualTree(element, ref nextId);
                if (childItems == null)
                {
                    continue;
                }

                item.AddChildren(childItems);
            }

            return(items);
        }
Esempio n. 4
0
    /// <summary>
    /// 生成树形菜单
    /// </summary>
    public void GenerateTreeView()
    {
        //删除可能已经存在的树形菜单元素
        if (_treeViewItems != null)
        {
            for (int i = 0; i < _treeViewItems.Count; i++)
            {
                Destroy(_treeViewItems[i]);
            }
        }
        _treeViewItems.Clear();
        _tmpTree.Clear();
        for (int i = 0; i < Data.Count; i++)
        {
            GameObject item = null;

            if (Data[i].ParentID == -1)
            {
                item = Instantiate(FirstLevelTemplate);
                item.GetComponent <TreeViewItem>().SetHierarchy(0);
                item.GetComponent <TreeViewItem>().SetParent(null);
                item.SetActive(true);
            }
            else
            {
                item = Instantiate(SecondLevelTemplate);
                TreeViewItem tvi = null;
                tvi = _tmpTree[Data[i].ParentID].GetComponent <TreeViewItem>();
                item.GetComponent <TreeViewItem>().SetHierarchy(tvi.GetHierarchy() + 1);
                item.GetComponent <TreeViewItem>().SetParent(tvi);
                item.transform.SetSiblingIndex(tvi.GetHierarchy() + 1);
                tvi.AddChildren(item.GetComponent <TreeViewItem>());
                item.SetActive(false);//默认2级隐藏
            }
            if (!_tmpTree.ContainsKey(Data[i].SelfID))
            {
                _tmpTree.Add(Data[i].SelfID, item);
            }
            item.transform.name = Data[i].SelfID.ToString();
            item.transform.Find("TreeViewText").GetComponent <Text>().text = Data[i].Name;
            item.transform.SetParent(TreeItems);
            item.transform.localPosition = Vector3.zero;
            item.transform.localScale    = Vector3.one;
            item.transform.localRotation = Quaternion.Euler(Vector3.zero);


            _treeViewItems.Add(item);
        }
        RefreshTreeView();
        foreach (var vi in _treeViewItems)
        {
            ChangeChildren(vi.GetComponent <TreeViewItem>(), false);
        }
    }
Esempio n. 5
0
    /// <summary>
    /// 生成树形菜单
    /// </summary>
    public void GenerateTreeView()
    {
        //删除可能已经存在的树形菜单元素
        if (_treeViewItems != null)
        {
            for (int i = 0; i < _treeViewItems.Count; i++)
            {
                Destroy(_treeViewItems[i]);
            }
            _treeViewItems.Clear();
        }
        //重新创建树形菜单元素
        _treeViewItems = new List <GameObject>();
        for (int i = 0; i < Data.Count; i++)
        {
            GameObject item = Instantiate(Template);

            if (Data[i].ParentID == -1)
            {
                item.GetComponent <TreeViewItem>().SetHierarchy(0);
                item.GetComponent <TreeViewItem>().SetParent(null);
            }
            else
            {
                TreeViewItem tvi = _treeViewItems[Data[i].ParentID].GetComponent <TreeViewItem>();
                item.GetComponent <TreeViewItem>().SetHierarchy(tvi.GetHierarchy() + 1);
                item.GetComponent <TreeViewItem>().SetParent(tvi);
                item.GetComponent <TreeViewItem>().SetData(Data[i]);
                tvi.AddChildren(item.GetComponent <TreeViewItem>());
            }

            item.transform.name = "TreeViewItem";
            item.transform.Find("TreeViewText").GetComponent <Text>().text = Data[i].Name;



            item.transform.SetParent(TreeItems);
            item.transform.localPosition = Vector3.zero;
            item.transform.localScale    = Vector3.one;
            item.transform.localRotation = Quaternion.Euler(Vector3.zero);
            item.SetActive(true);

            _treeViewItems.Add(item);
        }
    }
Esempio n. 6
0
        IList <ITreeViewItem> GenerateItemList(int count, ref int nextId)
        {
            var items = new List <ITreeViewItem>(count);

            for (int i = 0; i < count; ++i)
            {
                var currentId = nextId;
                nextId++;

                var newItem = new TreeViewItem <int>(currentId, i);

                if (count > 2)
                {
                    newItem.AddChildren(GenerateItemList(count / 2, ref nextId));
                }

                items.Add(newItem);
            }

            return(items);
        }
Esempio n. 7
0
        private IList <ITreeViewItem> GetTreeItemsFromVisualTree(VisualElement parent, ref int nextId)
        {
            List <ITreeViewItem> items = null;

            if (parent == null)
            {
                return(null);
            }

            int count = parent.hierarchy.childCount;

            if (count == 0)
            {
                return(null);
            }

            for (int i = 0; i < count; i++)
            {
                if (items == null)
                {
                    items = new List <ITreeViewItem>();
                }

                var element = parent.hierarchy[i];

                var item = new TreeViewItem <VisualElement>(nextId, element);
                items.Add(item);
                nextId++;

                var childItems = GetTreeItemsFromVisualTree(element, ref nextId);
                if (childItems == null)
                {
                    continue;
                }

                item.AddChildren(childItems);
            }

            return(items);
        }
Esempio n. 8
0
        public void RebuildTree(IPanelDebug panelDebug)
        {
            if (!hierarchyHasChanged && m_CurrentPanelDebug == panelDebug)
            {
                return;
            }

            m_CurrentPanelDebug = panelDebug;
            m_Container.Clear();

            int nextId = 1;

            m_TreeRootItems.Clear();

            var visualTree = panelDebug?.visualTree;

            if (visualTree != null)
            {
                var rootItem = new TreeViewItem <VisualElement>(nextId++, visualTree);
                m_TreeRootItems.Add(rootItem);

                var childItems = new List <ITreeViewItem>();
                AddTreeItemsForElement(childItems, visualTree, ref nextId);

                rootItem.AddChildren(childItems);
            }

            Func <VisualElement> makeItem = () =>
            {
                var element = new VisualElement();
                element.name = "unity-treeview-item-content";
                element.RegisterCallback <MouseEnterEvent>((e) =>
                {
                    HighlightItemInTargetWindow(e.target as VisualElement);
                });
                element.RegisterCallback <MouseLeaveEvent>((e) =>
                {
                    UnhighlightItemInTargetWindow(e.target as VisualElement);
                });
                return(element);
            };

            // Clear selection which would otherwise persist via view data persistence.
            m_TreeView?.ClearSelection();

            m_TreeView = new TreeView(m_TreeRootItems, 20, makeItem, FillItem);
            m_TreeView.style.flexGrow     = 1;
            m_TreeView.onSelectionChange += items =>
            {
                if (m_SelectElementCallback == null)
                {
                    return;
                }

                if (!items.Any())
                {
                    m_SelectElementCallback(null);
                    return;
                }

                var item    = items.First() as TreeViewItem <VisualElement>;
                var element = item != null ? item.data : null;
                m_SelectElementCallback(element);
            };

            m_Container.Add(m_TreeView);

            hierarchyHasChanged = false;
        }