/// <summary> /// Will gather all children from this TreeElement which pass the provided filter /// </summary> /// <typeparam name="T">a generic TreeElement child class</typeparam> /// <param name="element">the root element for the parsing</param> /// <param name="filter">a filtering delegate</param> /// <returns>an array of TreeElement as T type</returns> public static T[] GetChildren <T>(this T rootElement, ChildFilter <T> filter) where T : TreeElement { List <T> children = new List <T>(); foreach (var child in rootElement.Children) { if (filter.Invoke(child as T)) { children.Add(child as T); } children.AddRange(((T)child).GetChildren <T>(filter)); } return(children.ToArray()); }
/// <summary> /// Flushes data through this filter and all its children. /// </summary> public virtual void Flush() { if (this.ChildFilter != null) { this.ChildFilter.Flush(); while (ChildFilter.DataReady) { T data = ChildFilter.Read(); lock (dataOut) { dataOut.Enqueue(data); } } } }
/// <summary> /// This helper try to find an element with the provided id in this TreeElement's hierarchy, including itself /// </summary> /// <typeparam name="T">a TreeElement generic class</typeparam> /// <param name="rootElement">the root element for the search</param> /// <param name="filter">the filtering predicate</param> /// <returns>casted TreeElement as T, or null if not found</returns> public static T Find <T>(this T rootElement, ChildFilter <T> filter) where T : TreeElement { if (filter.Invoke(rootElement as T)) { return(rootElement); } foreach (var child in rootElement.Children) { var itFound = ((T)child).Find <T>(filter); if (itFound != null) { return(itFound); } } return(null); }
/// <summary> /// Writes data to the filter. If there are child filters, the data will automaticaly /// flow through them before being placed in the output of the filter. /// </summary> /// <param name="Data">A data value to be passed through the filter</param> public virtual void Write(T Data) { if (ChildFilter != null) { ChildFilter.Write(Data); while (ChildFilter.DataReady) { T data = ChildFilter.Read(); lock (dataOut) { dataOut.Enqueue(data); } } } else { lock (dataOut) { dataOut.Enqueue(Data); } } }
/// <summary> Accept tags with children acceptable to the filter.</summary> /// <param name="node">The node to check. /// </param> /// <returns> <code>true</code> if the node has an acceptable child, /// <code>false</code> otherwise. /// </returns> public virtual bool Accept(INode node) { CompositeTag tag; NodeList children; bool ret; ret = false; if (node is CompositeTag) { tag = (CompositeTag)node; children = tag.Children; if (null != children) { for (int i = 0; !ret && i < children.Size(); i++) { if (ChildFilter.Accept(children.ElementAt(i))) { ret = true; } } // do recursion after all children are checked // to get breadth first traversal if (!ret && Recursive) { for (int i = 0; !ret && i < children.Size(); i++) { if (Accept(children.ElementAt(i))) { ret = true; } } } } } return(ret); }
internal static List <object> UnityObjectSearch(string input, string customTypeInput, SearchContext context, ChildFilter childFilter, SceneFilter sceneFilter) { var results = new List <object>(); Type searchType = null; if (!string.IsNullOrEmpty(customTypeInput)) { if (ReflectionUtility.GetTypeByName(customTypeInput) is Type customType) { if (typeof(UnityEngine.Object).IsAssignableFrom(customType)) { searchType = customType; } else { ExplorerCore.LogWarning($"Custom type '{customType.FullName}' is not assignable from UnityEngine.Object!"); } } else { ExplorerCore.LogWarning($"Could not find any type by name '{customTypeInput}'!"); } } if (searchType == null) { searchType = typeof(UnityEngine.Object); } var allObjects = RuntimeProvider.Instance.FindObjectsOfTypeAll(searchType); // perform filter comparers string nameFilter = null; if (!string.IsNullOrEmpty(input)) { nameFilter = input; } bool shouldFilterGOs = searchType == typeof(GameObject) || typeof(Component).IsAssignableFrom(searchType); foreach (var obj in allObjects) { // name check if (!string.IsNullOrEmpty(nameFilter) && !obj.name.ContainsIgnoreCase(nameFilter)) { continue; } GameObject go = null; var type = obj.GetActualType(); if (type == typeof(GameObject)) { go = obj.TryCast <GameObject>(); } else if (typeof(Component).IsAssignableFrom(type)) { go = obj.TryCast <Component>()?.gameObject; } if (go) { // hide unityexplorer objects if (go.transform.root.name == "ExplorerCanvas") { continue; } if (shouldFilterGOs) { // scene check if (sceneFilter != SceneFilter.Any) { if (!Filter(go.scene, sceneFilter)) { continue; } } if (childFilter != ChildFilter.Any) { if (!go) { continue; } // root object check (no parent) if (childFilter == ChildFilter.HasParent && !go.transform.parent) { continue; } else if (childFilter == ChildFilter.RootObject && go.transform.parent) { continue; } } } } results.Add(obj); } return(results); }
private void OnChildFilterDropChanged(int value) => m_childFilter = (ChildFilter)value;
internal static object[] UnityObjectSearch(string input, string customTypeInput, SearchContext context, ChildFilter childFilter, SceneFilter sceneFilter) { Type searchType = null; switch (context) { case SearchContext.GameObject: searchType = typeof(GameObject); break; case SearchContext.Component: searchType = typeof(Component); break; case SearchContext.Custom: if (string.IsNullOrEmpty(customTypeInput)) { ExplorerCore.LogWarning("Custom Type input must not be empty!"); return(null); } if (ReflectionUtility.GetTypeByName(customTypeInput) is Type customType) { if (typeof(UnityEngine.Object).IsAssignableFrom(customType)) { searchType = customType; } else { ExplorerCore.LogWarning($"Custom type '{customType.FullName}' is not assignable from UnityEngine.Object!"); } } else { ExplorerCore.LogWarning($"Could not find a type by the name '{customTypeInput}'!"); } break; default: searchType = typeof(UnityEngine.Object); break; } if (searchType == null) { return(null); } var allObjects = RuntimeProvider.Instance.FindObjectsOfTypeAll(searchType); var results = new List <object>(); // perform filter comparers string nameFilter = null; if (!string.IsNullOrEmpty(input)) { nameFilter = input.ToLower(); } bool canGetGameObject = (sceneFilter != SceneFilter.Any || childFilter != ChildFilter.Any) && (context == SearchContext.GameObject || typeof(Component).IsAssignableFrom(searchType)); string sceneFilterString = null; if (!canGetGameObject) { if (context != SearchContext.UnityObject && (sceneFilter != SceneFilter.Any || childFilter != ChildFilter.Any)) { ExplorerCore.LogWarning($"Type '{searchType}' cannot have Scene or Child filters applied to it"); } } else { if (sceneFilter == SceneFilter.DontDestroyOnLoad) { sceneFilterString = "DontDestroyOnLoad"; } else if (sceneFilter == SceneFilter.Explicit) { sceneFilterString = SearchPage.Instance.m_sceneDropdown.options[SearchPage.Instance.m_sceneDropdown.value].text; } } foreach (var obj in allObjects) { // name check if (!string.IsNullOrEmpty(nameFilter) && !obj.name.ToLower().Contains(nameFilter)) { continue; } if (canGetGameObject) { #if MONO var go = context == SearchContext.GameObject ? obj as GameObject : (obj as Component).gameObject; #else var go = context == SearchContext.GameObject ? obj.TryCast <GameObject>() : obj.TryCast <Component>().gameObject; #endif // scene check if (sceneFilter != SceneFilter.Any) { if (!go) { continue; } switch (context) { case SearchContext.GameObject: if (go.scene.name != sceneFilterString) { continue; } break; case SearchContext.Custom: case SearchContext.Component: if (go.scene.name != sceneFilterString) { continue; } break; } } if (childFilter != ChildFilter.Any) { if (!go) { continue; } // root object check (no parent) if (childFilter == ChildFilter.HasParent && !go.transform.parent) { continue; } else if (childFilter == ChildFilter.RootObject && go.transform.parent) { continue; } } } results.Add(obj); } return(results.ToArray()); }
internal void ConstructTopArea() { var topAreaObj = UIFactory.CreateVerticalGroup(Content, new Color(0.15f, 0.15f, 0.15f)); var topGroup = topAreaObj.GetComponent <VerticalLayoutGroup>(); topGroup.childForceExpandHeight = false; topGroup.childControlHeight = true; topGroup.childForceExpandWidth = true; topGroup.childControlWidth = true; topGroup.padding.top = 5; topGroup.padding.left = 5; topGroup.padding.right = 5; topGroup.padding.bottom = 5; topGroup.spacing = 5; GameObject titleObj = UIFactory.CreateLabel(topAreaObj, TextAnchor.UpperLeft); Text titleLabel = titleObj.GetComponent <Text>(); titleLabel.text = "Search"; titleLabel.fontSize = 20; LayoutElement titleLayout = titleObj.AddComponent <LayoutElement>(); titleLayout.minHeight = 30; titleLayout.flexibleHeight = 0; // top area options var optionsGroupObj = UIFactory.CreateVerticalGroup(topAreaObj, new Color(0.1f, 0.1f, 0.1f)); var optionsGroup = optionsGroupObj.GetComponent <VerticalLayoutGroup>(); optionsGroup.childForceExpandHeight = false; optionsGroup.childControlHeight = true; optionsGroup.childForceExpandWidth = true; optionsGroup.childControlWidth = true; optionsGroup.spacing = 10; optionsGroup.padding.top = 4; optionsGroup.padding.right = 4; optionsGroup.padding.left = 4; optionsGroup.padding.bottom = 4; var optionsLayout = optionsGroupObj.AddComponent <LayoutElement>(); optionsLayout.minWidth = 500; optionsLayout.minHeight = 70; optionsLayout.flexibleHeight = 100; // search context row var contextRowObj = UIFactory.CreateHorizontalGroup(optionsGroupObj, new Color(1, 1, 1, 0)); var contextGroup = contextRowObj.GetComponent <HorizontalLayoutGroup>(); contextGroup.childForceExpandWidth = false; contextGroup.childControlWidth = true; contextGroup.childForceExpandHeight = false; contextGroup.childControlHeight = true; contextGroup.spacing = 3; var contextLayout = contextRowObj.AddComponent <LayoutElement>(); contextLayout.minHeight = 25; var contextLabelObj = UIFactory.CreateLabel(contextRowObj, TextAnchor.MiddleLeft); var contextText = contextLabelObj.GetComponent <Text>(); contextText.text = "Searching for:"; var contextLabelLayout = contextLabelObj.AddComponent <LayoutElement>(); contextLabelLayout.minWidth = 125; contextLabelLayout.minHeight = 25; // context buttons AddContextButton(contextRowObj, "UnityEngine.Object", SearchContext.UnityObject, 140); AddContextButton(contextRowObj, "GameObject", SearchContext.GameObject); AddContextButton(contextRowObj, "Component", SearchContext.Component); AddContextButton(contextRowObj, "Custom...", SearchContext.Custom); // custom type input var customTypeObj = UIFactory.CreateInputField(contextRowObj); var customTypeLayout = customTypeObj.AddComponent <LayoutElement>(); customTypeLayout.minWidth = 250; customTypeLayout.flexibleWidth = 2000; customTypeLayout.minHeight = 25; customTypeLayout.flexibleHeight = 0; m_customTypeInput = customTypeObj.GetComponent <InputField>(); m_customTypeInput.placeholder.gameObject.GetComponent <Text>().text = "eg. UnityEngine.Texture2D, etc..."; // static class and singleton buttons var secondRow = UIFactory.CreateHorizontalGroup(optionsGroupObj, new Color(1, 1, 1, 0)); var secondGroup = secondRow.GetComponent <HorizontalLayoutGroup>(); secondGroup.childForceExpandWidth = false; secondGroup.childForceExpandHeight = false; secondGroup.spacing = 3; var secondLayout = secondRow.AddComponent <LayoutElement>(); secondLayout.minHeight = 25; var spacer = UIFactory.CreateUIObject("spacer", secondRow); var spaceLayout = spacer.AddComponent <LayoutElement>(); spaceLayout.minWidth = 125; spaceLayout.minHeight = 25; AddContextButton(secondRow, "Static Class", SearchContext.StaticClass); AddContextButton(secondRow, "Singleton", SearchContext.Singleton); // search input var nameRowObj = UIFactory.CreateHorizontalGroup(optionsGroupObj, new Color(1, 1, 1, 0)); var nameRowGroup = nameRowObj.GetComponent <HorizontalLayoutGroup>(); nameRowGroup.childForceExpandWidth = true; nameRowGroup.childControlWidth = true; nameRowGroup.childForceExpandHeight = false; nameRowGroup.childControlHeight = true; var nameRowLayout = nameRowObj.AddComponent <LayoutElement>(); nameRowLayout.minHeight = 25; nameRowLayout.flexibleHeight = 0; nameRowLayout.flexibleWidth = 5000; var nameLabelObj = UIFactory.CreateLabel(nameRowObj, TextAnchor.MiddleLeft); var nameLabelText = nameLabelObj.GetComponent <Text>(); nameLabelText.text = "Name contains:"; var nameLabelLayout = nameLabelObj.AddComponent <LayoutElement>(); nameLabelLayout.minWidth = 125; nameLabelLayout.minHeight = 25; var nameInputObj = UIFactory.CreateInputField(nameRowObj); m_nameInput = nameInputObj.GetComponent <InputField>(); //m_nameInput.placeholder.gameObject.GetComponent<TextMeshProUGUI>().text = ""; var nameInputLayout = nameInputObj.AddComponent <LayoutElement>(); nameInputLayout.minWidth = 150; nameInputLayout.flexibleWidth = 5000; nameInputLayout.minHeight = 25; // extra filter row m_extraFilterRow = UIFactory.CreateHorizontalGroup(optionsGroupObj, new Color(1, 1, 1, 0)); m_extraFilterRow.SetActive(false); var extraGroup = m_extraFilterRow.GetComponent <HorizontalLayoutGroup>(); extraGroup.childForceExpandHeight = true; extraGroup.childControlHeight = true; extraGroup.childForceExpandWidth = false; extraGroup.childControlWidth = true; var filterRowLayout = m_extraFilterRow.AddComponent <LayoutElement>(); filterRowLayout.minHeight = 25; filterRowLayout.flexibleHeight = 0; filterRowLayout.minWidth = 125; filterRowLayout.flexibleWidth = 150; // scene filter var sceneLabelObj = UIFactory.CreateLabel(m_extraFilterRow, TextAnchor.MiddleLeft); var sceneLabel = sceneLabelObj.GetComponent <Text>(); sceneLabel.text = "Scene Filter:"; var sceneLayout = sceneLabelObj.AddComponent <LayoutElement>(); sceneLayout.minWidth = 125; sceneLayout.minHeight = 25; var sceneDropObj = UIFactory.CreateDropdown(m_extraFilterRow, out m_sceneDropdown); m_sceneDropdown.itemText.text = "Any"; m_sceneDropdown.itemText.fontSize = 12; var sceneDropLayout = sceneDropObj.AddComponent <LayoutElement>(); sceneDropLayout.minWidth = 220; sceneDropLayout.minHeight = 25; m_sceneDropdown.onValueChanged.AddListener(OnSceneDropdownChanged); void OnSceneDropdownChanged(int value) { if (value < 4) { m_sceneFilter = (SceneFilter)value; } else { m_sceneFilter = SceneFilter.Explicit; } } // invisible space var invis = UIFactory.CreateUIObject("spacer", m_extraFilterRow); var invisLayout = invis.AddComponent <LayoutElement>(); invisLayout.minWidth = 25; invisLayout.flexibleWidth = 0; // children filter var childLabelObj = UIFactory.CreateLabel(m_extraFilterRow, TextAnchor.MiddleLeft); var childLabel = childLabelObj.GetComponent <Text>(); childLabel.text = "Child Filter:"; var childLayout = childLabelObj.AddComponent <LayoutElement>(); childLayout.minWidth = 100; childLayout.minHeight = 25; var childDropObj = UIFactory.CreateDropdown(m_extraFilterRow, out Dropdown childDrop); childDrop.itemText.text = "Any"; childDrop.itemText.fontSize = 12; var childDropLayout = childDropObj.AddComponent <LayoutElement>(); childDropLayout.minWidth = 180; childDropLayout.minHeight = 25; childDrop.options.Add(new Dropdown.OptionData { text = "Any" }); childDrop.options.Add(new Dropdown.OptionData { text = "Root Objects Only" }); childDrop.options.Add(new Dropdown.OptionData { text = "Children Only" }); childDrop.onValueChanged.AddListener(OnChildDropdownChanged); void OnChildDropdownChanged(int value) { m_childFilter = (ChildFilter)value; } // search button var searchBtnObj = UIFactory.CreateButton(topAreaObj); var searchText = searchBtnObj.GetComponentInChildren <Text>(); searchText.text = "Search"; LayoutElement searchBtnLayout = searchBtnObj.AddComponent <LayoutElement>(); searchBtnLayout.minHeight = 30; searchBtnLayout.flexibleHeight = 0; var searchBtn = searchBtnObj.GetComponent <Button>(); searchBtn.onClick.AddListener(OnSearchClicked); }