Limiting EditorGUI.ObjectField() to Prefabs only?

Hey all,
I’m making a EditorWindow with a few ObjectFields to let the user assign different GameObjects to each field. Some fields only allow you to assign non-scene objects, while the others can use any GameObject…that’s the problem though, I want to restrict those GameObject fields to only Prefabs, that way you can assign a non-Prefab object to them.

I need to restrict those fields to only allow PrefabType.Prefab objects and not PrefabType.ModelPrefab objects. How can I get this to work? Can I do something like ‘typeof(PrefabType.Prefab)’ (I tried that and it doesn’t like it…).

Any help would be greatly appreciated as always :slight_smile:

Thanks for your time…
Stephane

Assuming your field is obj. You can check for the type before assigning it to the field.

GameObject pObj = EditorGUI.ObjectField(new Rect(3,3,position.width - 6, 20),
            "Blablabla",
            obj,
            typeof(GameObject));
if (pObj != null && PrefabUtility.GetPrefabType(pObj) == PrefabType.Prefab)
{
    obj = pObj
}
else
{
    obj = null;
}

Just chiming in to say you can replace this now with:

_prefabToSearchFor = EditorGUILayout.ObjectField("Prefab to search for", _prefabToSearchFor, typeof(GameObject), false) as GameObject;

The “false” makes sure that you can’t assign a scene object, so it must be a Prefab.