HideInEditorMode for property with ShowInInspector processes the code anyway

Issue #881 wontfix
Václav Soukup created an issue

This code:

[HideInEditorMode] [ShowInInspector]
        public bool IsOnTurn => GameComponentAggregator.Instance.TurnController.IsCharaterOnTurn(this);

Throws null reference exeption (GameCOmponentAggregater is a Singleton in runtime only) when not in playmode (in editormode).

I expect HideInEditorMode runs the property calculation only in play mode.

I use Unity 2022.1.15f1. On MacOS M1, Odin 3.1.3

Comments (2)

  1. Tor Esa Vestergaard

    I'm afraid this is how it is intended to work - a hidden member still exists, it is merely not being shown; as such, Odin still checks its value, because for example, it could in theory decide to no longer be hidden based on said value. That is not the case for this specific attribute, but it is still implemented in the broader system where the hidden state of a member can be conditional on that member's value.

    What you ask for is possible to do custom yourself by using a property processor to cull members with this attribute while in editor mode, before they are ever inspected. This would involve not merely hiding them, but removing the entirely from the scope and knowledge of the inspector:

    using Sirenix.OdinInspector;
    using Sirenix.OdinInspector.Editor;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class RemoveHideInEditorModeMembersProcessor : OdinPropertyProcessor
    {
        public override bool CanProcessForProperty(InspectorProperty property)
        {
            return !Application.isPlaying;
        }
    
        public override void ProcessMemberProperties(List<InspectorPropertyInfo> propertyInfos)
        {
            for (int i = 0; i < propertyInfos.Count; i++)
            {
                if (propertyInfos[i].Attributes.HasAttribute<HideInEditorModeAttribute>())
                    propertyInfos.RemoveAt(i--);
            }
        }
    }
    

    Declaring the above processor in your code somewhere will trigger the behaviour you want. However, we won't add it to Odin itself, as it would break some of the generic API promises of the property tree design.

    If you don't want to do the above, I can only recommend that you code the property getter in a way that it does not throw exceptions in editor mode.

  2. Václav Soukup reporter

    Thanks for help, this did not solve it properly however. The property is not showing in either modes with this code (while playing or in editor only). I solved it myself, tx.

  3. Log in to comment