Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is it possible to make a field a default override?

Discussion in 'Prefabs' started by lynxelia, Mar 20, 2019.

  1. lynxelia

    lynxelia

    Joined:
    Sep 17, 2014
    Posts:
    18
    https://docs.unity3d.com/ScriptReference/PrefabUtility.IsDefaultOverride.html

    Default overrides seem like a very useful feature to prevent properties from cluttering the Prefab Overrides Window. While unity considers GameObject.name, Transform properties, and RectTransform properties to be default overrides, is it possible to mark fields in our own MonoBehaviour classes as default overrides too?

    For example, all our MonoBehaviours have a unique "Id" field that is used by our save system. Whenever a MonoBehaviour is added to a GameObject, this id is automatically set to the fileid + the guid for the MonoBehaviour. This allows us to have a unique identifier for the MonoBehaviour that can be accessed at runtime and is persistent (it is the same for every run of the game). We use the [HideInInspector] attribute on this field since it is a field that is set automatically and should never be changed by the user. But even though the field is hidden, it still causes the MonoBehaviour to show up the Prefab Overrides Window.

    In short, is there a way to make the Prefabs Overrides Window ignore changes to this field? Since all our MonoBehaviours have this id field, they all show up in the Prefab Overrides Window. It makes it very difficult to differentiate the changes a user actually intended to make to prefab from changes that were made automatically by our id management system.
     
    Vaarz, KWaldt and Storm4_ like this.
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    This is not possible at this time, no.

    Ignoring properties that have [HideInInspector] in the overrides dropdown is an interesting idea since we'd then only need to handle it there and not change anything about how properties are shown in the Inspector.

    Would it be possible for you to submit a bug report with a project that shows the issue and post the case number here? I'd like to investigate if it's something that's viable to do something about.

    [Edit] On further thought, it would not be sufficient to handle it in the Overrides dropdown. Apply functionality would also need to refrain from applying the property.
     
    Last edited: Mar 20, 2019
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Please don't do that.

    We use [HideInInspector] a bunch together with custom editors:

    Code (csharp):
    1. public class MyBehaviour : MonoBehaviour {
    2.     public int fieldA;
    3.     [HideInInspector]
    4.     public int fieldB;
    5. }
    6.  
    7. [CustomEditor(typeof(MyBehaviour))]
    8. public class MyBehaviourEditor : Editor {
    9.  
    10.     private static int[] validChoices = {1, 3, 5, 6};
    11.     private static GUIContent[] validChoiceNames = {new GUIContent("1"), new GUIContent("3"), new GUIContent("5"), new GUIContent("7"),};
    12.  
    13.     public override void OnInspectorGUI() {
    14.         DrawDefaultInspector();
    15.  
    16.         var fieldB = serializedObject.FindProperty(nameof(MyBehaviour.fieldB));
    17.         EditorGUILayout.IntPopup(fieldB, validChoiceNames, validChoices);
    18.         serializedObject.ApplyModifiedProperties();
    19.     }
    20. }
    So if you skipped applying [HideInInspector] fields, you'd break that.


    The good way to solve this is what's discussed here. In short, attributes that allow us to specify how fields should interact with prefabs, which should be completely independent from HideInInspector.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Yeah, fair enough. The idea seemed harmless enough when I at first thought it would affect the overrides dropdown only. But when it alo affects applying, it's not really a good way to go, as you say.

    Yeah. It's just a much larger prospect that requires us to cache some additional data for all properties and query this when applying as well as when drawing things in the Inspector. It's not something we'll be able to do in the near future. That's why looking into a potential shortcut was interesting, but alas it was not workable after all.
     
  5. lynxelia

    lynxelia

    Joined:
    Sep 17, 2014
    Posts:
    18
    A system like that would be extremely useful! It's a shame that it seems it wont be implemented anytime soon.
     
  6. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    809
    Reviving this feature request, is this something still being considered? We would have use cases for it too!
     
  7. AggroBirdGK

    AggroBirdGK

    Joined:
    Dec 21, 2021
    Posts:
    6
    I would like to ask this too, we ran into a similar use case.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Rune isn't at Unity anymore.

    I'm not quite sure if I'm agreeing with my own suggestion from 2019 anymore. Scripts become a lot less reusable if it's the responsibility of the script to say how values interact with prefabs.

    Instead, I think this is something you should be able to set on prefabs. When editing a prefab in prefab view, you should be able to right-click fields or entire components in the inspector and set prefab options on them. I know that Unity has considered this, but not how seriously.