From 0e34dde651792d6dbcd0356daf54677861379d2c Mon Sep 17 00:00:00 2001 From: Robin <1121080+rYuuk@users.noreply.github.com> Date: Thu, 14 Dec 2023 14:13:39 +0100 Subject: [PATCH] [SDK-700] Add asset type filter (#187) ## [SDK-700](https://ready-player-me.atlassian.net/browse/SDK-700) ## Description - Added filter for asset type --- Editor/AssetTypeFilterDrawer.cs | 56 +++++++++++++++++++ Editor/AssetTypeFilterDrawer.cs.meta | 3 + Editor/ReadyPlayerMe.Core.Editor.asmdef | 3 +- Runtime/AvatarCreator/Data/AssetType.cs | 29 ++++++++++ .../UI/AssetTypeFilterAttribute.cs | 18 ++++++ .../UI/AssetTypeFilterAttribute.cs.meta | 3 + .../UI/Elements/AssetSelectionElement.cs | 2 +- .../UI/Elements/ColorSelectionElement.cs | 2 +- 8 files changed, 113 insertions(+), 3 deletions(-) create mode 100644 Editor/AssetTypeFilterDrawer.cs create mode 100644 Editor/AssetTypeFilterDrawer.cs.meta create mode 100644 Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs create mode 100644 Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs.meta diff --git a/Editor/AssetTypeFilterDrawer.cs b/Editor/AssetTypeFilterDrawer.cs new file mode 100644 index 00000000..3c7141b4 --- /dev/null +++ b/Editor/AssetTypeFilterDrawer.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace ReadyPlayerMe.AvatarCreator +{ + [CustomPropertyDrawer(typeof(AssetTypeFilterAttribute))] + public class AssetTypeFilterDrawer : PropertyDrawer + { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + var assetTypeAttribute = attribute as AssetTypeFilterAttribute; + + if (property.propertyType == SerializedPropertyType.Enum) + { + EditorGUI.BeginProperty(position, label, property); + + EditorGUI.BeginChangeCheck(); + + // Get the current enum value + var currentEnumValue = (AssetType) property.enumValueIndex; + + var filteredEnumNames = new List(); + foreach (var enumName in Enum.GetNames(typeof(AssetType))) + { + var enumFieldInfo = typeof(AssetType).GetField(enumName); + var enumAttribute = (AssetTypeFilterAttribute) Attribute.GetCustomAttribute(enumFieldInfo, typeof(AssetTypeFilterAttribute)); + if (enumAttribute == null) continue; + + var filter =(AssetFilter) Enum.Parse(typeof(AssetFilter), enumAttribute.filter.ToString()); + if (filter == assetTypeAttribute?.filter) + { + filteredEnumNames.Add(enumName); + } + } + + // Display the dropdown with filtered enum values + var newIndex = EditorGUI.Popup(position, label.text, Array.IndexOf(filteredEnumNames.ToArray(), currentEnumValue.ToString()), filteredEnumNames.ToArray()); + + // Set the new enum value if it has changed + if (EditorGUI.EndChangeCheck()) + { + property.enumValueIndex = (int) Enum.Parse(typeof(AssetType), filteredEnumNames[newIndex]); + } + + EditorGUI.EndProperty(); + } + else + { + EditorGUI.LabelField(position, label.text, "Use AssetType with Enum."); + } + } + + } +} diff --git a/Editor/AssetTypeFilterDrawer.cs.meta b/Editor/AssetTypeFilterDrawer.cs.meta new file mode 100644 index 00000000..4786e068 --- /dev/null +++ b/Editor/AssetTypeFilterDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6add29e0a6c64d6786c6742dfbd87acf +timeCreated: 1702392584 \ No newline at end of file diff --git a/Editor/ReadyPlayerMe.Core.Editor.asmdef b/Editor/ReadyPlayerMe.Core.Editor.asmdef index a58ad14b..18a1ea32 100644 --- a/Editor/ReadyPlayerMe.Core.Editor.asmdef +++ b/Editor/ReadyPlayerMe.Core.Editor.asmdef @@ -2,7 +2,8 @@ "name": "ReadyPlayerMe.Core.Editor", "rootNamespace": "", "references": [ - "ReadyPlayerMe.Core" + "ReadyPlayerMe.Core", + "ReadyPlayerMe.Core.AvatarCreator" ], "includePlatforms": [ "Editor" diff --git a/Runtime/AvatarCreator/Data/AssetType.cs b/Runtime/AvatarCreator/Data/AssetType.cs index 3c8e2305..dba65c25 100644 --- a/Runtime/AvatarCreator/Data/AssetType.cs +++ b/Runtime/AvatarCreator/Data/AssetType.cs @@ -3,27 +3,56 @@ public enum AssetType { None, + [AssetTypeFilter(AssetFilter.Color)] SkinColor, + [AssetTypeFilter(AssetFilter.Style)] BeardStyle, + [AssetTypeFilter(AssetFilter.Color)] EyeColor, + [AssetTypeFilter(AssetFilter.Style)] EyeShape, + [AssetTypeFilter(AssetFilter.Style)] EyebrowStyle, + [AssetTypeFilter(AssetFilter.Style)] FaceMask, + [AssetTypeFilter(AssetFilter.Style)] FaceShape, + [AssetTypeFilter(AssetFilter.Style)] Glasses, + [AssetTypeFilter(AssetFilter.Style)] HairStyle, + [AssetTypeFilter(AssetFilter.Style)] Facewear, + [AssetTypeFilter(AssetFilter.Style)] Headwear, + [AssetTypeFilter(AssetFilter.Style)] LipShape, + [AssetTypeFilter(AssetFilter.Style)] NoseShape, + [AssetTypeFilter(AssetFilter.Style)] Outfit, + [AssetTypeFilter(AssetFilter.Style)] Shirt, + [AssetTypeFilter(AssetFilter.Color)] HairColor, + [AssetTypeFilter(AssetFilter.Color)] EyebrowColor, + [AssetTypeFilter(AssetFilter.Color)] BeardColor, + [AssetTypeFilter(AssetFilter.Style)] Bottom, + [AssetTypeFilter(AssetFilter.Style)] Top, + [AssetTypeFilter(AssetFilter.Style)] Footwear, + [AssetTypeFilter(AssetFilter.Template)] AvatarTemplate } + + public enum AssetFilter + { + Color, + Style, + Template + } } diff --git a/Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs b/Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs new file mode 100644 index 00000000..8f94c5f2 --- /dev/null +++ b/Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs @@ -0,0 +1,18 @@ +using System; +using UnityEditor.UIElements; +using UnityEngine; +using UnityEngine.UIElements; + +namespace ReadyPlayerMe.AvatarCreator +{ + public class AssetTypeFilterAttribute : PropertyAttribute + { + public AssetFilter filter; + + public AssetTypeFilterAttribute(AssetFilter filter) + { + this.filter = filter; + } + } + +} diff --git a/Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs.meta b/Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs.meta new file mode 100644 index 00000000..a1e97d5f --- /dev/null +++ b/Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f8a8897044b240048e2bec9300d37561 +timeCreated: 1702392185 \ No newline at end of file diff --git a/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs b/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs index d87edc85..d4931765 100644 --- a/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs +++ b/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs @@ -15,7 +15,7 @@ public class AssetSelectionElement : SelectionElement { [Header("Properties")] [SerializeField] private BodyType bodyType = BodyType.FullBody; - [SerializeField] private AssetType assetType; + [SerializeField, AssetTypeFilter(AssetFilter.Style)] private AssetType assetType; [SerializeField] private int iconSize = 64; private PartnerAsset[] assets; diff --git a/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs b/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs index fda17f39..07ea1571 100644 --- a/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs +++ b/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs @@ -11,7 +11,7 @@ namespace ReadyPlayerMe.AvatarCreator public class ColorSelectionElement : SelectionElement { [Header("Properties")] - [SerializeField] private AssetType assetType; + [SerializeField, AssetTypeFilter(AssetFilter.Color)] private AssetType assetType; private AssetColor[] colorAssets; private readonly AvatarAPIRequests avatarAPIRequests = new AvatarAPIRequests();