Get original prefab in Project while in Playmode in Editor

Hi, I making an editor tool and I need to know the original prefab of a gameobject in playmode,
I’m trying to use PrefabUtility.GetCorrespondingObjectFromSource(gameObject) but it returns null,
Is this possible?

No, the concept of Prefabs doesn’t exist as such at runtime. Runtime instantiation of Prefabs just clones the object, it doesn’t keep track of overrides or what the originating Asset is, or any of the edit time Prefab functionality.

You can use PrefabUtility.InstantiatePrefab in editor, just do

#if UNITY_EDITOR
return PrefabUtility.InstantiatePrefab(prefab, parent);
#else
return Object.Instantiate(prefab, parent);
#endif

This will be equivalent as dropping a prefab directly in the runtime scene.

Be mindful, since it’s a prefab instance, you will not be able to change the parent of the prefab’s children.

You can still delete them, or add new children.

It might slow down your game a bit, since the editor has to keep track of prefab overrides.

Another solution is to add a “AssetLink” component to your prefabs, and make it store the prefab guid or asset path as a string. You can then recover the prefab source using the AssetDatabase.

Unfortunately, it’s impossible for the “AssetLink” component to simply hold a reference to the asset, since that reference will be replaced with a self reference on instantiation.