You could try InternalEditorUtility.RepaintAllViews(). It’s undocumented, but will repaint everything. Unfortunately, there’s no public API to repaint specifically inspector windows.
I don’t know if this has any bad side effects, but I found a solution to make the editor refresh from an example that uses a shortcut-key to lock/unlock the inspector. I modified the code below with what is working for me. There is a slight delay (1-2 seconds) before the refresh, but it does refresh and show all the components expanded/collapsed, and now that I’ve used it for a couple of days, this should absolutely be a standard feature in the inspector AND hierarchy!
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
public static class ComponentTools
{
[MenuItem("CONTEXT/Component/Collapse All")]
public static void CollapseAll(MenuCommand command)
{
GameObject gameObject = (command.context as Component).gameObject;
Component[] components = gameObject.GetComponents<Component>();
foreach (Component component in components)
{
InternalEditorUtility.SetIsInspectorExpanded(component, false);
}
ActiveEditorTracker.sharedTracker.ForceRebuild();
}
[MenuItem("CONTEXT/Component/Expand All")]
public static void ExpandAll(MenuCommand command)
{
GameObject gameObject = (command.context as Component).gameObject;
Component[] components = gameObject.GetComponents<Component>();
foreach (Component component in components)
{
InternalEditorUtility.SetIsInspectorExpanded(component, true);
}
ActiveEditorTracker.sharedTracker.ForceRebuild();
}
}
Hey this script is super handy!
I noticed though it doesn’t collapse material components, do know why that would be? I’m thinking it might be because they aren’t really regular components as such and just displayed to simplify things for the user.
Might be a little tricky but does anyone know how to add them?
Nice script! I was looking for something like this.
It seems like it doesn’t refresh though?
So after you Collapse All, you have to reselect the object to see the changes?
It should refresh automatically (Unity 2017.4.10f1), however, if you have more than 1 inspector window open, it may be expanding/collapsing the other inspector window(s). I don’t know why it does not do all of the open inspector windows, or the selected inspector window, but it does not. I have not been able to find a work-around or how to determine which inspector window is considered the “Active” one to the sharedTracker.
I put this ComponentTools.cs in my 2018.3.8f1 editor folder and it does not appear in menu after Unity restart. I had to put it under and exitsting Tools menu itme then it showed up. But then I received: NullReferenceException: Object reference not set to an instance of an object ComponentTools.CollapseAll (UnityEditor.MenuCommand command) (at Assets/Editor/ComponentTools.cs:10)
with a heirarchy item selected.
Is there any way to scroll inspector to a newly expanded component via script? Unfortunately, setting Selection.activeObject to the expanded component does not scroll the inspector view to it.
ActiveEditorTracker.sharedTracker is not suitable: it only works if you work with a single Inspector window
I tried using myEditor.Repaint() and InternalEditorUtility.RepaintAllViews() but it doesn’t do anything. I still have to deselect and reselect the game object.
I slighty changed it. I have deleted method parameter and also added hot-keys to allow collapse / expand Inspector without mouse:
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using Object = UnityEngine.Object;
public static class MyEditorHotkeys
{
// Hot key: ALT + W
[MenuItem("Tools/Inspector/Collapse All &w")]
public static void InspectorCollapseAll() {
if (Selection.objects == null)
return;
foreach (Object obj in Selection.objects) {
if (obj is GameObject go) {
Component[] components = go.GetComponents<Component>();
foreach (Component component in components) {
InternalEditorUtility.SetIsInspectorExpanded(component, false);
// Fix for renders and materials.
if (component is Renderer) {
Material[] mats = ((Renderer)component).sharedMaterials;
for (int i = 0; i < mats.Length; ++i) {
InternalEditorUtility.SetIsInspectorExpanded(mats[i], false);
}
}
}
ActiveEditorTracker.sharedTracker.ForceRebuild();
}
}
}
// Hot key: ALT + Q
[MenuItem("Tools/Inspector/Expand All &q")]
public static void InspectorExpandAll() {
if (Selection.objects == null)
return;
foreach (Object obj in Selection.objects) {
if (obj is GameObject go) {
// GameObject gameObject = (command.context as Component).gameObject;
Component[] components = go.GetComponents<Component>();
foreach (Component component in components) {
InternalEditorUtility.SetIsInspectorExpanded(component, true);
//// Uncomment those lines to also expand materials.
//// Fix for renders and materials.
//if (component is Renderer) {
// Material[] mats = ((Renderer)component).sharedMaterials;
// for (int i = 0; i < mats.Length; ++i) {
// InternalEditorUtility.SetIsInspectorExpanded(mats[i], true);
// }
//}
}
ActiveEditorTracker.sharedTracker.ForceRebuild();
}
}
}
}
Put this script to any ‘Editor’ folder in your project end then use shortcuts (hotkeys) for collapsing and expanding your Inspector.
You can change hotkeys in script by your own by changing very last characters in line:
[MenuItem("Tools/Inspector/Collapse All &w")]
There some special symbols are used:
% = CTRL, & = ALT, # = SHIFT
For example %&#W = CTRL + ALT + SHIFT + W