Skip to content

Commit 98e99d6

Browse files
[SDK-685] Feature/avatar preview element (#181)
- added avatar preview prefab - refactor of avatar rotate scsript - added new CameraFocuser script
1 parent 5f855ff commit 98e99d6

26 files changed

+380
-154
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!1 &1740648930196985801
4+
GameObject:
5+
m_ObjectHideFlags: 0
6+
m_CorrespondingSourceObject: {fileID: 0}
7+
m_PrefabInstance: {fileID: 0}
8+
m_PrefabAsset: {fileID: 0}
9+
serializedVersion: 6
10+
m_Component:
11+
- component: {fileID: 1740648930196985805}
12+
- component: {fileID: 1740648930196985800}
13+
- component: {fileID: 3027828850030535667}
14+
m_Layer: 0
15+
m_Name: Avatar Preview Element
16+
m_TagString: Untagged
17+
m_Icon: {fileID: 0}
18+
m_NavMeshLayer: 0
19+
m_StaticEditorFlags: 0
20+
m_IsActive: 1
21+
--- !u!4 &1740648930196985805
22+
Transform:
23+
m_ObjectHideFlags: 0
24+
m_CorrespondingSourceObject: {fileID: 0}
25+
m_PrefabInstance: {fileID: 0}
26+
m_PrefabAsset: {fileID: 0}
27+
m_GameObject: {fileID: 1740648930196985801}
28+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
29+
m_LocalPosition: {x: 0, y: 0, z: 0}
30+
m_LocalScale: {x: 1, y: 1, z: 1}
31+
m_Children:
32+
- {fileID: 3703985022246169223}
33+
m_Father: {fileID: 0}
34+
m_RootOrder: 0
35+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
36+
--- !u!114 &1740648930196985800
37+
MonoBehaviour:
38+
m_ObjectHideFlags: 0
39+
m_CorrespondingSourceObject: {fileID: 0}
40+
m_PrefabInstance: {fileID: 0}
41+
m_PrefabAsset: {fileID: 0}
42+
m_GameObject: {fileID: 1740648930196985801}
43+
m_Enabled: 1
44+
m_EditorHideFlags: 0
45+
m_Script: {fileID: 11500000, guid: 198f21f00ad242b47bee2a7073994bea, type: 3}
46+
m_Name:
47+
m_EditorClassIdentifier:
48+
cameraTransform: {fileID: 0}
49+
faceViewPoint: {x: 0, y: 1.66, z: 0.85}
50+
bodyViewPoint: {x: 0, y: 1.4, z: 2.6}
51+
defaultDuration: 1
52+
--- !u!114 &3027828850030535667
53+
MonoBehaviour:
54+
m_ObjectHideFlags: 0
55+
m_CorrespondingSourceObject: {fileID: 0}
56+
m_PrefabInstance: {fileID: 0}
57+
m_PrefabAsset: {fileID: 0}
58+
m_GameObject: {fileID: 1740648930196985801}
59+
m_Enabled: 1
60+
m_EditorHideFlags: 0
61+
m_Script: {fileID: 11500000, guid: 1ed4881de9835c940954c6b8ab021a85, type: 3}
62+
m_Name:
63+
m_EditorClassIdentifier:
64+
avatarRotator: {fileID: 7370162397791757437}
65+
resetRotation: 1
66+
--- !u!1 &7195514442768515100
67+
GameObject:
68+
m_ObjectHideFlags: 0
69+
m_CorrespondingSourceObject: {fileID: 0}
70+
m_PrefabInstance: {fileID: 0}
71+
m_PrefabAsset: {fileID: 0}
72+
serializedVersion: 6
73+
m_Component:
74+
- component: {fileID: 3703985022246169223}
75+
- component: {fileID: 7370162397791757437}
76+
- component: {fileID: 8765811575219735875}
77+
m_Layer: 0
78+
m_Name: AvatarRotator
79+
m_TagString: Untagged
80+
m_Icon: {fileID: 0}
81+
m_NavMeshLayer: 0
82+
m_StaticEditorFlags: 0
83+
m_IsActive: 1
84+
--- !u!4 &3703985022246169223
85+
Transform:
86+
m_ObjectHideFlags: 0
87+
m_CorrespondingSourceObject: {fileID: 0}
88+
m_PrefabInstance: {fileID: 0}
89+
m_PrefabAsset: {fileID: 0}
90+
m_GameObject: {fileID: 7195514442768515100}
91+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
92+
m_LocalPosition: {x: 0, y: 0, z: 0}
93+
m_LocalScale: {x: 1, y: 1, z: 1}
94+
m_Children: []
95+
m_Father: {fileID: 1740648930196985805}
96+
m_RootOrder: 0
97+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
98+
--- !u!114 &7370162397791757437
99+
MonoBehaviour:
100+
m_ObjectHideFlags: 0
101+
m_CorrespondingSourceObject: {fileID: 0}
102+
m_PrefabInstance: {fileID: 0}
103+
m_PrefabAsset: {fileID: 0}
104+
m_GameObject: {fileID: 7195514442768515100}
105+
m_Enabled: 1
106+
m_EditorHideFlags: 0
107+
m_Script: {fileID: 11500000, guid: 740b98666dec44e5bb949b172ef49d6a, type: 3}
108+
m_Name:
109+
m_EditorClassIdentifier:
110+
speed: 50
111+
--- !u!114 &8765811575219735875
112+
MonoBehaviour:
113+
m_ObjectHideFlags: 0
114+
m_CorrespondingSourceObject: {fileID: 0}
115+
m_PrefabInstance: {fileID: 0}
116+
m_PrefabAsset: {fileID: 0}
117+
m_GameObject: {fileID: 7195514442768515100}
118+
m_Enabled: 1
119+
m_EditorHideFlags: 0
120+
m_Script: {fileID: 11500000, guid: c3cb903a634eb8c48a7050b2566ff30f, type: 3}
121+
m_Name:
122+
m_EditorClassIdentifier:

Runtime/AvatarCreator/Prefabs/Elements/Avatar Preview Element.prefab.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using UnityEngine;
2+
3+
namespace ReadyPlayerMe.AvatarCreator
4+
{
5+
public class AvatarPreviewElement : MonoBehaviour
6+
{
7+
[SerializeField] private AvatarRotator avatarRotator;
8+
[Tooltip("If true the avatar will face directly at the camera"), SerializeField] private bool resetRotationOnPreview = true;
9+
private GameObject avatar;
10+
11+
public void PreviewAvatar(GameObject newAvatar)
12+
{
13+
if (avatar != null)
14+
{
15+
Destroy(avatar);
16+
}
17+
18+
avatar = newAvatar;
19+
avatar.transform.SetParent(avatarRotator.transform);
20+
avatar.transform.localPosition = Vector3.zero;
21+
avatar.transform.localRotation = Quaternion.identity;
22+
if (resetRotationOnPreview)
23+
{
24+
avatarRotator.ResetRotation();
25+
}
26+
}
27+
}
28+
}

Runtime/AvatarCreator/UI/Elements/AvatarPreviewElement.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/AvatarCreator/UI/Elements/GenderSelectElement.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using ReadyPlayerMe.Core;
23
using UnityEngine;
34
using UnityEngine.Events;
@@ -18,33 +19,16 @@ public class GenderSelectElement : MonoBehaviour
1819
[Header("Events")]
1920
public UnityEvent<OutfitGender> OnGenderSelected;
2021

21-
private void Awake()
22+
private void OnEnable()
2223
{
23-
InitializeButtons();
24+
maleButton.onClick.AddListener(MaleButtonClicked);
25+
femaleButton.onClick.AddListener(FemaleButtonClicked);
2426
}
2527

26-
private void InitializeButtons()
28+
private void OnDisable()
2729
{
28-
if (maleButton != null)
29-
{
30-
maleButton.onClick.AddListener(MaleButtonClicked);
31-
}
32-
if (femaleButton != null)
33-
{
34-
femaleButton.onClick.AddListener(FemaleButtonClicked);
35-
}
36-
}
37-
38-
private void OnDestroy()
39-
{
40-
if (maleButton != null)
41-
{
42-
maleButton.onClick.RemoveListener(MaleButtonClicked);
43-
}
44-
if (femaleButton != null)
45-
{
46-
femaleButton.onClick.RemoveListener(FemaleButtonClicked);
47-
}
30+
maleButton.onClick.RemoveListener(MaleButtonClicked);
31+
femaleButton.onClick.RemoveListener(FemaleButtonClicked);
4832
}
4933

5034
private void MaleButtonClicked()
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using ReadyPlayerMe.Core;
2+
using UnityEngine;
3+
4+
namespace ReadyPlayerMe.AvatarCreator
5+
{
6+
public class CameraFocuser : MonoBehaviour
7+
{
8+
private const string TAG = nameof(CameraFocuser);
9+
10+
[SerializeField] private Transform cameraTransform;
11+
[SerializeField] private Vector3 faceViewPoint = new Vector3(0f, 1.66f, 0.85f);
12+
[SerializeField] private Vector3 bodyViewPoint = new Vector3(0f, 1.4f, 2.6f);
13+
[SerializeField] private float defaultDuration = 1f;
14+
15+
private Vector3 targetPosition;
16+
private Vector3 startPosition;
17+
private float transitionTime;
18+
private float duration;
19+
private bool isTransitioning;
20+
21+
private void Awake()
22+
{
23+
if (cameraTransform == null)
24+
{
25+
SDKLogger.LogWarning(TAG, $"Avatar object not set for {gameObject.name}.");
26+
}
27+
}
28+
29+
private void LateUpdate()
30+
{
31+
if (!isTransitioning) return;
32+
transitionTime += Time.deltaTime;
33+
if (transitionTime < duration)
34+
{
35+
var t = Mathf.SmoothStep(0.0f, 1.0f, transitionTime / duration);
36+
cameraTransform.position = Vector3.Lerp(startPosition, targetPosition, t);
37+
}
38+
else
39+
{
40+
cameraTransform.position = targetPosition;
41+
isTransitioning = false;
42+
}
43+
}
44+
45+
public void FocusOnFace()
46+
{
47+
StartTransition(faceViewPoint, defaultDuration);
48+
}
49+
50+
public void FocusOnBody()
51+
{
52+
StartTransition(bodyViewPoint, defaultDuration);
53+
}
54+
55+
public void StopTransition()
56+
{
57+
isTransitioning = false;
58+
}
59+
60+
private void StartTransition(Vector3 newTargetLocalPosition, float transitionDuration)
61+
{
62+
startPosition = cameraTransform.position;
63+
targetPosition = transform.TransformPoint(newTargetLocalPosition);
64+
duration = transitionDuration;
65+
transitionTime = 0f;
66+
isTransitioning = true;
67+
}
68+
69+
private void OnDrawGizmos()
70+
{
71+
Gizmos.color = Color.red;
72+
Gizmos.DrawSphere(transform.position + faceViewPoint, 0.1f);
73+
74+
Gizmos.color = Color.blue;
75+
Gizmos.DrawSphere(transform.position + bodyViewPoint, 0.1f);
76+
}
77+
}
78+
}

Runtime/AvatarCreator/Utils/CameraFocuser.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/AvatarCreator/Utils/Rotation/AvatarMouseRotation.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using UnityEngine;
2+
3+
namespace ReadyPlayerMe.AvatarCreator
4+
{
5+
public class AvatarRotator : MonoBehaviour
6+
{
7+
private const string TAG = nameof(AvatarRotator);
8+
[SerializeField] private float speed = 50;
9+
10+
private IAvatarRotatorInput avatarRotatorInput;
11+
12+
private void Awake()
13+
{
14+
avatarRotatorInput = GetComponent<IAvatarRotatorInput>();
15+
}
16+
17+
public void ResetRotation()
18+
{
19+
transform.rotation = Quaternion.identity;
20+
}
21+
22+
private void Update()
23+
{
24+
if (avatarRotatorInput == null || !avatarRotatorInput.IsInputDetected())
25+
{
26+
return;
27+
}
28+
29+
var rotationAmount = avatarRotatorInput.GetRotationAmount();
30+
transform.Rotate(Vector3.up, rotationAmount * Time.deltaTime * speed);
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)