Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


scriptedimporter

ScriptedImporter

Hiding Sub-Assets in the Project View

By default, assets added via ctx.AddObjectToAsset will be shown in the foldout of the imported asset in the Project view. This can be cluttered. You can hide sub assets in two ways:

ScriptableObjects

Subassets of type ScriptableObject may be hidden by setting Object.hideFlags |= HideFlags.HideInHierarchy.

GameObjects

The hiding behavior of GameObjects is special cased by ScriptedImporter and HideInHierarchy will *not* work. Instead, all GameObjects of nesting level 1 and 2 are shown, and all others are hidden. In other words, if a GameObject has two or more parents above it in the transform hierarchy, it will be hidden.

You can take advantage of this to hide GameObjects by creating a fake parent above them, but not adding it to the imported asset. For example:

// This object will be included in the imported asset.
var childToHide = new GameObject("GeneratedPrefab");
            
// This object will *not* be included in the asset, despite being referenced by child.
var superParent = new GameObject("SuperParent_ImportWorkaround_ShouldBeDeleted");
var superParent2 = new GameObject("SuperParent_ImportWorkaround_ShouldBeDeleted2");
superParent2.transform.parent = superParent.transform;
            
childToHide.transform.parent = superParent2.transform;
            
ctx.AddObjectToAsset("GeneratedPrefab", childToHide);

Debugging

ScriptedImporters run on Asset Import worker processes1). These importers can't be debugged normally with the Mono Debugger in Rider or Visual Studio. Breakpoints can be set, but will never be triggered, because the breakpoint is being set in the Editor process, not the Worker process.

Rider can connect to these worker processes manually if you want to debug them via “Attach To Unity Process…”.

1)
You can confirm this if you see [Worker0] on output logged by a ScriptedImporter
scriptedimporter.txt · Last modified: 2024/11/06 22:32 by 97.113.69.90