public override void ValidateProperty(SerializedProperty property) { MinValueAttribute minValueAttribute = PropertyUtility.GetAttribute <MinValueAttribute>(property); if (property.propertyType == SerializedPropertyType.Integer) { if (property.intValue < minValueAttribute.MinValue) { property.intValue = (int)minValueAttribute.MinValue; } } else if (property.propertyType == SerializedPropertyType.Float) { if (property.floatValue < minValueAttribute.MinValue) { property.floatValue = minValueAttribute.MinValue; } } else { string warning = minValueAttribute.GetType().Name + " can be used only on int or float fields"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property)); } }
public override bool CanDrawProperty(SerializedProperty property) { HideIfAttribute hideIfAttribute = PropertyUtility.GetAttribute <HideIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo conditionField = target.GetType().GetField(hideIfAttribute.ConditionName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (conditionField != null && conditionField.FieldType == typeof(bool)) { return(!(bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = target.GetType().GetMethod(hideIfAttribute.ConditionName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { return(!(bool)conditionMethod.Invoke(target, null)); } string warning = hideIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, target); return(true); }
public override void DrawProperty(SerializedProperty property) { EditorDrawUtility.DrawHeader(property); if (property.propertyType == SerializedPropertyType.String) { EditorGUILayout.LabelField(property.displayName); EditorGUI.BeginChangeCheck(); string textAreaValue = EditorGUILayout.TextArea(property.stringValue, GUILayout.MinHeight(EditorGUIUtility.singleLineHeight * 3f)); if (EditorGUI.EndChangeCheck()) { property.stringValue = textAreaValue; } } else { string warning = PropertyUtility.GetAttributes <ResizableTextAreaAttribute>(property)[0].GetType().Name + " can only be used on string fields"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property)); EditorDrawUtility.DrawPropertyField(property); } }
public override void ValidateProperty(SerializedProperty property) { ValidateInputAttribute validateInputAttribute = PropertyUtility.GetAttribute <ValidateInputAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); MethodInfo validationCallback = target.GetType().GetMethod(validateInputAttribute.CallbackName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (validationCallback != null && validationCallback.ReturnType == typeof(ValidatorAttribute.ValidateResult) && validationCallback.GetParameters().Length == 1) { FieldInfo fieldInfo = target.GetType().GetField(property.name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); Type fieldType = fieldInfo.FieldType; Type parameterType = validationCallback.GetParameters()[0].ParameterType; if (fieldType == parameterType) { ValidatorAttribute.ValidateResult result = (ValidatorAttribute.ValidateResult)validationCallback.Invoke(target, new object[] { fieldInfo.GetValue(target) }); if (result != null) { if (result.ValidateType != ValidatorAttribute.ValidateType.Success) { MessageType messageType = MessageType.Error; if (result.ValidateType == ValidatorAttribute.ValidateType.Warning) { messageType = MessageType.Warning; } EditorGUILayout.HelpBox(result.Message, messageType); } else { EditorGUILayout.LabelField(GUIContent.none, GUIStyle.none, GUILayout.Height(0)); } } } else { EditorGUILayout.HelpBox("The field type is not the same as the callback's parameter type", MessageType.Warning); } } else { EditorGUILayout.HelpBox(validateInputAttribute.GetType().Name + " needs a callback with boolean return type and a single parameter of the same type as the field", MessageType.Warning); } }
public override void ApplyPropertyMeta(SerializedProperty property, MetaAttribute metaAttribute) { InfoBoxAttribute infoBoxAttribute = (InfoBoxAttribute)metaAttribute; UnityEngine.Object target = PropertyUtility.GetTargetObject(property); if (!string.IsNullOrEmpty(infoBoxAttribute.VisibleIf)) { FieldInfo conditionField = target.GetType().GetField(infoBoxAttribute.VisibleIf, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (conditionField != null && conditionField.FieldType == typeof(bool)) { if ((bool)conditionField.GetValue(target)) { this.DrawInfoBox(infoBoxAttribute.Text, infoBoxAttribute.Type); } return; } MethodInfo conditionMethod = target.GetType().GetMethod(infoBoxAttribute.VisibleIf, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { if ((bool)conditionMethod.Invoke(target, null)) { this.DrawInfoBox(infoBoxAttribute.Text, infoBoxAttribute.Type); } return; } string warning = infoBoxAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property)); } else { this.DrawInfoBox(infoBoxAttribute.Text, infoBoxAttribute.Type); } }
public override void ApplyPropertyMeta(SerializedProperty property, MetaAttribute metaAttribute) { OnValueChangedAttribute onValueChangedAttribute = (OnValueChangedAttribute)metaAttribute; UnityEngine.Object target = PropertyUtility.GetTargetObject(property); MethodInfo callbackMethod = target.GetType().GetMethod(onValueChangedAttribute.CallbackName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (callbackMethod != null && callbackMethod.ReturnType == typeof(void) && callbackMethod.GetParameters().Length == 0) { property.serializedObject.ApplyModifiedProperties(); // We must apply modifications so that the callback can be invoked with up-to-date data callbackMethod.Invoke(target, null); } else { string warning = onValueChangedAttribute.GetType().Name + " can invoke only action methods - with void return type and no parameters"; Debug.LogWarning(warning, target); } }
public override void DrawProperty(SerializedProperty property) { EditorDrawUtility.DrawHeader(property); if (property.isArray) { if (!this.reorderableListsByPropertyName.ContainsKey(property.name)) { ReorderableList reorderableList = new ReorderableList(property.serializedObject, property, true, true, true, true) { drawHeaderCallback = (Rect rect) => { EditorGUI.LabelField(rect, string.Format("{0}: {1}", property.displayName, property.arraySize), EditorStyles.label); }, drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { var element = property.GetArrayElementAtIndex(index); rect.y += 2f; EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element); } }; this.reorderableListsByPropertyName[property.name] = reorderableList; } this.reorderableListsByPropertyName[property.name].DoLayoutList(); } else { string warning = typeof(ReorderableListAttribute).Name + " can be used only on arrays or lists"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property)); EditorDrawUtility.DrawPropertyField(property); } }
public override void DrawProperty(SerializedProperty property) { EditorDrawUtility.DrawHeader(property); SliderAttribute sliderAttribute = PropertyUtility.GetAttribute <SliderAttribute>(property); if (property.propertyType == SerializedPropertyType.Integer) { EditorGUILayout.IntSlider(property, (int)sliderAttribute.MinValue, (int)sliderAttribute.MaxValue); } else if (property.propertyType == SerializedPropertyType.Float) { EditorGUILayout.Slider(property, sliderAttribute.MinValue, sliderAttribute.MaxValue); } else { string warning = sliderAttribute.GetType().Name + " can be used only on int or float fields"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property)); EditorDrawUtility.DrawPropertyField(property); } }
public override void DrawProperty(SerializedProperty property) { EditorDrawUtility.DrawHeader(property); DropdownAttribute dropdownAttribute = PropertyUtility.GetAttribute <DropdownAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); FieldInfo fieldInfo = target.GetType().GetField(property.name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); FieldInfo valuesFieldInfo = target.GetType().GetField(dropdownAttribute.ValuesFieldName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); if (valuesFieldInfo == null) { this.DrawWarningBox(string.Format("{0} cannot find a values field with name \"{1}\"", dropdownAttribute.GetType().Name, dropdownAttribute.ValuesFieldName)); EditorGUILayout.PropertyField(property, true); } else if (fieldInfo.FieldType == valuesFieldInfo.FieldType.GetElementType()) { // Selected value object selectedValue = fieldInfo.GetValue(target); // Values and display options IList valuesList = (IList)valuesFieldInfo.GetValue(target); object[] values = new object[valuesList.Count]; string[] displayOptions = new string[valuesList.Count]; for (int i = 0; i < values.Length; i++) { object value = valuesList[i]; values[i] = value; displayOptions[i] = value.ToString(); } // Selected value index int selectedValueIndex = Array.IndexOf(values, selectedValue); if (selectedValueIndex < 0) { selectedValueIndex = 0; } // Draw the dropdown this.DrawDropdown(target, fieldInfo, property.displayName, selectedValueIndex, values, displayOptions); } else if (valuesFieldInfo.GetValue(target) is IDropdownList) { // Current value object selectedValue = fieldInfo.GetValue(target); // Current value index, values and display options IDropdownList dropdown = (IDropdownList)valuesFieldInfo.GetValue(target); IEnumerator <KeyValuePair <string, object> > dropdownEnumerator = dropdown.GetEnumerator(); int index = -1; int selectedValueIndex = -1; List <object> values = new List <object>(); List <string> displayOptions = new List <string>(); while (dropdownEnumerator.MoveNext()) { index++; KeyValuePair <string, object> current = dropdownEnumerator.Current; if (current.Value.Equals(selectedValue)) { selectedValueIndex = index; } values.Add(current.Value); displayOptions.Add(current.Key); } if (selectedValueIndex < 0) { selectedValueIndex = 0; } // Draw the dropdown this.DrawDropdown(target, fieldInfo, property.displayName, selectedValueIndex, values.ToArray(), displayOptions.ToArray()); } else { this.DrawWarningBox(typeof(DropdownAttribute).Name + " works only when the type of the field is equal to the element type of the array"); EditorGUILayout.PropertyField(property, true); } }
private void DrawWarningBox(string warningText, SerializedProperty property) { EditorGUILayout.HelpBox(warningText, MessageType.Warning); Debug.LogWarning(warningText, PropertyUtility.GetTargetObject(property)); }
public override void DrawProperty(SerializedProperty property) { EditorDrawUtility.DrawHeader(property); MinMaxSliderAttribute minMaxSliderAttribute = PropertyUtility.GetAttribute <MinMaxSliderAttribute>(property); if (property.propertyType == SerializedPropertyType.Vector2) { Rect controlRect = EditorGUILayout.GetControlRect(); float labelWidth = EditorGUIUtility.labelWidth; float floatFieldWidth = EditorGUIUtility.fieldWidth; float sliderWidth = controlRect.width - labelWidth - 2f * floatFieldWidth; float sliderPadding = 5f; Rect labelRect = new Rect( controlRect.x, controlRect.y, labelWidth, controlRect.height); Rect sliderRect = new Rect( controlRect.x + labelWidth + floatFieldWidth + sliderPadding, controlRect.y, sliderWidth - 2f * sliderPadding, controlRect.height); Rect minFloatFieldRect = new Rect( controlRect.x + labelWidth, controlRect.y, floatFieldWidth, controlRect.height); Rect maxFloatFieldRect = new Rect( controlRect.x + labelWidth + floatFieldWidth + sliderWidth, controlRect.y, floatFieldWidth, controlRect.height); // Draw the label EditorGUI.LabelField(labelRect, property.displayName); // Draw the slider EditorGUI.BeginChangeCheck(); Vector2 sliderValue = property.vector2Value; EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue); sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x); sliderValue.x = Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y)); sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y); sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue); if (EditorGUI.EndChangeCheck()) { property.vector2Value = sliderValue; } } else { string warning = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 fields"; EditorGUILayout.HelpBox(warning, MessageType.Warning); Debug.LogWarning(warning, PropertyUtility.GetTargetObject(property)); EditorDrawUtility.DrawPropertyField(property); } }