//------------------------------------------------------------------------------------// //---------------------------------- METHODS -----------------------------------------// //------------------------------------------------------------------------------------// private float GetMax(MaxValueUsed maxValue, SerializedProperty property, float defaultValue) { switch (maxValue) { case MaxValueUsed.Field: return(_retrievedField.propertyType == SerializedPropertyType.Float ? _retrievedField.floatValue : _retrievedField.intValue); case MaxValueUsed.Method: return((float)_retrievedMethod.Invoke(property.serializedObject.targetObject, null)); case MaxValueUsed.Property: return((float)_retrievedProperty.GetAccessors()[0].Invoke(property.serializedObject.targetObject, null)); default: return(defaultValue); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { #region Variable Declaration var barAttribute = attribute as BarAttribute; float lineHight = EditorGUIUtility.singleLineHeight; float padding = EditorGUIUtility.standardVerticalSpacing; var barPosition = new Rect(position.position.x, position.position.y, position.size.x, lineHight); float fillPercentage = 0; string barLabel = string.Empty; bool maxValueIsInvalid = false; bool currentFieldIsInvalid = false; bool error = false; #endregion Variable Declaration #region Cache Reflection if (!_alreadyAttemptedToRetrieve) { _alreadyAttemptedToRetrieve = true; _classType = property.serializedObject.targetObject.GetType(); _retrievedField = property.serializedObject.FindProperty(barAttribute.maxValueName); if (barAttribute.maxValueName != null) { _retrievedProperty = _classType.GetProperty(barAttribute.maxValueName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); _retrievedMethod = _classType.GetMethod(barAttribute.maxValueName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); } } #endregion Cache Reflection MaxValueUsed maxValueType = MaxValueUsed.StaticValue; if (barAttribute.UsingStaticMaxValue) { maxValueType = MaxValueUsed.StaticValue; } else if (_retrievedMethod != null) { maxValueType = MaxValueUsed.Method; } else if (_retrievedProperty != null) { maxValueType = MaxValueUsed.Property; } else if (_retrievedField != null) { maxValueType = MaxValueUsed.Field; } else // Provided Max Value Is Invalid { maxValueIsInvalid = true; barLabel = "Invalid Field/Property/Method. Please Check."; } // If field with parameter is invalid. currentFieldIsInvalid = property.propertyType != SerializedPropertyType.Integer && property.propertyType != SerializedPropertyType.Float; if (currentFieldIsInvalid) { string message = string.Format("The <b><i>BarAttribute</i></b> Cannot Be Used On \"<i><b>{0}s</b></i>\". Only Ints and Floats.", property.propertyType.ToString()); barLabel = barLabel == String.Empty ? message : string.Format("{0}\nAlso, {1}", barLabel, message); } error = currentFieldIsInvalid || maxValueIsInvalid; // No errors, get normal bar label. if (!error) { float current = property.propertyType == SerializedPropertyType.Integer ? property.intValue : property.floatValue; float max = GetMax(maxValueType, property, barAttribute.staticMaxValue); // Get Fill % fillPercentage = current / max; // Draw Label Text barLabel = string.Format("{0} ({3:0.0}%) | {1:0.0}/{2:0.0}", string.Format("{0}{1}", property.name[0].ToString().ToUpperInvariant(), property.name.Substring(1)), current, max, fillPercentage * 100); } EditorGUI.PropertyField(new Rect(position.position.x, position.position.y + lineHight + padding, position.size.x, lineHight), property); #region Draw Result if (error) { GUI.Label(barPosition, string.Format("<color=red>{0}</color>", barLabel), new GUIStyle { richText = true }); } else { var color = BarAttribute.GetConcreteColor(barAttribute.color); var color2 = Color.white; DrawBar(barPosition, Mathf.Clamp01(fillPercentage), barLabel, color, color2); } #endregion Draw Result // Force to always redraw, else when inspector gets out of focus, any highlight will also be turned off. EditorUtility.SetDirty(property.serializedObject.targetObject); }