Search Unity

EditorGUI.ObjectField allowSceneObjects in isolation mode?

Discussion in 'Prefabs' started by LazloBonin, Jan 10, 2019.

  1. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    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.IsPartOfPrefabAsset also 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?
     
  2. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    So because I have to issue a fix to my users soon, the workaround I found for IsPartOfPrefabAsset is this:

    Code (CSharp):
    1. return PrefabUtility.IsPartOfPrefabAsset(uo) || PrefabStageUtility.GetPrefabStage(uo.GameObject()) != null;
    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.
     
    Last edited: Jan 10, 2019
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
  4. bjarkeck

    bjarkeck

    Joined:
    Oct 26, 2014
    Posts:
    301
    Hello @SteenLund,

    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.
     
    LazloBonin likes this.
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Hi,

    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.
     
    Arsonistic, LazloBonin and bjarkeck like this.
  6. bjarkeck

    bjarkeck

    Joined:
    Oct 26, 2014
    Posts:
    301
    That is awesome, thanks! If we could get one for the EditorGUIUtility.ShowObjectPicker as well that would be fantastic :)
     
    runevision likes this.
  7. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    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.
     
  8. Arsonistic

    Arsonistic

    Joined:
    Dec 11, 2016
    Posts:
    13
    Great! Can't wait.
    Any idea of when this might land and whether it will be backported/fixed for 2018.3?
     
  9. kulesz

    kulesz

    Joined:
    Jul 1, 2012
    Posts:
    140
    Any news on this?