Skip to content
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

[SDK-685] Feature/avatar preview element #181

Merged
merged 5 commits into from
Dec 7, 2023
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
122 changes: 122 additions & 0 deletions Runtime/AvatarCreator/Prefabs/Elements/Avatar Preview Element.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &1740648930196985801
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1740648930196985805}
- component: {fileID: 1740648930196985800}
- component: {fileID: 3027828850030535667}
m_Layer: 0
m_Name: Avatar Preview Element
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1740648930196985805
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1740648930196985801}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 3703985022246169223}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1740648930196985800
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1740648930196985801}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 198f21f00ad242b47bee2a7073994bea, type: 3}
m_Name:
m_EditorClassIdentifier:
cameraTransform: {fileID: 0}
faceViewPoint: {x: 0, y: 1.66, z: 0.85}
bodyViewPoint: {x: 0, y: 1.4, z: 2.6}
defaultDuration: 1
--- !u!114 &3027828850030535667
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1740648930196985801}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1ed4881de9835c940954c6b8ab021a85, type: 3}
m_Name:
m_EditorClassIdentifier:
avatarRotator: {fileID: 7370162397791757437}
resetRotation: 1
--- !u!1 &7195514442768515100
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3703985022246169223}
- component: {fileID: 7370162397791757437}
- component: {fileID: 8765811575219735875}
m_Layer: 0
m_Name: AvatarRotator
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &3703985022246169223
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7195514442768515100}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1740648930196985805}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &7370162397791757437
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7195514442768515100}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 740b98666dec44e5bb949b172ef49d6a, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 50
--- !u!114 &8765811575219735875
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7195514442768515100}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: c3cb903a634eb8c48a7050b2566ff30f, type: 3}
m_Name:
m_EditorClassIdentifier:

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions Runtime/AvatarCreator/UI/Elements/AvatarPreviewElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
{
public class AvatarPreviewElement : MonoBehaviour
{
[SerializeField] private AvatarRotator avatarRotator;
[Tooltip("If true the avatar will face directly at the camera"), SerializeField] private bool resetRotationOnPreview = true;
private GameObject avatar;

public void PreviewAvatar(GameObject newAvatar)
{
if (avatar != null)
{
Destroy(avatar);
}

avatar = newAvatar;
avatar.transform.SetParent(avatarRotator.transform);
avatar.transform.localPosition = Vector3.zero;
avatar.transform.localRotation = Quaternion.identity;
if (resetRotationOnPreview)
{
avatarRotator.ResetRotation();
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/AvatarCreator/UI/Elements/AvatarPreviewElement.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 7 additions & 23 deletions Runtime/AvatarCreator/UI/Elements/GenderSelectElement.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using ReadyPlayerMe.Core;
using UnityEngine;
using UnityEngine.Events;
Expand All @@ -18,33 +19,16 @@ public class GenderSelectElement : MonoBehaviour
[Header("Events")]
public UnityEvent<OutfitGender> OnGenderSelected;

private void Awake()
private void OnEnable()
{
InitializeButtons();
maleButton.onClick.AddListener(MaleButtonClicked);
femaleButton.onClick.AddListener(FemaleButtonClicked);
}

private void InitializeButtons()
private void OnDisable()
{
if (maleButton != null)
{
maleButton.onClick.AddListener(MaleButtonClicked);
}
if (femaleButton != null)
{
femaleButton.onClick.AddListener(FemaleButtonClicked);
}
}

private void OnDestroy()
{
if (maleButton != null)
{
maleButton.onClick.RemoveListener(MaleButtonClicked);
}
if (femaleButton != null)
{
femaleButton.onClick.RemoveListener(FemaleButtonClicked);
}
maleButton.onClick.RemoveListener(MaleButtonClicked);
femaleButton.onClick.RemoveListener(FemaleButtonClicked);
}

private void MaleButtonClicked()
Expand Down
78 changes: 78 additions & 0 deletions Runtime/AvatarCreator/Utils/CameraFocuser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using ReadyPlayerMe.Core;
using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
{
public class CameraFocuser : MonoBehaviour
{
private const string TAG = nameof(CameraFocuser);

[SerializeField] private Transform cameraTransform;
[SerializeField] private Vector3 faceViewPoint = new Vector3(0f, 1.66f, 0.85f);
[SerializeField] private Vector3 bodyViewPoint = new Vector3(0f, 1.4f, 2.6f);
[SerializeField] private float defaultDuration = 1f;

private Vector3 targetPosition;
private Vector3 startPosition;
private float transitionTime;
private float duration;
private bool isTransitioning;

private void Awake()
{
if (cameraTransform == null)
{
SDKLogger.LogWarning(TAG, $"Avatar object not set for {gameObject.name}.");
}
}

private void LateUpdate()
{
if (!isTransitioning) return;
transitionTime += Time.deltaTime;
if (transitionTime < duration)
{
var t = Mathf.SmoothStep(0.0f, 1.0f, transitionTime / duration);
cameraTransform.position = Vector3.Lerp(startPosition, targetPosition, t);
}
else
{
cameraTransform.position = targetPosition;
isTransitioning = false;
}
}

public void FocusOnFace()
{
StartTransition(faceViewPoint, defaultDuration);
}

public void FocusOnBody()
{
StartTransition(bodyViewPoint, defaultDuration);
}

public void StopTransition()
{
isTransitioning = false;
}

private void StartTransition(Vector3 newTargetLocalPosition, float transitionDuration)
{
startPosition = cameraTransform.position;
targetPosition = transform.TransformPoint(newTargetLocalPosition);
duration = transitionDuration;
transitionTime = 0f;
isTransitioning = true;
}

private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawSphere(transform.position + faceViewPoint, 0.1f);

Gizmos.color = Color.blue;
Gizmos.DrawSphere(transform.position + bodyViewPoint, 0.1f);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/AvatarCreator/Utils/CameraFocuser.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 0 additions & 46 deletions Runtime/AvatarCreator/Utils/Rotation/AvatarMouseRotation.cs

This file was deleted.

33 changes: 33 additions & 0 deletions Runtime/AvatarCreator/Utils/Rotation/AvatarRotator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;

namespace ReadyPlayerMe.AvatarCreator
{
public class AvatarRotator : MonoBehaviour
{
private const string TAG = nameof(AvatarRotator);
[SerializeField] private float speed = 50;

private IAvatarRotatorInput avatarRotatorInput;

private void Awake()
{
avatarRotatorInput = GetComponent<IAvatarRotatorInput>();
}

public void ResetRotation()
{
transform.rotation = Quaternion.identity;
}

private void Update()
{
if (avatarRotatorInput == null || !avatarRotatorInput.IsInputDetected())
{
return;
}

var rotationAmount = avatarRotatorInput.GetRotationAmount();
transform.Rotate(Vector3.up, rotationAmount * Time.deltaTime * speed);
}
}
}
Loading