官方的Touch相关数据:
Unity手册的Touch
实现代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraControl : MonoBehaviour
{
//相机移动速度
public float m_moveSpeed = 0.1f;
//目标物体
public GameObject building;
public Text textPos;
// 记录手指触屏的位置
private Vector2 m_screenpos = new Vector2();
private Vector3 oldPosition;
private Quaternion oldRotation;
public HybridTest hybridTest;
// Use this for initialization
void Start()
{
//记录开始摄像机的Position
oldPosition = Camera.main.transform.position;
oldRotation = Camera.main.transform.rotation;
Debug.Log("old pos is:" + oldPosition);
}
// Update is called once per frame
void Update()
{
//————————input会监听整个屏幕,就算unity的layout只占一小部分——————————
if (Input.touchCount <= 0)
{
//ResetCameraPosition();
}
else
{
textPos.text = "Pos is:" + Input.touches[0].position + "/" + CheckTouchValid(Input.touches[0].position);
}
// 1个手指触摸屏幕
if (Input.touchCount == 1)
{
if (!CheckTouchValid(Input.touches[0].position))
{
return ;
}
if (Input.touches[0].phase == TouchPhase.Began)
{
// 记录手指触屏的位置
//m_screenpos = Input.touches[0].position;
//Debug.Log("touch pos is:" + m_screenpos);
}
// 手指移动
else if (Input.touches[0].phase == TouchPhase.Moved )
{
// 移动摄像机
//Camera.main.transform.Translate(new Vector3(-Input.touches[0].deltaPosition.x * Time.deltaTime * m_moveSpeed, -Input.touches[0].deltaPosition.y * Time.deltaTime * m_moveSpeed, 0));
//让相机围绕目标Y轴旋转
Camera.main.transform.RotateAround(building.transform.position, building.transform.up, Input.touches[0].deltaPosition.x * Time.deltaTime * m_moveSpeed);
Camera.main.transform.RotateAround(building.transform.position, Camera.main.transform.right, - Input.touches[0].deltaPosition.y * Time.deltaTime * m_moveSpeed);
}
}
else if (Input.touchCount > 1 )
{
if(!CheckTouchValid(Input.touches[0].position) || !CheckTouchValid(Input.touches[1].position))
{
return;
}
// 记录两个手指的位置
Vector2 finger1 = new Vector2();
Vector2 finger2 = new Vector2();
// 记录两个手指的移动
Vector2 mov1 = new Vector2();
Vector2 mov2 = new Vector2();
for (int i = 0; i < 2; i++)
{
Touch touch = Input.touches[i];
if (touch.phase == TouchPhase.Ended)
break;
if (touch.phase == TouchPhase.Moved)
{
float mov = 0;
if (i == 0)
{
finger1 = touch.position;
mov1 = touch.deltaPosition;
}
else
{
finger2 = touch.position;
mov2 = touch.deltaPosition;
if (finger1.x > finger2.x)
{
mov = mov1.x;
}
else
{
mov = mov2.x;
}
if (finger1.y > finger2.y)
{
mov += mov1.y;
}
else
{
mov += mov2.y;
}
Camera.main.transform.Translate(0, 0, mov * Time.deltaTime * 0.2f);
}
}
}
//控制物体始终在屏幕中
//Camera.main.transform.position = new Vector3(Mathf.Clamp(transform.position.x, -4f, 5.5f), Mathf.Clamp(transform.position.y, 0.5f, 10f), Mathf.Clamp(transform.position.z, -0.9f, 4.6f));
}
}
//通过按钮让物体回到最初的位置
public void ResetCameraPosition()
{
Camera.main.transform.position = oldPosition;
Camera.main.transform.rotation = oldRotation;
}
public bool CheckTouchValid(Vector2 position)
{
if(position.y < hybridTest.layoutHeight && position.y > 0)
{
return true;
}
return false;
}
}
最下方的函数用于Android开发中识别交互范围,如果不涉及的话可以删掉。
主要原理也就是拿到Touch的position,进行简单的逻辑判断然后进行相机位置的修改。
在Asset store中有个TouchScripts可以免费使用,效果也很不错。