- Home /
keep gameObject in hierarchy folded when not selected
i have "folder" gameObject for large group of gameObjects .... any time I click on one of them, the gameObject unfolds and i have to scroll very long way to fold it
is there way t do it by script?, something like on select anything else then "star" unfold this gameObject
Edit: it must be one line script like Hierarchy.Fold(gameObject) in OnDisable function in custom inspector
Answer by vexe · Mar 06, 2014 at 11:35 AM
This will hopefully cover all your folding needs :)
Using the Collapse
method from this answer, I was able to write the following one-liners (as you requested):
public static class Folds
{
[MenuItem("Folds/FoldSelection #&,")]
public static void FoldSelection()
{
Utils.CollapseSelection(false);
}
[MenuItem("Folds/CollapseSelection #&.")]
public static void CollapseSelection()
{
Utils.CollapseSelection(true);
}
[MenuItem("Folds/FoldAllExceptCurrentSelected %#&,")]
public static void FoldAllExceptCurrentSelected()
{
Utils.CollapseAllExcept(Selection.activeGameObject, false);
}
[MenuItem("Folds/CollapseAllExceptCurrentSelected %#&.")]
public static void CollapseAllExceptCurrentSelected()
{
Utils.CollapseAllExcept(Selection.activeGameObject, true);
}
}
And then put these in your Utility
class (just a static class) (you can tell that mine is called Utils
) (`Collapse` goes there as well)
public static void CollapseSelection(bool value)
{
var selection = Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel);
foreach(var go in selection)
Collapse(go, value);
}
public static void CollapseAllExcept(GameObject go, bool value)
{
// get the toplevel GOs (whose parents are null) - exclude 'go' - Don't forget to include System.Linq for the Where method to work
var toplevelGos = Object.FindObjectsOfType<GameObject>().Where(g => g.transform.parent == null && g != go);
foreach(var g in toplevelGos)
Collapse(g, value);
SelectObject(go);
}
REMARK:
%: Ctrl on Windows, cmd on OS X
#: Shift
&: Alt
However, all this stuff deals with GameObjects
in the Hiearchy
window, if you wanted to deal with Objects
, then this means that we could be selecting something from the Project
window (i.e. assets, folders, etc) - so our current Collapse
won't work cause it's using the Hierarchy
window. But it's very simple to add that functionality to it, modify the Collapse
method to:
public static void Collapse(Object obj, bool collapse, string window = "Hierarchy") // by default, it's the "Hierarchy" window
{
// get a reference to the wanted window
var hierarchy = GetFocusedWindow(window);
// select our object
SelectObject(obj);
// create a new key event (RightArrow for collapsing, LeftArrow for folding)
var key = new Event { keyCode = collapse ? KeyCode.RightArrow : KeyCode.LeftArrow, type = EventType.keyDown };
// finally, send the event to the window
hierarchy.SendEvent(key);
}
Now you could fold/collapse stuff from the Project
window as well:
Collapse(obj, true, "Project");
Answer by KillMobil · Feb 21, 2014 at 05:02 PM
You can make an little editor script that creates a menu item with a keyboard shortcut that when it get Hit its selects the parent object if there is one. it will also scroll up to the top! Hope that helps.
Here the code!
using UnityEngine;
using UnityEditor;
using System.Collections;
public class SelectParent : Editor{
[MenuItem ("Edit/Select Parent %e")]
public static void SelectParentFunction () {
if (Selection.activeGameObject && Selection.activeGameObject.transform.parent)
Selection.activeGameObject = Selection.activeGameObject.transform.parent.gameObject;
}
}
thank you for trying to help me but it's not what i seek, i might write it wrong
i need script to fold gameObject in hierarchy
example:
i have gameobject Stars and inside of it i have large group of gameobjects Star 0 to Star N
Stars
___Star 1
___Star 2
___Star 3
when i click on Star 2 i need to have Stars unfolded, when i click on anything else then Star N i need to fold Stars
there is OnDisable function in editor which is perfect place to put the script i need, and it might look like Hierarchy.Fold(gameobject) i just don't know how and if it's possible
I See! i have a similar problem that i what to solve and it seems that there isn't Hierarchy type class that is accessible and that can accept simple function. How ever in this Post the guy has made a script that focuses the hierarchy window and sends fake keyboard events to rename an object. Bit more than a line of scipt im afraid! I want to look into this soon if i manage to do fix ill post my findings!
Answer by sandolkakos · Nov 22, 2020 at 09:34 AM
For those who are still trying to find a good solution, based on a lot of research, I've created a utility with these methods:
- IsExpanded(GameObject go)
- GetExpandedGameObjects()
- SetExpanded(GameObject go, bool expand)
- SetExpandedRecursive(GameObject go, bool expand)
You can find my script here, I hope you guys make good use of it: - https://github.com/sandolkakos/unity-utilities/blob/main/Scripts/Editor/SceneHierarchyUtility.cs