Пример #1
0
 /// <summary>
 /// 根据每个treeNode获取数据信息,并创建UI对象。
 /// Method CreateTreeNode is called for every TreeNode is TreeNodes data source to create the Node UI GameObject.
 /// The created GameObject will be added to the tree as Branch or Leaf.
 /// </summary>
 /// <param name="treeNode">The index and information for this node</param>
 /// <returns></returns>
 protected virtual GameObject CreateTreeNode(TreeNode treeNode)
 {
     return new GameObject();
 }
Пример #2
0
        protected void CreateTree(GameObject parentObject, TreeNode treeNode)
        {
            do
            {
                var nodeObject = CreateTreeNode(treeNode);
                if (!nodeObject)
                {
                    break;
                }

                treeHeight += GetNodeHeight(nodeObject);
                UpdateContentPanelSize();

                // Add node object to tree.
                nodeObject.transform.SetParent(parentObject.transform, false);
                nodeObject.transform.SetAsLastSibling();
                nodeObject.transform.localScale = Vector3.one;

                theLastObject = nodeObject;

                // For branch node.
                if (treeNode.Type != TreeNode.NodeType.Branch)
                {
                    break;
                }

                var nodes = (treeNode as TreeBranch).Children;
                if (nodes == null || nodes.Count == 0)
                {
                    break;
                }

                float oldHeight = treeHeight;
                float addedHeight = 0;

                // Set Padding here if you want.
                GameObject grid = new GameObject();
                grid.name = "Grid" + treeNode.Index;

                grid.AddComponent<RectTransform>();
                var verticalLayoutGroup = grid.AddComponent<VerticalLayoutGroup>();
                verticalLayoutGroup.childForceExpandWidth = false;
                verticalLayoutGroup.childForceExpandHeight = false;

                grid.transform.SetParent(parentObject.transform, false);
                grid.transform.SetAsLastSibling();
                foreach (var node in nodes)
                {
                    CreateTree(grid, node);
                }

                var button = nodeObject.GetComponent<Button>();
                if (!button)
                {
                    Debug.LogError("Tree Branch should have a Button component.");
                    break;
                }

                addedHeight = treeHeight - oldHeight;
                button.onClick.AddListener(() =>
                {
                    bool shown = true;
                    bool hidden = true;
                    for (int i = 0; i < grid.transform.childCount; ++i)
                    {
                        GameObject node = grid.transform.GetChild(i).gameObject;
                        if (node.activeSelf)
                        {
                            hidden = false;
                        }
                        else
                        {
                            shown = false;
                        }
                    }

                    if (!shown && !hidden)
                    {
                        return;
                    }

                    if (shown)
                    {
                        for (int i = 0; i < grid.transform.childCount; ++i)
                        {
                            // Instantiate Avatar
                            GameObject node = grid.transform.GetChild(i).gameObject;

                            LeanTween.delayedCall(node, (i + 1) * duration, () =>
                            {
                                node.SetActive(false);
                            });
                        }
                        LeanTween.delayedCall(grid, grid.transform.childCount * duration, () =>
                        {
                            treeHeight -= addedHeight;
                            UpdateContentPanelSize();
                        });
                    }

                    if (hidden)
                    {
                        for (int i = grid.transform.childCount - 1; i >= 0; --i)
                        {
                            // Instantiate Avatar
                            GameObject node = grid.transform.GetChild(i).gameObject;

                            LeanTween.delayedCall(node, (i + 1) * duration, () =>
                            {
                                node.SetActive(true);
                            });
                        }

                        treeHeight += addedHeight;
                        UpdateContentPanelSize();
                    }
                });
            } while (false);
        }