From 47b640ffcd25bf27164f54b6af5500c81b0742fe Mon Sep 17 00:00:00 2001 From: sander-rpm Date: Fri, 13 Dec 2024 16:29:41 +0200 Subject: [PATCH 1/4] feat(colors): mark avatar's equipped colors as selected --- .../Scripts/UI/AssetButtonCreator.cs | 11 +++++++--- .../AvatarCreatorSelection.cs | 22 ++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs index dde1ab1f..08459223 100644 --- a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs @@ -71,7 +71,7 @@ private void SetSelectedIcon(string assetId, AssetType category) SelectButton(category, buttonsById[assetId]); } - public void CreateColorUI(Dictionary colorLibrary, Action onClick) + public void CreateColorUI(Dictionary colorLibrary, Action onClick, Dictionary colorAssets) { foreach (var colorPalette in colorLibrary) { @@ -82,8 +82,13 @@ public void CreateColorUI(Dictionary 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); } diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs index 0452a565..3c8113d9 100644 --- a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using System.Reflection; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -171,6 +173,7 @@ private async Task LoadAvatar() else { var id = AvatarCreatorData.AvatarProperties.Id; + if (!AvatarCreatorData.IsExistingAvatar) { var avatarTemplateResponse = await avatarManager.CreateAvatarFromTemplateAsync(id); @@ -198,10 +201,27 @@ private async Task LoadAvatarColors() { var startTime = Time.time; var colors = await avatarManager.LoadAvatarColors(); - assetButtonCreator.CreateColorUI(colors, UpdateAvatar); + + var colorAssetTypes = GetAssetTypesByFilter(AssetFilter.Color).ToHashSet(); + var equippedColors = AvatarCreatorData.AvatarProperties.Assets.Where(kvp => colorAssetTypes.Contains(kvp.Key)) + .ToDictionary(kvp => kvp.Key, kvp => Convert.ToInt32(kvp.Value)); + + assetButtonCreator.CreateColorUI(colors, UpdateAvatar, equippedColors); SDKLogger.Log(TAG, $"All colors loaded in {Time.time - startTime:F2}s"); } + public static IEnumerable GetAssetTypesByFilter(AssetFilter filter) + { + return Enum.GetValues(typeof(AssetType)) + .Cast() + .Where(assetType => + { + var fieldInfo = typeof(AssetType).GetField(assetType.ToString()); + var attribute = fieldInfo?.GetCustomAttribute(); + return attribute?.filter == filter; + }); + } + private void CreateUI() { categoryUICreator.Setup(); From 9f7bbab1cecd3f383dbe4d96ed8509f61fa694ae Mon Sep 17 00:00:00 2001 From: sander-rpm Date: Fri, 13 Dec 2024 16:51:39 +0200 Subject: [PATCH 2/4] refactor: extract the helper method to a separate file --- .../AvatarCreatorSelection.cs | 14 +------------- .../Scripts/Utils/AssetTypeHelper.cs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs index 3c8113d9..51bf70ca 100644 --- a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs @@ -202,7 +202,7 @@ private async Task LoadAvatarColors() var startTime = Time.time; var colors = await avatarManager.LoadAvatarColors(); - var colorAssetTypes = GetAssetTypesByFilter(AssetFilter.Color).ToHashSet(); + var colorAssetTypes = AssetFilterHelper.GetAssetTypesByFilter(AssetFilter.Color).ToHashSet(); var equippedColors = AvatarCreatorData.AvatarProperties.Assets.Where(kvp => colorAssetTypes.Contains(kvp.Key)) .ToDictionary(kvp => kvp.Key, kvp => Convert.ToInt32(kvp.Value)); @@ -210,18 +210,6 @@ private async Task LoadAvatarColors() SDKLogger.Log(TAG, $"All colors loaded in {Time.time - startTime:F2}s"); } - public static IEnumerable GetAssetTypesByFilter(AssetFilter filter) - { - return Enum.GetValues(typeof(AssetType)) - .Cast() - .Where(assetType => - { - var fieldInfo = typeof(AssetType).GetField(assetType.ToString()); - var attribute = fieldInfo?.GetCustomAttribute(); - return attribute?.filter == filter; - }); - } - private void CreateUI() { categoryUICreator.Setup(); diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs new file mode 100644 index 00000000..f03d7a46 --- /dev/null +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using ReadyPlayerMe.AvatarCreator; +public static class AssetTypeHelper +{ + public static IEnumerable GetAssetTypesByFilter(AssetFilter filter) + { + return Enum.GetValues(typeof(AssetType)) + .Cast() + .Where(assetType => + { + var fieldInfo = typeof(AssetType).GetField(assetType.ToString()); + var attribute = fieldInfo?.GetCustomAttribute(); + return attribute?.filter == filter; + }); + } +} From 3170c4cbe5a671b4b0e1d26719b96313664e1740 Mon Sep 17 00:00:00 2001 From: sander-rpm Date: Fri, 13 Dec 2024 17:16:40 +0200 Subject: [PATCH 3/4] refactor: safely convert to int; remove typo in readme --- README.md | 2 +- .../AvatarCreatorSelection.cs | 25 ++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 15a70e38..42dcb604 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs index 51bf70ca..b3186ee5 100644 --- a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs @@ -201,15 +201,32 @@ private async Task LoadAvatarColors() { var startTime = Time.time; var colors = await avatarManager.LoadAvatarColors(); - - var colorAssetTypes = AssetFilterHelper.GetAssetTypesByFilter(AssetFilter.Color).ToHashSet(); - var equippedColors = AvatarCreatorData.AvatarProperties.Assets.Where(kvp => colorAssetTypes.Contains(kvp.Key)) - .ToDictionary(kvp => kvp.Key, kvp => Convert.ToInt32(kvp.Value)); + var equippedColors = GetEquippedColors(); assetButtonCreator.CreateColorUI(colors, UpdateAvatar, equippedColors); SDKLogger.Log(TAG, $"All colors loaded in {Time.time - startTime:F2}s"); } + private Dictionary GetEquippedColors() + { + var colorAssetTypes = AssetFilterHelper.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(); From 02bd5b18ae83d609ec4e55e0de6b5af8d4c0e085 Mon Sep 17 00:00:00 2001 From: sander-rpm Date: Mon, 16 Dec 2024 10:38:51 +0200 Subject: [PATCH 4/4] refactor: fix compile error; wrap helper class to namespace --- .../Scripts/UI/AssetButtonCreator.cs | 42 +++++++++---------- .../AvatarCreatorSelection.cs | 2 +- .../Scripts/Utils/AssetTypeHelper.cs | 26 +++++++----- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs index 08459223..a8832aaa 100644 --- a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs @@ -83,7 +83,7 @@ public void CreateColorUI(Dictionary colorLibrary, Acti button.SetColor(assetColor.HexColor); int equippedValue = 0; - if (colorAssets.TryGetValue(assetColor.AssetType, out var value)) + if (colorAssets.TryGetValue(assetColor.AssetType, out var value)) { equippedValue = value; } @@ -176,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.Bottom, out AssetButton bottomButton)) { - bottomButton.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.Footwear, out AssetButton footwearButton)) - { - footwearButton.SetSelect(false); - } - break; - } } } diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs index b3186ee5..eda29bd6 100644 --- a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs @@ -209,7 +209,7 @@ private async Task LoadAvatarColors() private Dictionary GetEquippedColors() { - var colorAssetTypes = AssetFilterHelper.GetAssetTypesByFilter(AssetFilter.Color).ToHashSet(); + var colorAssetTypes = AssetTypeHelper.GetAssetTypesByFilter(AssetFilter.Color).ToHashSet(); return AvatarCreatorData.AvatarProperties.Assets .Where(kvp => colorAssetTypes.Contains(kvp.Key)) .ToDictionary( diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs index f03d7a46..5cc4f65c 100644 --- a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs @@ -3,17 +3,21 @@ using System.Linq; using System.Reflection; using ReadyPlayerMe.AvatarCreator; -public static class AssetTypeHelper + +namespace ReadyPlayerMe.Samples.AvatarCreatorWizard { - public static IEnumerable GetAssetTypesByFilter(AssetFilter filter) + public static class AssetTypeHelper { - return Enum.GetValues(typeof(AssetType)) - .Cast() - .Where(assetType => - { - var fieldInfo = typeof(AssetType).GetField(assetType.ToString()); - var attribute = fieldInfo?.GetCustomAttribute(); - return attribute?.filter == filter; - }); + public static IEnumerable GetAssetTypesByFilter(AssetFilter filter) + { + return Enum.GetValues(typeof(AssetType)) + .Cast() + .Where(assetType => + { + var fieldInfo = typeof(AssetType).GetField(assetType.ToString()); + var attribute = fieldInfo?.GetCustomAttribute(); + return attribute?.filter == filter; + }); + } } -} +} \ No newline at end of file