using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class SetRoaAndScal : MonoBehaviour
{
public Transform TargetTransform;//要操作的物体
public bool flag_Roable = true;//自动旋转标志
public float xSpeed = 50f;
public float ySpeed = 50f;
//public float distance = 30f;
private float x = 0f;
private float y = 0f;
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private System.DateTime oldTime;
private System.DateTime nowTime;
void Update()
{
nowTime = System.DateTime.Now;
System.TimeSpan ts1 = new System.TimeSpan(oldTime.Ticks);
System.TimeSpan ts2 = new System.TimeSpan(nowTime.Ticks);
System.TimeSpan ts = ts2.Subtract(ts1).Duration();
if (ts.Seconds > 8 && !Input.anyKey)
{
flag_Roable = true;
oldTime = System.DateTime.Now;
}
if (Input.anyKey)
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
x = Input.GetAxis("Mouse X") * xSpeed;
y = Input.GetAxis("Mouse Y") * ySpeed;
TargetTransform.Rotate(Vector3.up * -x * Time.deltaTime, Space.Self);
TargetTransform.Rotate(Vector3.right * -y * Time.deltaTime, Space.Self);
}
}
单点触摸, 水平上下移动
//if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
//{
// //EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)为true表示点到了UI
// if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
// {
// var deltaposition = Input.GetTouch(0).deltaPosition;
// TargetTransform.Translate(-deltaposition.x * 0.1f, 0f, -deltaposition.y * 0.1f);
// }
//}
if (Input.touchCount > 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector2 tempPosition1 = Input.GetTouch(0).position;
Vector2 tempPosition2 = Input.GetTouch(1).position;
if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
{
float oldScale = TargetTransform.localScale.x;
float newScale = oldScale * 1.025f;
TargetTransform.localScale = new Vector3(newScale, newScale, newScale);
}
else
{
float oldScale = TargetTransform.localScale.x;
float newScale = oldScale / 1.025f;
TargetTransform.localScale = new Vector3(newScale, newScale, newScale);
}
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
}
}
}
}
bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
var leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
var leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if (leng1 < leng2)
{
//放大手势
return true;
}
else
{
//缩小手势
return false;
}
}
}
08-18
2750