Skip to content

Commit ede5544

Browse files
committed
feat: decouple input logic from rotate
1 parent c986eac commit ede5544

File tree

8 files changed

+91
-1
lines changed

8 files changed

+91
-1
lines changed

Runtime/AvatarCreator/Utils/Rotation.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using UnityEngine;
2+
using UnityEngine.EventSystems;
3+
4+
namespace ReadyPlayerMe.AvatarCreator
5+
{
6+
public class AvatarMouseRotation: MonoBehaviour
7+
{
8+
private const int MOUSE_BUTTON_INDEX = 0;
9+
10+
[SerializeField] private float speed = 50;
11+
12+
private IMouseInput mouseInput;
13+
private float lastPosX;
14+
private bool rotate;
15+
16+
private void Awake()
17+
{
18+
mouseInput = GetComponent<IMouseInput>();
19+
}
20+
21+
private void Update()
22+
{
23+
if (EventSystem.current.IsPointerOverGameObject() && !rotate)
24+
{
25+
return;
26+
}
27+
28+
if (mouseInput.GetButtonDown(MOUSE_BUTTON_INDEX))
29+
{
30+
lastPosX = Input.mousePosition.x;
31+
rotate = true;
32+
}
33+
34+
if (mouseInput.GetButtonUp(MOUSE_BUTTON_INDEX))
35+
{
36+
rotate = false;
37+
}
38+
39+
if (mouseInput.GetButtonPressed(MOUSE_BUTTON_INDEX))
40+
{
41+
transform.Rotate(Vector3.up, (lastPosX - Input.mousePosition.x) * (Time.deltaTime * speed));
42+
lastPosX = Input.mousePosition.x;
43+
}
44+
}
45+
}
46+
}

Runtime/AvatarCreator/Utils/Rotation/AvatarMouseRotation.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ReadyPlayerMe.AvatarCreator
2+
{
3+
public interface IMouseInput
4+
{
5+
bool GetButtonDown(int index);
6+
bool GetButtonUp(int index);
7+
bool GetButtonPressed(int index);
8+
}
9+
}

Runtime/AvatarCreator/Utils/Rotation/IMouseInput.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using UnityEngine;
2+
3+
namespace ReadyPlayerMe.AvatarCreator
4+
{
5+
public class MouseInput: MonoBehaviour, IMouseInput
6+
{
7+
public bool GetButtonDown(int index)
8+
{
9+
return Input.GetMouseButtonDown(index);
10+
}
11+
12+
public bool GetButtonUp(int index)
13+
{
14+
return Input.GetMouseButtonUp(index);
15+
}
16+
17+
public bool GetButtonPressed(int index)
18+
{
19+
return Input.GetMouseButton(index);
20+
}
21+
}
22+
}

Runtime/AvatarCreator/Utils/Rotation/MouseInput.cs.meta

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

Samples~/AvatarCreatorSamples/Scripts/UI/SelectionScreens/AvatarCreatorSelection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ private void ProcessAvatar(GameObject avatar)
302302
avatar.GetComponent<Animator>().runtimeAnimatorController = animator;
303303
}
304304
avatar.transform.rotation = lastRotation;
305-
avatar.AddComponent<RotateAvatar>();
305+
avatar.AddComponent<MouseInput>();
306+
avatar.AddComponent<AvatarMouseRotation>();
306307
}
307308

308309
public void Dispose()

0 commit comments

Comments
 (0)