JetBrains / resharper-unity

Unity support for both ReSharper and Rider
Apache License 2.0
1.21k stars 134 forks source link

Add analysis for type passed to CustomPropertyDrawer #1160

Open citizenmatt opened 5 years ago

citizenmatt commented 5 years ago

The [CustomPropertyDrawer(typeof(...))] attribute sets the target type of the property or decorator drawer class it is being assigned to. There are requirements for the target type, as well as the base class of the custom drawer. Unity will silently ignore if these are set incorrectly (this needs testing).

citizenmatt commented 5 years ago

E.g.:

using System;
using UnityEngine;

[AttributeUsage(AttributeTargets.Field)]
public class AreaMaskAttribute : PropertyAttribute
{
}

[AttributeUsage(AttributeTargets.Field)]
public class AreaMask2Attribute : Attribute
{
}

using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(AreaMaskAttribute))]
[CustomPropertyDrawer(typeof(AreaMask2Attribute))]
public class AreaMaskAttributeDrawer : PropertyDrawer
{
  public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  {
    EditorGUI.LabelField(position, attribute.GetType().ToString());
  }
}

using UnityEngine;

public class MyBehaviour : MonoBehaviour
{
  // The editor will work for this one
  [AreaMaskAttribute] public int myField;
  // But not for this one
  [AreaMask2Attribute] public int myField;
}