Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-700] Add asset type filter #187

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Other)]
AvatarTemplate
}

public enum AssetFilter
{
Color,
Style,
Other
rYuuk marked this conversation as resolved.
Show resolved Hide resolved
}
}
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.

56 changes: 56 additions & 0 deletions Runtime/AvatarCreator/UI/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 Runtime/AvatarCreator/UI/AssetTypeFilterDrawer.cs.meta

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

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
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