public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { HideWhenFalseAttribute hiddenAttribute = attribute as HideWhenFalseAttribute; SerializedProperty boolProperty = property.serializedObject.FindProperty(hiddenAttribute.hideBoolean); if (boolProperty.boolValue) { EditorGUI.PropertyField(position, property, label, true); } }
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { HideWhenFalseAttribute hiddenAttribute = attribute as HideWhenFalseAttribute; SerializedProperty boolProperty = property.serializedObject.FindProperty(hiddenAttribute.hideBoolean); if (!boolProperty.boolValue) { return(0f); } return(EditorGUI.GetPropertyHeight(property)); }
public void ModifyOptionIncrement(PowerPropertyAttribute ppa, SerializedObject serializedObject, SerializedProperty property, FieldInfo fieldInfo) { #region Color Modify if (!mainColorThemeAssigned) { ColorThemePreset.ColorTheme colorTheme = ColorThemePreset.Instance.GetColorTheme(ppa.theme); //Solve base color settings if (colorTheme == null) { fontColor = GUI.contentColor; backgroundColor = new Color(1, 1, 1, 0); } else { if (!Application.isPlaying) { fontColor = colorTheme.fontColor; backgroundColor = colorTheme.backgroundColor; } else { fontColor = colorTheme.runtimeFontColor; backgroundColor = colorTheme.runtimeBackgroundColor; } } } if (ppa is ColorThemeAttribute) { ColorThemeAttribute ct = ppa as ColorThemeAttribute; //Additional modify color settings if (!Application.isPlaying) { if (PowerInspectorUtility.CheckColorCodeValid(ct.fontColor)) { fontColor = PowerInspectorUtility.ColorCode2Color(ct.fontColor); } if (PowerInspectorUtility.CheckColorCodeValid(ct.backgroundColor)) { backgroundColor = PowerInspectorUtility.ColorCode2Color(ct.backgroundColor); } } else { if (PowerInspectorUtility.CheckColorCodeValid(ct.runtimeFontColor)) { fontColor = PowerInspectorUtility.ColorCode2Color(ct.runtimeFontColor); } if (PowerInspectorUtility.CheckColorCodeValid(ct.runtimeBackgroundColor)) { backgroundColor = PowerInspectorUtility.ColorCode2Color(ct.runtimeBackgroundColor); } } mainColorThemeAssigned = true; } #endregion #region Access Level Modify if (ppa is PresetDataAttribute) { if (Application.isPlaying) { readOnly = true; } else { readOnly = false; } } if (ppa is RuntimeDataAttribute) { if (Application.isPlaying) { readOnly = true; visible = true; } else { visible = false; } } if (ppa is HideWhenTrueAttribute) { HideWhenTrueAttribute hwt = ppa as HideWhenTrueAttribute; SerializedProperty sp = PowerInspectorUtility.GetSiblingProperty(serializedObject, property, hwt.condition); if (sp == null || sp.propertyType != SerializedPropertyType.Boolean) { Debug.LogWarningFormat("Boolean-typed Property '{0}' not found in class '{1}'", hwt.condition, serializedObject.targetObject.GetType().Name); } else { visible = !sp.boolValue; } } if (ppa is HideWhenFalseAttribute) { HideWhenFalseAttribute hwt = ppa as HideWhenFalseAttribute; SerializedProperty sp = PowerInspectorUtility.GetSiblingProperty(serializedObject, property, hwt.condition); if (sp == null || sp.propertyType != SerializedPropertyType.Boolean) { Debug.LogWarningFormat("Boolean-typed Property '{0}' not found in class '{1}'", hwt.condition, serializedObject.targetObject.GetType().Name); } else { visible = sp.boolValue; } } if (ppa is ReadOnlyAttribute) { readOnly = true; } #endregion #region Custom Drawer Modify if (ppa is PowerListAttribute) { heightBeforeDraw = 0f; string data = PowerInspectorUtility.GetMemberName(property.propertyPath); string listPath = ""; Regex elementPattern = new Regex(@"data\[\d+\]"); if (elementPattern.IsMatch(data))//PowerList works on list only { data = data.Replace("data[", ""); data = data.Replace("]", ""); int index = int.Parse(data); listPath = PowerInspectorUtility.GetPrefixPath(property.propertyPath); listPath = listPath.Substring(0, listPath.Length - 1); listPath = PowerInspectorUtility.GetPrefixPath(listPath); listPath = listPath.Substring(0, listPath.Length - 1); if (index == 0)//Only draw in first element { if (overrideDrawer == null) { overrideDrawer = (r, p, l) => { return(PowerListDrawerManager.Instance.DrawPowerList(r, p.serializedObject.targetObject, listPath, (ppa as PowerListAttribute).Key)); } } ; } else { visible = false; } } else { if (overrideDrawer == null) { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":'PowerListAttribute' should not be applied to non-list type field!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); } } ; } } if (ppa is StringSelectorAttribute) { if (fieldInfo.FieldType != typeof(string)) { if (overrideDrawer == null) { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":'StringSelectorAttribute' should not be applied to non-string type property!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); } } ; } else if (overrideDrawer == null) { StringSelectorAttribute ss = ppa as StringSelectorAttribute; string listGetterMethodPath = PowerInspectorUtility.GetPrefixPath(property.propertyPath) + ss.ListGetter; MethodInfo methodInfo = PowerInspectorUtility.GetMemberInfoResursively <MethodInfo>(serializedObject.targetObject.GetType(), listGetterMethodPath); if (methodInfo != null && methodInfo.ReturnType == typeof(List <string>)) { List <string> list = PowerInspectorUtility.InvokeMethodRecursively(serializedObject.targetObject, listGetterMethodPath) as List <string>; if (list != null) { if (list.Count != 0) { overrideDrawer = (r, p, l) => { float height = 0f; string value = p.stringValue; bool contain = false; contain = list.Contains(value); if (!contain)//Try resolve value missing problem { Color color = GUI.backgroundColor; GUI.backgroundColor = Color.red; contain = GUI.Button(r, property.displayName + ":value missing in selecting list ,click to resolve!"); height += r.height; GUI.backgroundColor = color; if (contain) { value = list[0]; } } if (contain) { value = PowerInspectorUtility.DrawStringSelector(r, value, list, l); height += EditorGUIUtility.singleLineHeight; } p.stringValue = value; return(height); }; } else { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawWarningMessage(r, p.displayName + ":Target list is empty!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; } } else { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":Target list is null!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; } } else { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":Method '" + listGetterMethodPath + "' validate failed!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; } } } if (ppa is ElementSelectorAttribute) { if (fieldInfo.FieldType != typeof(string)) { if (overrideDrawer == null) { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":'ElementSelectorAttribute' should not be applied to non-string type property!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); } } ; } else if (overrideDrawer == null) { ElementSelectorAttribute es = ppa as ElementSelectorAttribute; string listGetterMethodPath = PowerInspectorUtility.GetPrefixPath(property.propertyPath) + es.ListGetter; MethodInfo methodInfo = PowerInspectorUtility.GetMemberInfoResursively <MethodInfo>(serializedObject.targetObject.GetType(), listGetterMethodPath); if (methodInfo != null) { object output = PowerInspectorUtility.InvokeMethodRecursively(serializedObject.targetObject, listGetterMethodPath); if (output == null) { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":Target list is null!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; } else { if (output is IList) { IList olist = output as IList; if (olist.Count != 0) { FieldInfo displayFieldInfo = olist[0].GetType().GetField(es.DisplayKey == null ? "" : es.DisplayKey, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); FieldInfo refFieldInfo = olist[0].GetType().GetField(es.ReferenceKey == null ? "" : es.ReferenceKey, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); if (displayFieldInfo == null) { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":Display key word '" + es.DisplayKey + "' does not exist in target type!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; return; } if (refFieldInfo == null) { refFieldInfo = olist[0].GetType().GetField(es.DisplayKey); } List <string> displayKeyList = new List <string>(); List <string> referenceKeyList = new List <string>(); foreach (var o in olist) { displayKeyList.Add(displayFieldInfo.GetValue(o).ToString()); } foreach (var o in olist) { referenceKeyList.Add(refFieldInfo.GetValue(o).ToString()); } overrideDrawer = (r, p, l) => { float height = 0f; string refValue = p.stringValue; bool contain = false; contain = referenceKeyList.Contains(refValue); if (!contain)//Try resolve value missing problem { Color color = GUI.backgroundColor; GUI.backgroundColor = Color.red; contain = GUI.Button(r, property.displayName + ":value missing in selecting list ,click to resolve!"); height += r.height; heightAfterDraw = heightBeforeDraw; GUI.backgroundColor = color; if (contain && referenceKeyList.Count != 0) { refValue = referenceKeyList[0]; } } if (contain) { int index = referenceKeyList.IndexOf(refValue); string displayValue = displayKeyList[index]; displayValue = PowerInspectorUtility.DrawStringSelector(r, displayValue, displayKeyList, l); height += EditorGUIUtility.singleLineHeight; index = displayKeyList.IndexOf(displayValue); refValue = referenceKeyList[index]; } p.stringValue = refValue; return(height); }; } else { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawWarningMessage(r, p.displayName + ":Target list is empty!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; } } else { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":Method '" + listGetterMethodPath + "' return type is not a list!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; } } } else { overrideDrawer = (r, p, l) => { return(PowerInspectorUtility.DrawErrorMessage(r, p.displayName + ":Method '" + listGetterMethodPath + "' does not exist!", (float)(1.5 * EditorGUIUtility.singleLineHeight))); }; } } } #endregion } }