How should the ObjectField picker be called with the new prefab workflow?
I’ve been trying to update my plugin Bolt to fully support the new prefab workflow, and the last standing issue I’m seeing is how the ObjectField picker is handled.
Here’s the problems I have:
If I set allowSceneObjects to true, scene objects will be shown in the picker when in prefab isolation mode, even though the tab is called “Self” and not “Scene”.
If I set allowSceneObjects to false, the “Self” tab is entirely hidden, so users cannot pick objects within the “Self” stage of the prefab at all.
When referencing an object in prefab isolation mode, PrefabUtility.IsPartOfPrefabInstance returns false, and PrefabUtility.IsPartOfPrefabAssetalso returns false. This actually seems like a bug to me, because in isolation mode, the reference is actually to the asset, so I expected the latter to return true.
Is there a new overload or approach for EditorGUI.ObjectField that I could use to allow self objects but not scene objects, or a new PrefabUtility method I could use to detect that the object is in isolation mode?
Can this check have edge cases I couldn’t think of?
Note that this doesn’t fix the issue with ObjectField. Actually, with this check in place setting allowSceneObjects to false, even the objects from within the prefab can’t be assigned or picked to the field. Really unsure how I’m supposed to implement this.
I’m also curious if there is a way to make the object-selector show objects from the the prefab stage, instead of the active scene, without using SerializedProperties. I would also appreciate an overload to EditorGUI.ObjectField or something equivalent. But if it’s possible to do without, by hijacking click events and manually opening the ObjectSelector, at least we would have a workaround until then.
It turns out the version of ObjectField that does not take a SerializedProperty doesn’t really have enough information to handle Prefab Mode entirely correctly. It doesn’t know if it’s being called from an object in the regular scenes or from an object that’s part of the Prefab loaded in Prefab Mode.
If at all possible, we recommend that you use the overloads that do take a SerializedProperty, since they have and use information about where the object lives that you store the selected object in.
For cases where you can’t use SerializedProperty for whatever reason, we’ll make a new overload where you can supply a target object which we will then use to determine which objects to show, and whether to show the “Scene” tab or the “Self” tab (or neither) in addition to the “Assets” tab. I.e. it will then have the same information we currently have and use for the overloads that take a SerializedProperty, and we can then handle it automatically the same way.
Great, thanks! That overload is what would solve the issue for us, because we’re using custom serialization (Odin Serializer / Full Serializer), so the SerializedProperties are not an option.