Skip to content

Commit

Permalink
[SDK-700] Add asset type filter (#187)
Browse files Browse the repository at this point in the history
## [SDK-700](https://ready-player-me.atlassian.net/browse/SDK-700)

## Description

- Added filter for asset type
  • Loading branch information
rYuuk authored Dec 14, 2023
1 parent 6b2f17b commit 0e34dde
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 3 deletions.
56 changes: 56 additions & 0 deletions Editor/AssetTypeFilterDrawer.cs
Original file line number Diff line number Diff line change
@@ -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<string>();
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.");
}
}

}
}
3 changes: 3 additions & 0 deletions Editor/AssetTypeFilterDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Editor/ReadyPlayerMe.Core.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "ReadyPlayerMe.Core.Editor",
"rootNamespace": "",
"references": [
"ReadyPlayerMe.Core"
"ReadyPlayerMe.Core",
"ReadyPlayerMe.Core.AvatarCreator"
],
"includePlatforms": [
"Editor"
Expand Down
29 changes: 29 additions & 0 deletions Runtime/AvatarCreator/Data/AssetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
18 changes: 18 additions & 0 deletions Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
3 changes: 3 additions & 0 deletions Runtime/AvatarCreator/UI/AssetTypeFilterAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 0e34dde

Please sign in to comment.