| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.EventSystems; |
| | 5 | |
|
| | 6 | | public class PreviewCameraRotation : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler |
| | 7 | | { |
| | 8 | | public event System.Action<float> OnHorizontalRotation; |
| | 9 | |
|
| 47 | 10 | | public float rotationFactor = -15f; |
| | 11 | |
|
| 47 | 12 | | public float slowDownTime = 0.5f; |
| | 13 | |
|
| | 14 | | private float currentHorizontalRotationVelocity = 0f; |
| | 15 | |
|
| | 16 | | private float slowDownVelocity; |
| | 17 | |
|
| | 18 | | private Coroutine slowDownCoroutine; |
| | 19 | |
|
| | 20 | | private float timer; |
| | 21 | |
|
| 0 | 22 | | public void OnBeginDrag(PointerEventData eventData) { AudioScriptableObjects.buttonClick.Play(true); } |
| | 23 | |
|
| | 24 | | public void OnDrag(PointerEventData eventData) |
| | 25 | | { |
| 0 | 26 | | if (slowDownCoroutine != null) |
| | 27 | | { |
| 0 | 28 | | StopCoroutine(slowDownCoroutine); |
| 0 | 29 | | slowDownCoroutine = null; |
| | 30 | | } |
| | 31 | |
|
| 0 | 32 | | currentHorizontalRotationVelocity = rotationFactor * eventData.delta.x; |
| 0 | 33 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public void OnEndDrag(PointerEventData eventData) |
| | 37 | | { |
| 0 | 38 | | timer = slowDownTime; |
| 0 | 39 | | slowDownVelocity = currentHorizontalRotationVelocity; |
| | 40 | |
|
| 0 | 41 | | if (slowDownCoroutine == null) |
| | 42 | | { |
| 0 | 43 | | slowDownCoroutine = StartCoroutine(SlowDown()); |
| | 44 | | } |
| | 45 | |
|
| 0 | 46 | | AudioScriptableObjects.buttonRelease.Play(true); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | private IEnumerator SlowDown() |
| | 50 | | { |
| 0 | 51 | | float inverseTimer = 1f / slowDownTime; |
| | 52 | |
|
| 0 | 53 | | while (timer > 0) |
| | 54 | | { |
| 0 | 55 | | timer -= Time.deltaTime; |
| 0 | 56 | | currentHorizontalRotationVelocity = Mathf.Lerp(slowDownVelocity, 0, 1 - (timer * inverseTimer)); |
| 0 | 57 | | OnHorizontalRotation?.Invoke(currentHorizontalRotationVelocity); |
| | 58 | |
|
| 0 | 59 | | yield return null; |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | | } |