Skip to content

[Question] Can I inherit from existing attribute? #237

Closed

Description

@Kotsuha

I would like to create a "PlayModeReadOnly" attribute which is readonly only in play mode. Currently I use something like this

using UnityEngine;

namespace MakeSomething
{
    public class PlayModeReadOnlyAttribute : PropertyAttribute
    {
    }
}
using UnityEditor;
using UnityEngine;

namespace MakeSomething
{
    // Reference: https://answers.unity.com/questions/489942/how-to-make-a-readonly-property-in-inspector.html
    [CustomPropertyDrawer(typeof(PlayModeReadOnlyAttribute))]
    public class PlayModeReadOnlyDrawer : PropertyDrawer
    {
        public override float GetPropertyHeight(SerializedProperty property,
                                                GUIContent label)
        {
            return EditorGUI.GetPropertyHeight(property, label, true);
        }

        public override void OnGUI(Rect position,
                                SerializedProperty property,
                                GUIContent label)
        {
            if (Application.isPlaying)
            {
                GUI.enabled = false;
                EditorGUI.PropertyField(position, property, label, true);
                GUI.enabled = true;
            }
            else
            {
                EditorGUI.PropertyField(position, property, label, true);
            }
        }
    }
}

It works for simple types but doesn't work for List<T>. In play mode the elements are readonly, but the count can be changed.

There is a "ReadOnlyAttribute" in NaughtyAttribute which covers List<T>.Count perfectly. I am wondering if I can somehow use it? If I can, how? Seems like stuff are handled in PropertyUtility class but then I'll have to modify package code. I know I can use [EnableIf("Foo")] but it requires creating extra method in every class where I would like to have this feature, which I don't prefer in this case.

Thank you.

Activity

  1. dbrizov commented on Jun 4, 2021

    @dbrizov
    Owner

    [ReadOnly] is working for List<T>, because it's not a property drawer, but a meta attribute that NaughtyAttribute's inspector handles internally.

    I have a pull request that adds a bunch of attributes which includes an attribute that covers your case: #203.
    I will probably merge it for the next release.

    If you want to do it yourself, you have to modify PropertyUtility.IsEnabled a little. You need to add a check if the application is playing.

    Maybe it's a good idea to make a new attribute called ReadOnlyInPlayMode or something, and do a check for that attribute in PropertyUtility.IsEnabled.

    image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions