From 853bb45adcfd57aff6f73ffa588db35b00892773 Mon Sep 17 00:00:00 2001 From: MaxAndreassenRPM Date: Thu, 30 Nov 2023 21:00:34 +0000 Subject: [PATCH] chore: reduce repeated code --- .../UI/Elements/AssetSelectionElement.cs | 37 +++---------- .../UI/Elements/ColorSelectionElement.cs | 24 ++------- .../UI/Elements/SelectionElement.cs | 21 ++++++++ .../UI/Elements/TemplateSelectionElement.cs | 53 +++++-------------- 4 files changed, 46 insertions(+), 89 deletions(-) diff --git a/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs b/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs index 8de3ee6a..55971acb 100644 --- a/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs +++ b/Runtime/AvatarCreator/UI/Elements/AssetSelectionElement.cs @@ -40,41 +40,18 @@ public async Task LoadTemplateData() public async void LoadAndCreateButtons() { await LoadTemplateData(); - CreateButtons(); - await LoadIcons(); - } - - public void CreateButtons() - { - if (partnerAssets.Length == 0) - { - SDKLogger.LogWarning(TAG, "No templates found. You need to load fetch the template data first."); - return; - } - foreach (var partnerAsset in partnerAssets) - { - var button = CreateButton(partnerAsset.Id); - var partnerAssetData = partnerAsset; - button.AddListener(() => AssetSelected(partnerAssetData)); - } - } - - public async Task LoadIcons() - { - foreach (var partnerAsset in partnerAssets) + + CreateButtons(partnerAssets, + async (button, asset) => { var downloadHandler = new DownloadHandlerTexture(); var webRequestDispatcher = new WebRequestDispatcher(); - var url = $"{partnerAsset.ImageUrl}?w=64"; + var url = $"{asset.ImageUrl}?w=64"; var response = await webRequestDispatcher.SendRequest(url, HttpMethod.GET, downloadHandler: downloadHandler); response.ThrowIfError(); - OnIconLoaded(partnerAsset, response.Texture); - } - } - - private void OnIconLoaded(PartnerAsset partnerAsset, Texture texture) - { - UpdateButtonIcon(partnerAsset.Id, texture); + + UpdateButtonIcon(asset.Id, response.Texture); + }); } } } diff --git a/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs b/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs index e193a815..cd0c00f6 100644 --- a/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs +++ b/Runtime/AvatarCreator/UI/Elements/ColorSelectionElement.cs @@ -1,6 +1,5 @@ -using System.Threading; using System.Threading.Tasks; -using ReadyPlayerMe.Core; + using UnityEngine; namespace ReadyPlayerMe.AvatarCreator @@ -21,24 +20,11 @@ public async Task LoadColorPalette(AvatarProperties avatarProperties) public async void LoadAndCreateButtons(AvatarProperties avatarProperties) { await LoadColorPalette(avatarProperties); - CreateButtons(); - } - - public void CreateButtons() - { - if (colorAssets.Length == 0) - { - SDKLogger.LogWarning(TAG, "No templates found. You need to load fetch the template data first."); - return; - } - for (int i = 0; i < colorAssets.Length; i++) + + CreateButtons(colorAssets, (button, asset) => { - var colorAsset = colorAssets[i]; - var button = CreateButton(colorAsset.Id); - button.SetColor(colorAsset.HexColor); - button.AddListener(() => AssetSelected(colorAsset)); - Debug.Log($"Create button {colorAsset.Id}"); - } + button.SetColor(asset.HexColor); + }); } } } diff --git a/Runtime/AvatarCreator/UI/Elements/SelectionElement.cs b/Runtime/AvatarCreator/UI/Elements/SelectionElement.cs index b5ba8c4c..ad40573b 100644 --- a/Runtime/AvatarCreator/UI/Elements/SelectionElement.cs +++ b/Runtime/AvatarCreator/UI/Elements/SelectionElement.cs @@ -1,4 +1,6 @@ +using System; using System.Collections.Generic; +using ReadyPlayerMe.Core; using UnityEngine; using UnityEngine.Events; @@ -12,6 +14,8 @@ public interface IAssetData public abstract class SelectionElement : MonoBehaviour { + private const string TAG = nameof(SelectionElement); + [Header("UI Elements")] [Space(5)] [SerializeField] private ButtonElement buttonElementPrefab; @@ -29,6 +33,23 @@ public ButtonElement CreateButton(string id) return button; } + public void CreateButtons(T[] assets, Action onButtonCreated = default) where T : IAssetData + { + if (assets.Length == 0) + { + SDKLogger.LogWarning(TAG, "No assets provided."); + return; + } + + for (int i = 0; i < assets.Length; i++) + { + var button = CreateButton(assets[i].Id); + button.AddListener(() => AssetSelected(assets[i])); + onButtonCreated?.Invoke(button, assets[i]); + Debug.Log($"Create button {assets[i].Id}"); + } + } + public void ClearButtons() { foreach (var button in buttonMap) diff --git a/Runtime/AvatarCreator/UI/Elements/TemplateSelectionElement.cs b/Runtime/AvatarCreator/UI/Elements/TemplateSelectionElement.cs index e85a54b1..95d29b47 100644 --- a/Runtime/AvatarCreator/UI/Elements/TemplateSelectionElement.cs +++ b/Runtime/AvatarCreator/UI/Elements/TemplateSelectionElement.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using ReadyPlayerMe.Core; +using UnityEngine.Networking; namespace ReadyPlayerMe.AvatarCreator { @@ -12,7 +13,7 @@ public class TemplateSelectionElement : SelectionElement { private const string TAG = nameof(TemplateSelectionElement); - private List avatarTemplateDataList; + private List avatarTemplates; private AvatarTemplateFetcher avatarTemplateFetcher; private void Awake() @@ -26,8 +27,14 @@ private void Awake() public async void LoadAndCreateButtons() { await LoadTemplateData(); - CreateButtons(); - await LoadTemplateRenders(); + CreateButtons(avatarTemplates.ToArray(), async (button, asset) => + { + var downloadHandler = new DownloadHandlerTexture(); + var webRequestDispatcher = new WebRequestDispatcher(); + var url = $"{asset.ImageUrl}?w=64"; + var response = await webRequestDispatcher.SendRequest(url, HttpMethod.GET, downloadHandler: downloadHandler); + button.SetIcon(response.Texture); + }); } /// @@ -35,46 +42,12 @@ public async void LoadAndCreateButtons() /// public async Task LoadTemplateData() { - avatarTemplateDataList = await avatarTemplateFetcher.GetTemplates(); - if (avatarTemplateDataList == null || avatarTemplateDataList.Count == 0) + avatarTemplates = await avatarTemplateFetcher.GetTemplates(); + + if (avatarTemplates == null || avatarTemplates.Count == 0) { SDKLogger.LogWarning(TAG, "No templates found"); } } - - /// - /// Loads avatar template data and icon renders. This will wait for all the icons to be downloaded. - /// - public async Task LoadTemplateRenders() - { - avatarTemplateDataList = await avatarTemplateFetcher.GetTemplatesWithRenders(OnIconLoaded); - } - - private void OnIconLoaded(AvatarTemplateData avatarTemplate) - { - UpdateButtonIcon(avatarTemplate.Id, avatarTemplate.Texture); - } - - /// - /// Creates buttons from the loaded template data and sets the icon and button event. - /// - public void CreateButtons() - { - if (avatarTemplateDataList.Count == 0) - { - SDKLogger.LogWarning(TAG, "No templates found. You need to load fetch the template data first."); - return; - } - for (var i = 0; i < avatarTemplateDataList.Count; i++) - { - var button = CreateButton(avatarTemplateDataList[i].Id); - var templateData = avatarTemplateDataList[i]; - if (templateData.Texture != null) - { - button.SetIcon(templateData.Texture); - } - button.AddListener(() => AssetSelected(templateData)); - } - } } }