Skip to content

Commit

Permalink
feat(colors): mark avatar's equipped colors as selected (#327)
Browse files Browse the repository at this point in the history
## [SRV-1555](https://ready-player-me.atlassian.net/browse/SRV-1555)

## Description

- Fixes #325.
- These changes ensure that equipped colors are now taken into account
when loading an avatar, instead of defaulting to the first color.

## How to Test

- Open the AvatarCreatorWizard scene.
- Create a new avatar using the template.
- In the editor, navigate to the skin, eyebrow, hair, or beard category.
- Verify that the displayed color matches what the avatar is currently
wearing.


https://github.com/user-attachments/assets/0a8c7992-ed94-4ece-8b66-9cf7bbbf2962

[SRV-1555]:
https://ready-player-me.atlassian.net/browse/SRV-1555?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

---------

Co-authored-by: Harrison Hough <[email protected]>
  • Loading branch information
sander-rpm and HarrisonHough authored Dec 16, 2024
1 parent 6117328 commit ebfa6ed
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ Steps for trying out avatar creator sample can be found [here.](Documentation~/A
A guide for customizing avatar creator can be found [here.](Documentation~/CustomizationGuide.md)

### Note
- [*]Camera support is only provided for Windows and WebGL, using Unity’s webcam native API.
- Camera support is only provided for Windows and WebGL, using Unity’s webcam native API.
- Unity does not have a native file picker, so we have discontinued support for this feature.
- To add support for file picker (for selfies) you have to implement it yourself
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void SetSelectedIcon(string assetId, AssetType category)
SelectButton(category, buttonsById[assetId]);
}

public void CreateColorUI(Dictionary<AssetType, AssetColor[]> colorLibrary, Action<object, AssetType> onClick)
public void CreateColorUI(Dictionary<AssetType, AssetColor[]> colorLibrary, Action<object, AssetType> onClick, Dictionary<AssetType, int> colorAssets)
{
foreach (var colorPalette in colorLibrary)
{
Expand All @@ -82,8 +82,13 @@ public void CreateColorUI(Dictionary<AssetType, AssetColor[]> colorLibrary, Acti
var button = AddColorButton(assetIndex, parent.transform, colorPalette.Key, onClick);
button.SetColor(assetColor.HexColor);

// By default first color is applied on initial draft
if (assetIndex == 0)
int equippedValue = 0;
if (colorAssets.TryGetValue(assetColor.AssetType, out var value))
{
equippedValue = value;
}

if (assetIndex == equippedValue)
{
SelectButton(colorPalette.Key, button);
}
Expand Down Expand Up @@ -171,31 +176,31 @@ private void ConfigureOutfitSelection(AssetType category)
case AssetType.Top:
case AssetType.Bottom:
case AssetType.Footwear:
{
if (selectedButtonsByCategory.TryGetValue(AssetType.Outfit, out AssetButton outfitButton))
{
outfitButton.SetSelect(false);
if (selectedButtonsByCategory.TryGetValue(AssetType.Outfit, out AssetButton outfitButton))
{
outfitButton.SetSelect(false);
}
break;
}
break;
}
case AssetType.Outfit:
{
if (selectedButtonsByCategory.TryGetValue(AssetType.Top, out AssetButton topButton))
{
topButton.SetSelect(false);
if (selectedButtonsByCategory.TryGetValue(AssetType.Top, out AssetButton topButton))
{
topButton.SetSelect(false);
}

if (selectedButtonsByCategory.TryGetValue(AssetType.Bottom, out AssetButton bottomButton))
{
bottomButton.SetSelect(false);
}

if (selectedButtonsByCategory.TryGetValue(AssetType.Footwear, out AssetButton footwearButton))
{
footwearButton.SetSelect(false);
}
break;
}

if (selectedButtonsByCategory.TryGetValue(AssetType.Bottom, out AssetButton bottomButton))
{
bottomButton.SetSelect(false);
}

if (selectedButtonsByCategory.TryGetValue(AssetType.Footwear, out AssetButton footwearButton))
{
footwearButton.SetSelect(false);
}
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -171,6 +173,7 @@ private async Task<GameObject> LoadAvatar()
else
{
var id = AvatarCreatorData.AvatarProperties.Id;

if (!AvatarCreatorData.IsExistingAvatar)
{
var avatarTemplateResponse = await avatarManager.CreateAvatarFromTemplateAsync(id);
Expand Down Expand Up @@ -198,10 +201,32 @@ private async Task LoadAvatarColors()
{
var startTime = Time.time;
var colors = await avatarManager.LoadAvatarColors();
assetButtonCreator.CreateColorUI(colors, UpdateAvatar);
var equippedColors = GetEquippedColors();

assetButtonCreator.CreateColorUI(colors, UpdateAvatar, equippedColors);
SDKLogger.Log(TAG, $"All colors loaded in {Time.time - startTime:F2}s");
}

private Dictionary<AssetType, int> GetEquippedColors()
{
var colorAssetTypes = AssetTypeHelper.GetAssetTypesByFilter(AssetFilter.Color).ToHashSet();
return AvatarCreatorData.AvatarProperties.Assets
.Where(kvp => colorAssetTypes.Contains(kvp.Key))
.ToDictionary(
kvp => kvp.Key,
kvp =>
{
try
{
return Convert.ToInt32(kvp.Value);
}
catch
{
return 0;
}
});
}

private void CreateUI()
{
categoryUICreator.Setup();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ReadyPlayerMe.AvatarCreator;

namespace ReadyPlayerMe.Samples.AvatarCreatorWizard
{
public static class AssetTypeHelper
{
public static IEnumerable<AssetType> GetAssetTypesByFilter(AssetFilter filter)
{
return Enum.GetValues(typeof(AssetType))
.Cast<AssetType>()
.Where(assetType =>
{
var fieldInfo = typeof(AssetType).GetField(assetType.ToString());
var attribute = fieldInfo?.GetCustomAttribute<AssetTypeFilterAttribute>();
return attribute?.filter == filter;
});
}
}
}

0 comments on commit ebfa6ed

Please sign in to comment.