private object GetValues(SerializedProperty property, string valuesName) { object target = PropertyUtility.GetTargetObjectWithProperty(property); FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, valuesName); if (valuesFieldInfo != null) { return(valuesFieldInfo.GetValue(target)); } PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, valuesName); if (valuesPropertyInfo != null) { return(valuesPropertyInfo.GetValue(target)); } MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, valuesName); if (methodValuesInfo != null && methodValuesInfo.ReturnType != typeof(void) && methodValuesInfo.GetParameters().Length == 0) { return(methodValuesInfo.Invoke(target, null)); } return(null); }
private static List <bool> GetConditionValues(object target, string[] conditions) { List <bool> conditionValues = new List <bool>(); foreach (var condition in conditions) { FieldInfo conditionField = ReflectionUtility.GetField(target, condition); if (conditionField != null && conditionField.FieldType == typeof(bool)) { conditionValues.Add((bool)conditionField.GetValue(target)); } PropertyInfo conditionProperty = ReflectionUtility.GetProperty(target, condition); if (conditionProperty != null && conditionProperty.PropertyType == typeof(bool)) { conditionValues.Add((bool)conditionProperty.GetValue(target)); } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, condition); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { conditionValues.Add((bool)conditionMethod.Invoke(target, null)); } } return(conditionValues); }
/// <summary> /// Gets an enum value from reflection. /// </summary> /// <param name="target">The target object.</param> /// <param name="enumName">Name of a field, property, or method that returns an enum.</param> /// <returns>Null if can't find an enum value.</returns> internal static Enum GetEnumValue(object target, string enumName) { FieldInfo enumField = ReflectionUtility.GetField(target, enumName); if (enumField != null && enumField.FieldType.IsSubclassOf(typeof(Enum))) { return((Enum)enumField.GetValue(target)); } PropertyInfo enumProperty = ReflectionUtility.GetProperty(target, enumName); if (enumProperty != null && enumProperty.PropertyType.IsSubclassOf(typeof(Enum))) { return((Enum)enumProperty.GetValue(target)); } MethodInfo enumMethod = ReflectionUtility.GetMethod(target, enumName); if (enumMethod != null && enumMethod.ReturnType.IsSubclassOf(typeof(Enum))) { return((Enum)enumMethod.Invoke(target, null)); } return(null); }
private object GetMaxValue(SerializedProperty property, ProgressBarAttribute progressBarAttribute) { if (string.IsNullOrEmpty(progressBarAttribute.MaxValueName)) { return(progressBarAttribute.MaxValue); } else { object target = PropertyUtility.GetTargetObjectWithProperty(property); FieldInfo valuesFieldInfo = ReflectionUtility.GetField(target, progressBarAttribute.MaxValueName); if (valuesFieldInfo != null) { return(valuesFieldInfo.GetValue(target)); } PropertyInfo valuesPropertyInfo = ReflectionUtility.GetProperty(target, progressBarAttribute.MaxValueName); if (valuesPropertyInfo != null) { return(valuesPropertyInfo.GetValue(target)); } MethodInfo methodValuesInfo = ReflectionUtility.GetMethod(target, progressBarAttribute.MaxValueName); if (methodValuesInfo != null && (methodValuesInfo.ReturnType == typeof(float) || methodValuesInfo.ReturnType == typeof(int)) && methodValuesInfo.GetParameters().Length == 0) { return(methodValuesInfo.Invoke(target, null)); } return(null); } }
private static AnimatorController GetAnimatorController(SerializedProperty property, string animatorName) { object target = PropertyUtility.GetTargetObjectWithProperty(property); FieldInfo animatorFieldInfo = ReflectionUtility.GetField(target, animatorName); if (animatorFieldInfo != null && animatorFieldInfo.FieldType == typeof(Animator)) { Animator animator = animatorFieldInfo.GetValue(target) as Animator; if (animator != null) { AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController; return(animatorController); } } PropertyInfo animatorPropertyInfo = ReflectionUtility.GetProperty(target, animatorName); if (animatorPropertyInfo != null && animatorPropertyInfo.PropertyType == typeof(Animator)) { Animator animator = animatorPropertyInfo.GetValue(target) as Animator; if (animator != null) { AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController; return(animatorController); } } MethodInfo animatorGetterMethodInfo = ReflectionUtility.GetMethod(target, animatorName); if (animatorGetterMethodInfo != null && animatorGetterMethodInfo.ReturnType == typeof(Animator) && animatorGetterMethodInfo.GetParameters().Length == 0) { Animator animator = animatorGetterMethodInfo.Invoke(target, null) as Animator; if (animator != null) { AnimatorController animatorController = animator.runtimeAnimatorController as AnimatorController; return(animatorController); } } return(null); }
private static List <bool> GetConditionValues(SerializedProperty property, string[] conditions) { var serializedObject = property.serializedObject; List <bool> conditionValues = new List <bool>(); var indexOfDot = property.propertyPath.LastIndexOf('.'); var outerPath = indexOfDot == -1 ? "" : property.propertyPath.Substring(0, indexOfDot); foreach (var condition in conditions) { var conditionPath = outerPath.Length == 0 ? condition : outerPath + "." + condition; var conditionProperty = serializedObject.FindProperty(conditionPath); if (conditionProperty != null && conditionProperty.propertyType == SerializedPropertyType.Boolean) { conditionValues.Add(conditionProperty.boolValue); } else { var target = GetTargetObjectWithProperty(property); var reflectionProperty = ReflectionUtility.GetProperty(target, condition); if (reflectionProperty != null && reflectionProperty.PropertyType == typeof(bool)) { conditionValues.Add((bool)reflectionProperty.GetValue(target)); } else { var reflectionMethod = ReflectionUtility.GetMethod(target, condition); if (reflectionMethod != null && reflectionMethod.ReturnType == typeof(bool) && reflectionMethod.GetParameters().Length == 0) { conditionValues.Add((bool)reflectionMethod.Invoke(target, null)); } } } } return(conditionValues); }
public override bool CanDrawProperty(SerializedProperty property) { ShowIfAttribute showIfAttribute = PropertyUtility.GetAttribute <ShowIfAttribute>(property); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); List <bool> conditionValues = new List <bool>(); foreach (var condition in showIfAttribute.Conditions) { FieldInfo conditionField = ReflectionUtility.GetField(target, condition); if (conditionField?.FieldType == typeof(bool)) { conditionValues.Add((bool)conditionField.GetValue(target)); } MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, condition); if (conditionMethod?.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { conditionValues.Add((bool)conditionMethod.Invoke(target, null)); } PropertyInfo conditionProperty = ReflectionUtility.GetProperty(target, condition); if (conditionProperty?.PropertyType == typeof(bool)) { conditionValues.Add((bool)conditionProperty.GetValue(target)); } } if (conditionValues.Count > 0) { bool draw; if (showIfAttribute.ConditionOperator == ConditionOperator.And) { draw = true; foreach (var value in conditionValues) { draw = draw && value; } } else { draw = false; foreach (var value in conditionValues) { draw = draw || value; } } if (showIfAttribute.Reversed) { draw = !draw; } return(draw); } else { string warning = showIfAttribute.GetType().Name + " needs a valid boolean condition field or method name to work"; EditorDrawUtility.DrawHelpBox(warning, MessageType.Warning, context: target); return(true); } }
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.##}", 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); UnityEngine.Object target = PropertyUtility.GetTargetObject(property); // maxValueVar - if found so override maxValue var maxValueVar = progressBarAttribute.maxValueVar; if (maxValueVar.Trim().Length > 0) { // try to get field first FieldInfo maxValueFieldInfo = ReflectionUtility.GetField(target, maxValueVar); if (maxValueFieldInfo != null) { if (maxValueFieldInfo.FieldType == typeof(int)) { maxValue = (int)maxValueFieldInfo.GetValue(target); } if (maxValueFieldInfo.FieldType == typeof(float)) { maxValue = (float)maxValueFieldInfo.GetValue(target); } } else { // if not get the property PropertyInfo maxValuePropertyInfo = ReflectionUtility.GetProperty(target, maxValueVar); if (maxValuePropertyInfo != null) { if (maxValuePropertyInfo.PropertyType == typeof(int)) { maxValue = (int)maxValuePropertyInfo.GetValue(target); } if (maxValuePropertyInfo.PropertyType == typeof(float)) { maxValue = (float)maxValuePropertyInfo.GetValue(target); } } } } 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); }