-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SDK-700] Add asset type filter (#187)
## [SDK-700](https://ready-player-me.atlassian.net/browse/SDK-700) ## Description - Added filter for asset type
- Loading branch information
Showing
8 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters