private Vector2 GetAssetPreviewSize(SerializedProperty property) { Texture2D previewTexture = GetAssetPreview(property); if (previewTexture == null) { return(Vector2.zero); } else { int targetWidth = ShowAssetPreviewAttribute.DefaultWidth; int targetHeight = ShowAssetPreviewAttribute.DefaultHeight; ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property); if (showAssetPreviewAttribute != null) { targetWidth = showAssetPreviewAttribute.Width; targetHeight = showAssetPreviewAttribute.Height; } int width = Mathf.Clamp(targetWidth, 0, previewTexture.width); int height = Mathf.Clamp(targetHeight, 0, previewTexture.height); return(new Vector2(width, height)); } }
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) { ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property); var maxValue = GetMaxValue(property, progressBarAttribute); return(IsNumber(property) && maxValue is float ?GetPropertyHeight(property) : GetPropertyHeight(property) + GetHelpBoxHeight()); }
protected override float GetPropertyHeight_Internal(SerializedProperty property, GUIContent label) { AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute <AnimatorParamAttribute>(property); bool validAnimatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName) != null; bool validPropertyType = property.propertyType == SerializedPropertyType.Integer || property.propertyType == SerializedPropertyType.String; return((validAnimatorController && validPropertyType) ? GetPropertyHeight(property) : GetPropertyHeight(property) + GetHelpBoxHeight()); }
private static void PropertyField_Implementation(Rect rect, SerializedProperty property, bool includeChildren, PropertyFieldFunction propertyFieldFunction) { SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute <SpecialCaseDrawerAttribute>(property); if (specialCaseAttribute != null) { specialCaseAttribute.GetDrawer().OnGUI(rect, property); } else { GUIContent label = new GUIContent(PropertyUtility.GetLabel(property)); bool anyDrawerAttribute = PropertyUtility.GetAttributes <DrawerAttribute>(property).Any(); if (!anyDrawerAttribute) { // Drawer attributes check for visibility, enableability and validator themselves, // so if a property doesn't have a DrawerAttribute we need to check for these explicitly // Check if visible bool visible = PropertyUtility.IsVisible(property); if (!visible) { return; } // Validate ValidatorAttribute[] validatorAttributes = PropertyUtility.GetAttributes <ValidatorAttribute>(property); foreach (var validatorAttribute in validatorAttributes) { validatorAttribute.GetValidator().ValidateProperty(property); } // Check if enabled and draw EditorGUI.BeginChangeCheck(); bool enabled = PropertyUtility.IsEnabled(property); using (new EditorGUI.DisabledScope(disabled: !enabled)) { propertyFieldFunction.Invoke(rect, property, label, includeChildren); } // Call OnValueChanged callbacks if (EditorGUI.EndChangeCheck()) { PropertyUtility.CallOnValueChangedCallbacks(property); } } else { // We don't need to check for enableIfAttribute propertyFieldFunction.Invoke(rect, property, label, includeChildren); } } }
protected float GetPropertyHeight(SerializedProperty property) { SpecialCaseDrawerAttribute specialCaseAttribute = PropertyUtility.GetAttribute <SpecialCaseDrawerAttribute>(property); if (specialCaseAttribute != null) { return(specialCaseAttribute.GetDrawer().GetPropertyHeight(property)); } return(EditorGUI.GetPropertyHeight(property, true)); }
public override void OnInspectorGUI() { GetSerializedProperties(ref _serializedProperties); bool anyNaughtyAttribute = _serializedProperties.Any(p => PropertyUtility.GetAttribute <INaughtyAttribute>(p) != null); if (!anyNaughtyAttribute) { DrawDefaultInspector(); } else { DrawSerializedProperties(); } DrawNonSerializedFields(); DrawNativeProperties(); DrawButtons(); }
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(rect, label, property); // Check user error if (property.propertyType != SerializedPropertyType.AnimationCurve) { string message = string.Format("Field {0} is not an AnimationCurve", property.name); DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); return; } var attribute = PropertyUtility.GetAttribute <CurveRangeAttribute>(property); EditorGUI.CurveField(rect, property, attribute.Color == EColor.Clear ? Color.green : attribute.Color.GetColor(), new Rect(attribute.Min.x, attribute.Min.y, attribute.Max.x - attribute.Min.x, attribute.Max.y - attribute.Min.y)); EditorGUI.EndProperty(); }
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) { if (!IsNumber(property)) { string message = string.Format("Field {0} is not a number", property.name); DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); return; } ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property); var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue; var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : string.Format("{0:0.00}", value); var maxValue = GetMaxValue(property, progressBarAttribute); if (maxValue != null && maxValue is float) { var fillPercentage = value / (float)maxValue; var barLabel = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue; var barColor = progressBarAttribute.Color.GetColor(); var labelColor = Color.white; var indentLength = NaughtyEditorGUI.GetIndentLength(rect); Rect barRect = new Rect() { x = rect.x + indentLength, y = rect.y, width = rect.width - indentLength, height = EditorGUIUtility.singleLineHeight }; DrawBar(barRect, Mathf.Clamp01(fillPercentage), barLabel, barColor, labelColor); } else { string message = string.Format( "The provided dynamic max value for the progress bar is not correct. Please check if the '{0}' is correct, or the return type is float", nameof(progressBarAttribute.MaxValueName)); DrawDefaultPropertyAndHelpBox(rect, property, message, MessageType.Warning); } }
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) { AnimatorParamAttribute animatorParamAttribute = PropertyUtility.GetAttribute <AnimatorParamAttribute>(property); AnimatorController animatorController = GetAnimatorController(property, animatorParamAttribute.AnimatorName); if (animatorController == null) { DrawDefaultPropertyAndHelpBox(rect, property, InvalidAnimatorControllerWarningMessage, MessageType.Warning); return; } int parametersCount = animatorController.parameters.Length; List <AnimatorControllerParameter> animatorParameters = new List <AnimatorControllerParameter>(parametersCount); for (int i = 0; i < parametersCount; i++) { AnimatorControllerParameter parameter = animatorController.parameters[i]; if (animatorParamAttribute.AnimatorParamType == null || parameter.type == animatorParamAttribute.AnimatorParamType) { animatorParameters.Add(parameter); } } switch (property.propertyType) { case SerializedPropertyType.Integer: DrawPropertyForInt(rect, property, label, animatorParameters); break; case SerializedPropertyType.String: DrawPropertyForString(rect, property, label, animatorParameters); break; default: DrawDefaultPropertyAndHelpBox(rect, property, string.Format(InvalidTypeWarningMessage, property.name), MessageType.Warning); break; } }
public override void ValidateProperty(SerializedProperty property) { RequiredAttribute requiredAttribute = PropertyUtility.GetAttribute <RequiredAttribute>(property); if (property.propertyType == SerializedPropertyType.ObjectReference) { if (property.objectReferenceValue == null) { string errorMessage = property.name + " is required"; if (!string.IsNullOrEmpty(requiredAttribute.Message)) { errorMessage = requiredAttribute.Message; } NaughtyEditorGUI.HelpBox_Layout(errorMessage, MessageType.Error, context: property.serializedObject.targetObject); } } else { string warning = requiredAttribute.GetType().Name + " works only on reference types"; NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject); } }
public override void ValidateProperty(SerializedProperty property) { MaxValueAttribute maxValueAttribute = PropertyUtility.GetAttribute <MaxValueAttribute>(property); if (property.propertyType == SerializedPropertyType.Integer) { if (property.intValue > maxValueAttribute.MaxValue) { property.intValue = (int)maxValueAttribute.MaxValue; } } else if (property.propertyType == SerializedPropertyType.Float) { if (property.floatValue > maxValueAttribute.MaxValue) { property.floatValue = maxValueAttribute.MaxValue; } } else { string warning = maxValueAttribute.GetType().Name + " can be used only on int or float fields"; Debug.LogWarning(warning, property.serializedObject.targetObject); } }
protected override void OnGUI_Internal(Rect rect, SerializedProperty property, GUIContent label) { if (property.isArray) { string key = GetPropertyKeyName(property); ReorderableList reorderableList = null; if (!_reorderableListsByPropertyName.ContainsKey(key)) { reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true) { drawHeaderCallback = (Rect r) => { EditorGUI.LabelField(r, string.Format("{0}: {1}", label.text, property.arraySize), EditorStyles.boldLabel); HandleDragAndDrop(r, reorderableList); }, drawElementCallback = (Rect r, int index, bool isActive, bool isFocused) => { SerializedProperty element = property.GetArrayElementAtIndex(index); r.y += 1.0f; r.x += 10.0f; r.width -= 10.0f; string attribute = PropertyUtility.GetAttribute <ReorderableListAttribute>(property).itemName; if (!string.IsNullOrEmpty(attribute)) { EditorGUI.PropertyField(new Rect(r.x, r.y, r.width, 0.0f), element, new GUIContent(attribute + " " + index), true); } else { EditorGUI.PropertyField(new Rect(r.x, r.y, r.width, 0.0f), element, true); } }, elementHeightCallback = (int index) => { return(EditorGUI.GetPropertyHeight(property.GetArrayElementAtIndex(index)) + 4.0f); } }; _reorderableListsByPropertyName[key] = reorderableList; } reorderableList = _reorderableListsByPropertyName[key]; if (rect == default) { reorderableList.DoLayoutList(); } else { reorderableList.DoList(rect); } } else { string message = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists"; NaughtyEditorGUI.HelpBox_Layout(message, MessageType.Warning, context: property.serializedObject.targetObject); EditorGUILayout.PropertyField(property, true); } }
private static IEnumerable <IGrouping <string, SerializedProperty> > GetFoldoutProperties(IEnumerable <SerializedProperty> properties) { return(properties .Where(p => PropertyUtility.GetAttribute <FoldoutAttribute>(p) != null) .GroupBy(p => PropertyUtility.GetAttribute <FoldoutAttribute>(p).Name)); }
private static IEnumerable <SerializedProperty> GetNonGroupedProperties(IEnumerable <SerializedProperty> properties) { return(properties.Where(p => PropertyUtility.GetAttribute <IGroupAttribute>(p) == null)); }
public override void ValidateProperty(SerializedProperty property) { ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute <ValidateInputAttribute>(property); object target = PropertyUtility.GetTargetObjectWithProperty(property); MethodInfo validationCallback = ReflectionUtility.GetMethod(target, validateInputAttribute.CallbackName); if (validationCallback != null && validationCallback.ReturnType == typeof(bool)) { ParameterInfo[] callbackParameters = validationCallback.GetParameters(); if (callbackParameters.Length == 0) { if (!(bool)validationCallback.Invoke(target, null)) { if (string.IsNullOrEmpty(validateInputAttribute.Message)) { NaughtyEditorGUI.HelpBox_Layout( property.name + " is not valid", MessageType.Error, context: property.serializedObject.targetObject); } else { NaughtyEditorGUI.HelpBox_Layout( validateInputAttribute.Message, MessageType.Error, context: property.serializedObject.targetObject); } } } else if (callbackParameters.Length == 1) { FieldInfo fieldInfo = ReflectionUtility.GetField(target, property.name); Type fieldType = fieldInfo.FieldType; Type parameterType = callbackParameters[0].ParameterType; if (fieldType == parameterType) { if (!(bool)validationCallback.Invoke(target, new object[] { fieldInfo.GetValue(target) })) { if (string.IsNullOrEmpty(validateInputAttribute.Message)) { NaughtyEditorGUI.HelpBox_Layout( property.name + " is not valid", MessageType.Error, context: property.serializedObject.targetObject); } else { NaughtyEditorGUI.HelpBox_Layout( validateInputAttribute.Message, MessageType.Error, context: property.serializedObject.targetObject); } } } else { string warning = "The field type is not the same as the callback's parameter type"; NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject); } } else { string warning = validateInputAttribute.GetType().Name + " needs a callback with boolean return type and an optional single parameter of the same type as the field"; NaughtyEditorGUI.HelpBox_Layout(warning, MessageType.Warning, context: property.serializedObject.targetObject); } } }