You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Question] Can I inherit from existing attribute? #237
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.
[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.
Activity
dbrizov commentedon Jun 4, 2021
[ReadOnly]is working forList<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.IsEnableda little. You need to add a check if the application is playing.Maybe it's a good idea to make a new attribute called
ReadOnlyInPlayModeor something, and do a check for that attribute inPropertyUtility.IsEnabled.