Skip to content

Commit

Permalink
chore: cleanup and added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
HarrisonHough committed Feb 1, 2024
1 parent 7678c1c commit f756b1d
Showing 1 changed file with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

namespace ReadyPlayerMe.Samples.AvatarCreatorExperimental
{

/// <summary>
/// A class responsible for creating and customizing avatars using asset and color selections.
/// </summary>
public class SimpleAvatarCreator : MonoBehaviour
{
public UnityEvent<AvatarProperties> onAvatarCreated;
Expand All @@ -24,20 +28,24 @@ public class SimpleAvatarCreator : MonoBehaviour
private AvatarManager avatarManager;
private GameObject avatar;

/// <summary>
/// Start is used to initialize the avatar creator and loads initial avatar assets.
/// </summary>
private async void Start()
{
await AuthManager.LoginAsAnonymous();
avatarManager = new AvatarManager();

loading.SetActive(true);
GetAssets();
var avatarProperties = await GetTemplateAvatar();
LoadAssets();
var avatarProperties = await CreateTemplateAvatar();
GetColors(avatarProperties);
loading.SetActive(false);
}

private void OnEnable()
{
// Subscribes to asset selection events when this component is enabled.
foreach (var element in assetSelectionElements)
{
element.onAssetSelected.AddListener(OnAssetSelection);
Expand All @@ -51,6 +59,7 @@ private void OnEnable()

private void OnDisable()
{
// Unsubscribes from asset selection events when this component is disabled.
foreach (var element in assetSelectionElements)
{
element.onAssetSelected.RemoveListener(OnAssetSelection);
Expand All @@ -62,24 +71,40 @@ private void OnDisable()
}
}

/// <summary>
/// Handles the selection of an asset and updates the avatar accordingly.
/// </summary>
/// <param name="assetData">The selected asset data.</param>
private async void OnAssetSelection(IAssetData assetData)
{
loading.SetActive(true);
var newAvatar = await avatarManager.UpdateAsset(assetData.AssetType, bodyType, assetData.Id);
Destroy(avatar);

// Destroy the old avatar and replace it with the new one.
if (avatar != null)
{
Destroy(avatar);
}
avatar = newAvatar;
SetElements();
SetupAvatar();
loading.SetActive(false);
}

private async void GetAssets()
/// <summary>
/// Loads and initializes asset selection elements for avatar customization.
/// </summary>
private async void LoadAssets()
{
foreach (var element in assetSelectionElements)
{
element.LoadAndCreateButtons(gender);
}
}

/// <summary>
/// Loads and initializes color selection elements for choosing avatar colors.
/// </summary>
/// <param name="avatarProperties">The properties of the avatar.</param>
private void GetColors(AvatarProperties avatarProperties)
{
foreach (var element in colorSelectionElements)
Expand All @@ -88,21 +113,27 @@ private void GetColors(AvatarProperties avatarProperties)
}
}

private async Task<AvatarProperties> GetTemplateAvatar()
/// <summary>
/// Creates an avatar from a template and sets its initial properties.
/// </summary>
/// <returns>The properties of the created avatar.</returns>
private async Task<AvatarProperties> CreateTemplateAvatar()
{
var avatarTemplateFetcher = new AvatarTemplateFetcher();
var templates = await avatarTemplateFetcher.GetTemplates();
var avatarTemplate = templates[1];

var templateAvatarProps = await avatarManager.CreateAvatarFromTemplate(avatarTemplate.Id, bodyType);
avatar = templateAvatarProps.Item1;
SetElements();
//var avatarProperties = await avatarManager.GetAvatarProperties(templateAvatarProps.Item2.Id);
SetupAvatar();
onAvatarCreated?.Invoke(templateAvatarProps.Item2);
return templateAvatarProps.Item2;
}

private void SetElements()
/// <summary>
/// Sets additional elements and components on the created avatar, such as mouse rotation and animation controller.
/// </summary>
private void SetupAvatar()
{
avatar.AddComponent<MouseRotationHandler>();
avatar.AddComponent<AvatarRotator>();
Expand Down

0 comments on commit f756b1d

Please sign in to comment.