Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

EditorGUI.ObjectField allowSceneObjects in isolation mode?

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

  1. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    499
    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. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    499
    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:
    362
  4. bjarkeck

    bjarkeck

    Joined:
    Oct 26, 2014
    Posts:
    238
    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.
     
    Ludiq likes this.
  5. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,432
    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.
     
    Ludiq and bjarkeck like this.
  6. bjarkeck

    bjarkeck

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

    Ludiq

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