How do I get the actual prefab from within the prefab stage?

I’m making Editor tools for my project. In this case, I’m working on a custom inspector, and I’ve had quite a lot of problems identifying how a prefab is being interacted with.

I’ve found that I can identify that a prefab is selected in the project like so:

if ((PrefabUtility.GetPrefabAssetType(targetComponent.gameObject) == PrefabAssetType.Regular &&
                 PrefabUtility.GetPrefabInstanceStatus(targetComponent.gameObject) != PrefabInstanceStatus.Connected)) // asset selected
            {
                 // Selected in project, open in inspector
            }

and if it’s open in the prefab editor (prefab stage) like so:

if (PrefabStageUtility.GetPrefabStage(targetComponent.gameObject) != null)
{
     // Open in Prefab Editor
}

This makes it possible to have different custom inspectors for scene objects and prefabs. Useful.

Now I want to conditionally add the prefab (not an instance) to a list contained within a scriptable object. This works well when I have selected the prefab in the project pane, but within the prefab stage the “prefab” is a simple GameObject, not even a connected instance of the Prefab.

I want to get access to the prefab itself and add it to the list - when/how can I do that?

When we load a Prefab in Prefab Stage we load its contents as if it is a scene with the limitation that we can have only one GameObject in the root. The Prefab Stage has assetPath property from which you can get access to the actual Prefab Object using AssetDatabase.LoadMainAssetAtPath

Thanks, that appears to have worked.