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/AssetButtonCreator.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/AssetButtonCreator.cs index dde1ab1f..a8832aaa 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); } @@ -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; - } } } diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs index 0452a565..eda29bd6 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,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 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(); diff --git a/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs new file mode 100644 index 00000000..5cc4f65c --- /dev/null +++ b/Samples~/AvatarCreatorSamples/AvatarCreatorWizard/Scripts/Utils/AssetTypeHelper.cs @@ -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 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