115 lines
3.6 KiB
C#
115 lines
3.6 KiB
C#
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.EventSystems;
|
|||
|
|
|||
|
namespace HK.FUJIFILM
|
|||
|
{
|
|||
|
public class ModelRotateHitbox : MonoBehaviour
|
|||
|
{
|
|||
|
private float _sensitivity;
|
|||
|
private bool _isRotating;
|
|||
|
private Vector3 _rotation;
|
|||
|
|
|||
|
private Vector2 _touchReference;
|
|||
|
private Vector2 _touchOffset;
|
|||
|
|
|||
|
[HideInInspector] public Transform rotateTransform;
|
|||
|
[HideInInspector] public Transform pivotTransform;
|
|||
|
[SerializeField] public MugActualModel model;
|
|||
|
|
|||
|
void Start()
|
|||
|
{
|
|||
|
_sensitivity = 0.2f;
|
|||
|
_rotation = Vector3.zero;
|
|||
|
// if (GetComponent<NormalThreedeeProduct>() != null)
|
|||
|
// {
|
|||
|
// rotateTransform = GetComponent<NormalThreedeeProduct>().spawnedModel.modelMeshes;
|
|||
|
// pivotTransform = GetComponent<NormalThreedeeProduct>().spawnedModel.pivot;
|
|||
|
// }
|
|||
|
// else if (GetComponent<ThreeDeeStickerProduct>() != null)
|
|||
|
// {
|
|||
|
// rotateTransform = GetComponent<ThreeDeeStickerProduct>().spawnedModel.modelMeshes;
|
|||
|
// pivotTransform = GetComponent<ThreeDeeStickerProduct>().spawnedModel.pivot;
|
|||
|
// }
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
SetData(model.modelMeshes, model.pivot);
|
|||
|
}
|
|||
|
|
|||
|
private void SetData(Transform rotateTransform, Transform pivotTransform)
|
|||
|
{
|
|||
|
this.rotateTransform = rotateTransform;
|
|||
|
this.pivotTransform = pivotTransform;
|
|||
|
}
|
|||
|
|
|||
|
public float sens = 15f;
|
|||
|
|
|||
|
void Update()
|
|||
|
{
|
|||
|
if (rotateTransform == null || pivotTransform == null)
|
|||
|
return;
|
|||
|
|
|||
|
if (Input.touchCount > 0)
|
|||
|
{
|
|||
|
Touch touch = Input.GetTouch(0);
|
|||
|
if (touch.phase == TouchPhase.Began && _isRotating)
|
|||
|
{
|
|||
|
_touchReference = touch.position;
|
|||
|
}
|
|||
|
|
|||
|
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
|
|||
|
{
|
|||
|
// rotating flag
|
|||
|
_isRotating = false;
|
|||
|
}
|
|||
|
else if (touch.phase == TouchPhase.Moved)
|
|||
|
{
|
|||
|
if (_isRotating)
|
|||
|
{
|
|||
|
// offset
|
|||
|
_touchOffset = (touch.position - _touchReference);
|
|||
|
|
|||
|
|
|||
|
rotateTransform.Rotate(new Vector3(_touchOffset.y * sens * Time.deltaTime, 0, 0));
|
|||
|
pivotTransform.Rotate(new Vector3(0, -_touchOffset.x * sens * Time.deltaTime, 0));
|
|||
|
|
|||
|
|
|||
|
// store mouse
|
|||
|
_touchReference = touch.position;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (Input.GetMouseButtonUp(0) && _isRotating)
|
|||
|
_isRotating = false;
|
|||
|
|
|||
|
if (_isRotating)
|
|||
|
{
|
|||
|
_mouseOffset = (Input.mousePosition - _mouseReference);
|
|||
|
|
|||
|
|
|||
|
rotateTransform.Rotate(new Vector3(_mouseOffset.y * sens * Time.deltaTime, 0, 0));
|
|||
|
pivotTransform.Rotate(new Vector3(0, -_mouseOffset.x * sens * Time.deltaTime, 0));
|
|||
|
|
|||
|
|
|||
|
_mouseReference = Input.mousePosition;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private Vector3 _mouseReference;
|
|||
|
private Vector3 _mouseOffset;
|
|||
|
|
|||
|
|
|||
|
public void OnHitBoxTouched(BaseEventData eventData)
|
|||
|
{
|
|||
|
if (eventData is PointerEventData pointerData)
|
|||
|
{
|
|||
|
_isRotating = true;
|
|||
|
_mouseReference = Input.mousePosition;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|