private bool CanAutoPopulate(SerializedProperty property, AutoPopulateAttribute autoPopulate) { if (autoPopulate == null) { return(false); } if (property.isArray) { return(false); // Disable array support for now, // Unity doesn't call the drawer for the array, it calls it for the elements in the array :( } if (property.propertyPath.EndsWith("]")) { return(false); // as above, we use the ] char as a sign that Unity is trying to edit an array element } if (property.propertyType == SerializedPropertyType.ObjectReference) { return(true); } if (AutoPopulateUtils.IsInterfaceReference(property, fieldInfo.FieldType)) { return(true); } return(false); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { AutoPopulateAttribute autoPopulate = attribute as AutoPopulateAttribute; if (CanAutoPopulate(property, autoPopulate)) { bool wasEnabled = GUI.enabled; GUI.enabled = GetEditable(property, autoPopulate); bool populated = false; if (AutoPopulateUtils.NeedsPopulating(property, fieldInfo.FieldType)) { populated = AutoPopulateUtils.PopulateValue(fieldInfo.FieldType, property, autoPopulate); } else { populated = true; } GUIContent tweakedLabel = new GUIContent(label.text, GetIcon(autoPopulate, populated), GetTooltip(property, autoPopulate)); if (AutoPopulateUtils.IsInterfaceReference(property, fieldInfo.FieldType)) { InterfaceReferenceDrawer.PropertyField(position, property, fieldInfo, tweakedLabel); } else { EditorGUI.PropertyField(position, property, tweakedLabel); } GUI.enabled = wasEnabled; HandlePopup(position, property, autoPopulate); } else { GUIContent tweakedLabel = new GUIContent(label.text, ErrorIcon, GetErrorTooltip()); EditorGUI.PropertyField(position, property, tweakedLabel); } }