public override void ValidateProperty(SerializedProperty property) { PrefabAttribute requiredAttribute = PropertyUtility.GetAttribute <PrefabAttribute>(property); if (property.propertyType == SerializedPropertyType.ObjectReference) { if (property.objectReferenceValue == null) { EditorGUILayout.HelpBox(property.name + " can't be null!", MessageType.Error); } else { if (PrefabUtility.GetCorrespondingObjectFromSource(property.objectReferenceValue) == null && PrefabUtility.GetPrefabInstanceHandle(property.objectReferenceValue) == null) { string errorMessage = property.name + " must be a prefab!"; if (!string.IsNullOrEmpty(requiredAttribute.Message)) { errorMessage = requiredAttribute.Message; } EditorGUILayout.HelpBox(errorMessage, MessageType.Error); } } } else { string warning = requiredAttribute.GetType().Name + " works only on reference types"; EditorGUILayout.HelpBox(warning, MessageType.Warning); } }
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 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 void DrawProperty(SerializedProperty property) { EditorDrawUtility.DrawHeader(property); if (property.propertyType != SerializedPropertyType.Float && property.propertyType != SerializedPropertyType.Integer) { EditorGUILayout.HelpBox("Field " + property.name + " is not a number", MessageType.Warning); return; } var value = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue; var valueFormatted = property.propertyType == SerializedPropertyType.Integer ? value.ToString() : String.Format("{0:0.00}", value); ProgressBarAttribute progressBarAttribute = PropertyUtility.GetAttribute <ProgressBarAttribute>(property); var position = EditorGUILayout.GetControlRect(); var maxValue = progressBarAttribute.MaxValue; float lineHight = EditorGUIUtility.singleLineHeight; float padding = EditorGUIUtility.standardVerticalSpacing; var barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHight); var fillPercentage = value / maxValue; var barLabel = (!string.IsNullOrEmpty(progressBarAttribute.Name) ? "[" + progressBarAttribute.Name + "] " : "") + valueFormatted + "/" + maxValue; var color = GetColor(progressBarAttribute.Color); var color2 = Color.white; DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2); }
public override void DrawProperty(SerializedProperty property) { EditorDrawUtility.DrawPropertyField(property); if (property.propertyType == SerializedPropertyType.ObjectReference) { if (property.objectReferenceValue != null) { Texture2D previewTexture = AssetPreview.GetAssetPreview(property.objectReferenceValue); if (previewTexture != null) { ShowAssetPreviewAttribute showAssetPreviewAttribute = PropertyUtility.GetAttribute <ShowAssetPreviewAttribute>(property); int width = Mathf.Clamp(showAssetPreviewAttribute.Width, 0, previewTexture.width); int height = Mathf.Clamp(showAssetPreviewAttribute.Height, 0, previewTexture.height); GUILayout.Label(previewTexture, GUILayout.MaxWidth(width), GUILayout.MaxHeight(height)); } else { this.DrawWarningBox(property.name + " doesn't have an asset preview", property); } } } else { this.DrawWarningBox(property.name + " doesn't have an asset preview", property); } }
public override void ValidateProperty(SerializedProperty property) { GameObjectTagAttribute requiredAttribute = PropertyUtility.GetAttribute <GameObjectTagAttribute>(property); if (property.propertyType == SerializedPropertyType.ObjectReference) { if (property.objectReferenceValue == null) { EditorGUILayout.HelpBox(property.name + " can't be null!", MessageType.Error); } else { GameObject go = (GameObject)property.objectReferenceValue; if (!go.CompareTag(requiredAttribute.Tag)) { string errorMessage = requiredAttribute.DefaultMessage; if (!string.IsNullOrEmpty(requiredAttribute.Message)) { errorMessage = requiredAttribute.Message; } EditorGUILayout.HelpBox(errorMessage, MessageType.Error); } } } else { string warning = requiredAttribute.GetType().Name + " works only on reference types"; EditorGUILayout.HelpBox(warning, MessageType.Warning); } }
public static bool DrawHeader(SerializedProperty property) { HeaderAttribute headerAttr = PropertyUtility.GetAttribute <HeaderAttribute>(property); if (headerAttr != null) { DrawHeader(headerAttr.header); return(true); } return(false); }
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 ValidateProperty(SerializedProperty property) { ComponentAttribute requiredAttribute = PropertyUtility.GetAttribute <ComponentAttribute>(property); if (!requiredAttribute.RequiredType.IsSubclassOf(typeof(Component))) { EditorGUILayout.HelpBox("Wrong component type!", MessageType.Warning); return; } if (property.propertyType == SerializedPropertyType.ObjectReference) { if (property.objectReferenceValue == null) { EditorGUILayout.HelpBox(property.name + " can't be null!", MessageType.Error); } else { GameObject referenceGameObject = (GameObject)property.objectReferenceValue; if (referenceGameObject.GetComponent(requiredAttribute.RequiredType) == null) { string errorMessage = property.name + " must contains " + requiredAttribute.RequiredType.Name + " component!"; if (!string.IsNullOrEmpty(requiredAttribute.Message)) { errorMessage = requiredAttribute.Message; } EditorGUILayout.HelpBox(errorMessage, MessageType.Error); } } } else { EditorGUILayout.HelpBox(requiredAttribute.GetType().Name + " works only on reference types", MessageType.Warning); } }
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; } EditorGUILayout.HelpBox(errorMessage, MessageType.Error); } } else { string warning = requiredAttribute.GetType().Name + " works only on reference types"; EditorGUILayout.HelpBox(warning, MessageType.Warning); } }
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); } }
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); } }