static void AddChildrenRecursive(TreeElement element, int numChildren, bool force, int numTotalElements, ref int allowedDepth, List <MyTreeElement> treeElements) { if (element.depth >= allowedDepth) { allowedDepth = 0; return; } for (int i = 0; i < numChildren; ++i) { if (IDCounter > numTotalElements) { return; } var child = new MyTreeElement("Element " + IDCounter, element.depth + 1, ++IDCounter); treeElements.Add(child); if (!force && Random.value < probabilityOfBeingLeaf) { continue; } AddChildrenRecursive(child, Random.Range(minNumChildren, maxNumChildren), false, numTotalElements, ref allowedDepth, treeElements); } }
public static List <MyTreeElement> GenerateRandomTree(int numTotalElements) { int numRootChildren = numTotalElements / 4; IDCounter = 0; var treeElements = new List <MyTreeElement>(numTotalElements); var root = new MyTreeElement("Root", -1, IDCounter); treeElements.Add(root); for (int i = 0; i < numRootChildren; ++i) { int allowedDepth = 6; AddChildrenRecursive(root, Random.Range(minNumChildren, maxNumChildren), true, numTotalElements, ref allowedDepth, treeElements); } return(treeElements); }
void ToolBar() { using (new EditorGUILayout.HorizontalScope()) { var style = "miniButton"; if (GUILayout.Button("Expand All", style)) { m_TreeView.ExpandAll(); } if (GUILayout.Button("Collapse All", style)) { m_TreeView.CollapseAll(); } GUILayout.FlexibleSpace(); if (GUILayout.Button("Add Item", style)) { Undo.RecordObject(asset, "Add Item To Asset"); // Add item as child of selection var selection = m_TreeView.GetSelection(); TreeElement parent = (selection.Count == 1 ? m_TreeView.treeModel.Find(selection[0]) : null) ?? m_TreeView.treeModel.root; int depth = parent != null ? parent.depth + 1 : 0; int id = m_TreeView.treeModel.GenerateUniqueID(); var element = new MyTreeElement("Item " + id, depth, id); m_TreeView.treeModel.AddElement(element, parent, 0); // Select newly created element m_TreeView.SetSelection(new[] { id }, TreeViewSelectionOptions.RevealAndFrame); } if (GUILayout.Button("Remove Item", style)) { Undo.RecordObject(asset, "Remove Item From Asset"); var selection = m_TreeView.GetSelection(); m_TreeView.treeModel.RemoveElements(selection); } } }