From 87bfd65e23357ff2eeb45ac5d79a66ebd0db9883 Mon Sep 17 00:00:00 2001 From: Wenlan Hua Date: Wed, 8 Jun 2022 10:39:38 +0200 Subject: [PATCH] Make show if attribute support multiple enums --- .../Core/MetaAttributes/ShowIfAttribute.cs | 6 ++++++ .../Core/MetaAttributes/ShowIfAttributeBase.cs | 16 ++++++++++++++-- .../Scripts/Editor/Utility/PropertyUtility.cs | 7 ++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttribute.cs b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttribute.cs index 99813aa0..08b48322 100644 --- a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttribute.cs +++ b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttribute.cs @@ -22,5 +22,11 @@ public ShowIfAttribute(string enumName, object enumValue) { Inverted = false; } + + public ShowIfAttribute(string enumName, params object[] enumValues) + : base(enumName, enumValues) + { + Inverted = false; + } } } diff --git a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttributeBase.cs b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttributeBase.cs index e48d437e..94c89930 100644 --- a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttributeBase.cs +++ b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/ShowIfAttributeBase.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; namespace NaughtyAttributes { @@ -11,7 +12,7 @@ public class ShowIfAttributeBase : MetaAttribute /// /// If this not null, [0] is name of an enum variable. /// - public Enum EnumValue { get; private set; } + public Enum[] EnumValues { get; private set; } public ShowIfAttributeBase(string condition) { @@ -33,7 +34,18 @@ public ShowIfAttributeBase(string enumName, Enum enumValue) throw new ArgumentNullException(nameof(enumValue), "This parameter must be an enum value."); } - EnumValue = enumValue; + EnumValues = new []{ enumValue }; + } + + public ShowIfAttributeBase(string enumName, params object[] enumValues) + : this(enumName) + { + if (enumValues == null || enumValues.Any(value => value == null)) + { + throw new ArgumentNullException(nameof(enumValues), "All parameters must be enum values."); + } + + EnumValues = enumValues.Select(obj => obj as Enum).ToArray(); } } } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index 2336eb66..41f16c2a 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -3,6 +3,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; using UnityEngine; namespace NaughtyAttributes.Editor @@ -130,14 +131,14 @@ public static bool IsVisible(SerializedProperty property) object target = GetTargetObjectWithProperty(property); // deal with enum conditions - if (showIfAttribute.EnumValue != null) + if (showIfAttribute.EnumValues != null) { Enum value = GetEnumValue(target, showIfAttribute.Conditions[0]); if (value != null) { bool matched = value.GetType().GetCustomAttribute() == null - ? showIfAttribute.EnumValue.Equals(value) - : value.HasFlag(showIfAttribute.EnumValue); + ? showIfAttribute.EnumValues.Contains(value) + : showIfAttribute.EnumValues.Any(element => value.HasFlag(element)); return matched != showIfAttribute.Inverted; }