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

feat(colors): mark avatar's equipped colors as selected #327

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
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
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 = AssetFilterHelper.GetAssetTypesByFilter(AssetFilter.Color).ToHashSet();
HarrisonHough marked this conversation as resolved.
Show resolved Hide resolved
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,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ReadyPlayerMe.AvatarCreator;
public static class AssetTypeHelper
HarrisonHough marked this conversation as resolved.
Show resolved Hide resolved
{
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;
});
}
}
Loading