Question by
Estecka · Apr 29, 2018 at 12:41 AM ·
propertydrawerattributefield
Read Custom Attributes from Property Drawer
I'm trying to get a PropertyDrawer to change the way its target type is drawn using custom attributes, but I can't figure out a way to read a field's attribute from its propertyDrawer.
I did find something about making my attribute inherit PropertyAttribute
, and then recovering it from PropertyDrawer.Attribute
, but this variable is always null.
using UnityEditor;
// Field Type
[System.Serializable] public sealed class jauge {}
// Custom Attribute
[System.AttributeUsage( System.AttributeTargets.Field )]
public class jaugeDisplayOptions : PropertyAttribute {
public bool showContent, showSlider;
public jaugeDisplayOptions (bool showContent = false, bool showSlider = false){
this.showSlider = showSlider;
this.showContent = showContent;
}
}
// Test Class
public class jaugeDemo : MonoBehaviour {
[jaugeDisplayOptions (showContent:false, showSlider:false)] public jauge jauge1;
[jaugeDisplayOptions (showContent:false, showSlider:true) ] public jauge jauge2;
[jaugeDisplayOptions (showContent:true, showSlider:true) ] public jauge jauge3;
[jaugeDisplayOptions (showContent:true, showSlider:false)] public jauge jauge4;
}
// Property Drawer
[CustomPropertyDrawer(typeof (jauge))]
public class jaugeEditor : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label){
Debug.Log (this.attribute); // Null all the time
}
}
Comment