-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathTemplateSelectionElement.cs
58 lines (54 loc) · 2.28 KB
/
TemplateSelectionElement.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ReadyPlayerMe.Core;
using UnityEngine;
namespace ReadyPlayerMe.AvatarCreator
{
/// <summary>
/// This class is responsible for fetching avatar template data,
/// including icons, and creating buttons for each template.
/// It serves as a self-contained UI element for avatar template selection.
/// It extends the functionality of SelectionElement to handle AvatarTemplate-specific interactions.
/// </summary>
public class TemplateSelectionElement : SelectionElement
{
private const string TAG = nameof(TemplateSelectionElement);
[SerializeField] private TemplateVersions templateVersions = TemplateVersions.V2;
private List<AvatarTemplateData> avatarTemplates;
private AvatarTemplateFetcher avatarTemplateFetcher;
private CancellationToken ctx;
private void Awake()
{
avatarTemplateFetcher = new AvatarTemplateFetcher(ctx, templateVersions);
}
/// <summary>
/// Asynchronously loads avatar template data and creates button elements for each template.
/// Each button is created with an icon fetched from the template's image URL.
/// </summary>
public async void LoadAndCreateButtons()
{
await LoadTemplateData();
CreateButtons(avatarTemplates.ToArray(), async (button, asset) =>
{
var webRequestDispatcher = new WebRequestDispatcher();
var url = $"{asset.ImageUrl}";
var texture = await webRequestDispatcher.DownloadTexture(url);
button.SetIcon(texture);
});
}
/// <summary>
/// Loads avatar template data asynchronously.
/// This method fetches data but does not handle icon rendering.
/// </summary>
/// <returns>A Task representing the asynchronous operation of loading avatar template data.</returns>
public async Task LoadTemplateData()
{
avatarTemplates = await avatarTemplateFetcher.GetTemplates();
if (avatarTemplates == null || avatarTemplates.Count == 0)
{
SDKLogger.LogWarning(TAG, "No templates found");
}
}
}
}