Skip to content

feat: add gender option to template selection element #273

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 2 additions & 17 deletions Runtime/AvatarCreator/Scripts/AvatarTemplateFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ public class AvatarTemplateFetcher
{
private readonly CancellationToken ctx;
private readonly AvatarAPIRequests avatarAPIRequests;
private const string TEMPLATE_V2_USAGE_TYPE = "onboarding";
private const string TEMPLATE_V1_USAGE_TYPE = "randomize";
private readonly TemplateVersions templateVersions;

public AvatarTemplateFetcher(CancellationToken ctx = default, TemplateVersions templateVersions = TemplateVersions.V2)
public AvatarTemplateFetcher(CancellationToken ctx = default)
{
this.ctx = ctx;
this.templateVersions = templateVersions;
avatarAPIRequests = new AvatarAPIRequests(ctx);
}

Expand All @@ -38,18 +34,7 @@ public AvatarTemplateFetcher(CancellationToken ctx = default, TemplateVersions t
/// <returns></returns>
public async Task<List<AvatarTemplateData>> GetTemplates()
{
var templates = await avatarAPIRequests.GetAvatarTemplates();
switch (templateVersions)
{
case TemplateVersions.V2:
return templates.Where(template => template.UsageType.Contains(TEMPLATE_V2_USAGE_TYPE)).ToList();
case TemplateVersions.V1:
var filteredTemplates = templates.Where(template => template.UsageType.Contains(TEMPLATE_V1_USAGE_TYPE)).ToList();
return filteredTemplates;
case TemplateVersions.All:
default:
return templates;
}
return await avatarAPIRequests.GetAvatarTemplates();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ReadyPlayerMe.Core;
Expand All @@ -16,13 +17,17 @@ public class TemplateSelectionElement : SelectionElement
{
private const string TAG = nameof(TemplateSelectionElement);
[SerializeField] private TemplateVersions templateVersions = TemplateVersions.V2;
[SerializeField] private OutfitGender gender = OutfitGender.None;
private List<AvatarTemplateData> avatarTemplates;
private AvatarTemplateFetcher avatarTemplateFetcher;
private CancellationToken ctx;

private const string TEMPLATE_V2_USAGE_TYPE = "onboarding";
private const string TEMPLATE_V1_USAGE_TYPE = "randomize";

private void Awake()
{
avatarTemplateFetcher = new AvatarTemplateFetcher(ctx, templateVersions);
avatarTemplateFetcher = new AvatarTemplateFetcher(ctx);
}

/// <summary>
Expand All @@ -31,15 +36,52 @@ private void Awake()
/// </summary>
public async void LoadAndCreateButtons()
{
await LoadTemplateData();
CreateButtons(avatarTemplates.ToArray(), async (button, asset) =>
await LoadAndCreateButtons(false);
}

/// <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 Task LoadAndCreateButtons(bool useCachedResponse)
{
if (!useCachedResponse || avatarTemplates == null)
{
await LoadTemplateData();
}

var filteredTemplates = avatarTemplates!.Where(template => HasCorrectTemplateVersion(template) && HasCorrectGender(template)).ToList();

CreateButtons(filteredTemplates!.ToArray(), async (button, asset) =>
{
var webRequestDispatcher = new WebRequestDispatcher();
var url = $"{asset.ImageUrl}";
var texture = await webRequestDispatcher.DownloadTexture(url);
button.SetIcon(texture);
});
}
private bool HasCorrectTemplateVersion(AvatarTemplateData template)
{
switch (templateVersions)
{
case TemplateVersions.V2:
return template.UsageType.Contains(TEMPLATE_V2_USAGE_TYPE);
case TemplateVersions.V1:
return template.UsageType.Contains(TEMPLATE_V1_USAGE_TYPE);
case TemplateVersions.All:
default:
return true;
}
}

private bool HasCorrectGender(AvatarTemplateData template)
{
if (gender == OutfitGender.None || template.Gender == OutfitGender.None)
{
return true;
}
return gender == template.Gender;
}

/// <summary>
/// Loads avatar template data asynchronously.
Expand Down