using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchInput : MonoBehaviour {
private static TouchInput _instance;
public static TouchInput Instance { get { return _instance; } }
/// <summary>
/// 表面上需要进行操作的物体
/// </summary>
public Transform gameToCtrl;
/// <summary>
/// 物体不被操作时所在Transform
/// </summary>
public Transform parentFrame;
/// <summary>
/// 物体进行X轴旋转
/// </summary>
public Transform aroundX;
/// <summary>
/// 物体进行Y轴进行旋转
/// </summary>
public Transform aroundY;
/// <summary>
/// 物体旋转sudu
/// </summary>
float speedNormal = 0;
/// <summary>
/// 物体放大缩小的Transform
/// </summary>
public Transform parentScale;
private Touch oldTouch1; //上次触摸点1(手指1)
private Touch oldTouch2; //上次触摸点2(手指2)
private float scaleMin = 0.3f;
private float scaleMax = 3f;
private void Awake()
{
if (!_instance)
_instance = this;
}
// public InfoToScreen info;
// Use this for initialization
void Start()
{
speedNormal = 30 * Time.deltaTime;
}
// Update is called once per frame
void Update()
{
if (Manager_Screen_LockReset.Instance)
{
Manager_Screen_LockReset.Instance.UpdateMove();
}
//没有触摸
if (Input.touchCount <= 0)
{
//if (gameToCtrl.parent != parentFrame)
//{
// gameToCtrl.SetParent(parentFrame);
//}
//aroundX.eulerAngles = Vector3.zero;
//aroundY.eulerAngles = Vector3.zero;
return;
}
// info.UpdateInfo(gameToCtrl);
//单点触摸, 水平上下旋转
if (1 == Input.touchCount)
{
Touch touch = Input.GetTouch(0);
Vector2 deltaPos = touch.deltaPosition;
// gameToCtrl.SetParent(aroundY);
if (Manager_Screen_LockReset.Instance)
{
if (Manager_Screen_LockReset.Instance.UseX())//X轴是否可以旋转(可去除)
{
aroundX.Rotate(Vector3.forward * deltaPos.x * speedNormal, Space.World);
}
if (Manager_Screen_LockReset.Instance.UseY())//轴是否可以旋转(可去除)
{
aroundY.Rotate(Vector3.right * deltaPos.y * speedNormal, Space.World);
}
}
else
{
aroundX.Rotate(Vector3.forward * deltaPos.x * speedNormal, Space.World);
aroundY.Rotate(Vector3.right * deltaPos.y * speedNormal, Space.World);
}
}
DoTouche();
}
void DoTouche()
{
//多点触摸, 放大缩小
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//第2点刚开始接触屏幕, 只记录,不做处理
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
// gameToCtrl.SetParent(parentScale);
return;
}
//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两个距离之差,为正表示放大手势, 为负表示缩小手势
float offset = newDistance - oldDistance;
//放大因子, 一个像素按 0.01倍来算(100可调整)
float scaleFactor = offset / 100f;
Vector3 localScale = gameToCtrl.localScale;
Vector3 scale = new Vector3(localScale.x + scaleFactor, localScale.y + scaleFactor, localScale.z + scaleFactor);
//最小缩放到 0.3 倍
if ((scale.x > scaleMin && scale.x < scaleMax) && scale.y > scaleMin && scale.z > scaleMin)
{
gameToCtrl.localScale = scale;
}
//记住最新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
public void ResetEuler()
{
aroundX.localEulerAngles = Vector3.zero;
aroundY.localEulerAngles = Vector3.zero;
gameToCtrl.transform.GetChild(0).localPosition = Vector3.zero;
}
//public void TuenLeft()
//{
// moveXY.Translate(moveXY.up * Time.deltaTime * moveSpeed);
//}
//public void TurnRight()
//{
// moveXY.Translate(-1 * moveXY.up * Time.deltaTime * moveSpeed);
//}
void DoTurnAround()
{
//if (Input.GetMouseButtonDown(0))
//{
// gameToCtrl.SetParent(aroundY);
// isCanCtrl = true;
//}
//if (Input.GetMouseButton(0) && isCanCtrl)
//{
// speedY = Input.GetAxis("Mouse X") * Time.deltaTime * speedNormal * (-1);
// speedX = Input.GetAxis("Mouse Y") * Time.deltaTime * speedNormal;
// aroundX.Rotate(new Vector3(speedX, 0, 0));
// aroundY.Rotate(new Vector3(0, speedY, 0));
//}
}