98 lines
3.5 KiB
C#
98 lines
3.5 KiB
C#
using DG.Tweening;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TouchScript.Gestures;
|
|
using TouchScript.Gestures.TransformGestures;
|
|
using TouchScript.Layers;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ObjectRotate : MonoBehaviour
|
|
{
|
|
public TransformGesture OneFingerMoveGesture;
|
|
public TransformGesture ManipulationGesture;
|
|
public GameObject obj;
|
|
|
|
public float minScale;
|
|
public float maxScale;
|
|
public float minXRotation = -15f; // x轴的最小旋转角度
|
|
public float maxXRotation = 15f; // x轴的最大旋转角度
|
|
public float minZRotation = -15f; // z轴的最小旋转角度
|
|
public float maxZRotation = 15f; // z轴的最大旋转角度
|
|
public float rotateSpeed;
|
|
// Start is called before the first frame update
|
|
void OnEnable()
|
|
{
|
|
OneFingerMoveGesture.Transformed += oneFingerTransformHandler;
|
|
ManipulationGesture.Transformed += manipulationTransformedHandler;
|
|
|
|
}
|
|
void OnDisable()
|
|
{
|
|
OneFingerMoveGesture.Transformed -= oneFingerTransformHandler;
|
|
ManipulationGesture.Transformed -= manipulationTransformedHandler;
|
|
|
|
}
|
|
|
|
private void oneFingerTransformHandler(object sender, System.EventArgs e)
|
|
{
|
|
Debug.Log(OneFingerMoveGesture.DeltaPosition.x + "___" + OneFingerMoveGesture.DeltaPosition.y);
|
|
float y = OneFingerMoveGesture.DeltaPosition.x;// + obj.transform.rotation.y;
|
|
float x = OneFingerMoveGesture.DeltaPosition.y;//+ obj.transform.rotation.x;
|
|
obj.transform.eulerAngles += new Vector3(0, -y * rotateSpeed, 0);
|
|
//obj.transform.Rotate(Vector3.up, -y * rotateSpeed, Space.World);
|
|
obj.transform.Rotate(Vector3.right, x * rotateSpeed, Space.World);
|
|
|
|
// 获取当前物体的旋转
|
|
Vector3 currentRotation = obj.transform.eulerAngles;
|
|
|
|
// 将x轴的旋转限制在指定范围内
|
|
currentRotation.x = ClampAngle(currentRotation.x, minXRotation, maxXRotation);
|
|
|
|
// 将z轴的旋转限制在指定范围内
|
|
currentRotation.z = ClampAngle(currentRotation.z, minZRotation, maxZRotation);
|
|
|
|
// 设置限制后的旋转
|
|
obj.transform.eulerAngles = currentRotation;
|
|
}
|
|
|
|
private void manipulationTransformedHandler(object sender, System.EventArgs e)
|
|
{
|
|
Debug.Log(ManipulationGesture.DeltaScale);
|
|
obj.transform.localScale += new Vector3(ManipulationGesture.DeltaScale - 1f, ManipulationGesture.DeltaScale - 1f, ManipulationGesture.DeltaScale - 1f);
|
|
|
|
obj.transform.localScale = new Vector3(Mathf.Clamp(obj.transform.localScale.x, minScale, maxScale), Mathf.Clamp(obj.transform.localScale.y, minScale, maxScale), Mathf.Clamp(obj.transform.localScale.z, minScale, maxScale));
|
|
|
|
}
|
|
|
|
|
|
// 辅助函数,用于将角度限制在指定范围内
|
|
private float ClampAngle(float angle, float min, float max)
|
|
{
|
|
if (angle < 90 || angle > 270) // 0-90和270-360的范围
|
|
{
|
|
if (angle > 180) angle -= 360; // 处理大于180度的情况
|
|
}
|
|
else // 90-270的范围
|
|
{
|
|
if (angle < 180) angle += 360; // 处理小于180度的情况
|
|
}
|
|
return Mathf.Clamp(angle, min, max);
|
|
}
|
|
|
|
public void resetRotate()
|
|
{
|
|
Vector3 a =new Vector3(-1.6f,-0.12f,1);
|
|
obj.transform.DOMove(a, 1f);
|
|
obj.transform.DORotate(Vector3.zero,1f);
|
|
obj.transform.DOScale(Vector3.one, 1f);
|
|
GetComponent<FullscreenLayer>().enabled = false;
|
|
}
|
|
public void goTo()
|
|
{
|
|
obj.transform.DOMove(Vector3.zero, 1f);
|
|
obj.transform.DORotate(Vector3.zero, 1f);
|
|
GetComponent<FullscreenLayer>().enabled = true;
|
|
}
|
|
}
|