最近在做一个展示类的应用,因为要投放到移动设备上,所以首要的是要对触控进行识别,但是本身包体不大就不想用触摸插件,于是就都自己实现了。接下来就把触摸的主要实现剥离出来看看。
点击
if (Input.touchCount ==1 && Input.GetTouch(0).phase == TouchPhase.Began)
{
start = Input.GetTouch(0).position;
//判断是不是触摸到了物体
Ray ray = Camera.main.ScreenPointToRay(start);
RaycastHit hit;
int layer = LayerMask.GetMask("Models");
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layer))
{
//do sth...
}
}
移动
private void FixedUpdate(){
...
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
deltaPos = Input.GetTouch(0).deltaPosition;
float min = 966, max = 3202;
//移动name UI
name = GameObject.Find("names").transform;
if (CompareRange(name.transform.position.y, min, max))
{
name.Translate(0, deltaPos.y, 0);
}
name.transform.position = new Vector3(name.transform.position.x, Mathf.Clamp(name.transform.position.y, min, max), name.transform.position.z);
}
...
}
private bool CompareRange(float value, float min, float max)
{
if (value <= max && value >= min)
return true;
else
return false;
}
旋转
if (Input.touchCount == 1)
{
//旋转
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
//Debug.Log("---------------模型旋转-----------------");
float deltaX = Input.GetTouch(0).deltaPosition.x;
CurrentShowModel.Rotate(0, -deltaX * 0.2f, 0);
}
}
双指缩放
if (Input.touchCount == 2)
{
// 记录当前触摸位置
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
//记录前一帧触摸位置
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// 记录两指间长度
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// 记录缩放长度
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
//缩放
float scaleFactor = -deltaMagnitudeDiff / 100f;
Vector3 localScale = CurrentShowModel.transform.localScale;
Vector3 scale = new Vector3(localScale.x + scaleFactor,
localScale.y + scaleFactor,
localScale.z + scaleFactor);
CurrentShowModel.localScale = scale;
CurrentShowModel.localScale = new Vector3(Mathf.Clamp(CurrentShowModel.localScale.x, 1, 5), Mathf.Clamp(CurrentShowModel.localScale.y, 1, 5), Mathf.Clamp(CurrentShowModel.localScale.z, 1, 5));
}
简单应用
循环展示3D物体
void DetectFinger()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
start = Input.GetTouch(0).position;
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
deltaPos = Input.GetTouch(0).deltaPosition;
//记录移动标志
moved = true;
}
else if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && moved != false && startMove != true)
{
startMove = true;
moved = false;
MoveItems(deltaPos.x);
}
}
private void MoveItems(float dir)
{
startTime = Time.time;
if (dir < 0)
{
StopCoroutine(ForwardMove());
StartCoroutine(ForwardMove());
}
else if (dir > 0)
{
StopCoroutine(AfterwardMove());
StartCoroutine(AfterwardMove());
}
else
{
if (start.x > 500)
{
StopCoroutine(AfterwardMove());
StartCoroutine(AfterwardMove());
}
else
{
StopCoroutine(ForwardMove());
StartCoroutine(ForwardMove());
}
}
}
IEnumerator AfterwardMove()
{
//互换位置
while (childs[3].position.x < pos[1].x)
{
for (int i = 1; i < 4; i++)
{
//公式: s = v * t
//计算距离
float Distance = Vector3.Distance(posrecord[i], posrecord[i + 1]);
//计算耗时
float moveTime = Time.time - startTime;
//计算偏移量
float coveredDistance = moveTime * modelMoveSpeed;
//计算占距比
float currentPos = coveredDistance / Distance;
//移动物体
childs[i].position = Vector3.Lerp(posrecord[i], posrecord[(i + 1) % 5], currentPos);
}
yield return null;
}
//更换索引
AfterWardChange();
startMove = false;
}
IEnumerator ForwardMove()
{
//互换位置
while (childs[1].position.x > pos[0].x)
{
for (int i = 3; i > 0; i--) //要移动的就三个物体
{
float Distance = Vector3.Distance(posrecord[i], posrecord[i - 1]);
float moveTime = Time.time - startTime;
float coveredDistance = moveTime * modelMoveSpeed;
float currentPos = coveredDistance / Distance;
childs[i].position = Vector3.Lerp(posrecord[i], posrecord[i - 1], currentPos);
}
yield return null;
}
//更换索引
ForWardChange();
startMove = false;
}
private void AfterWardChange()
{
//后移位置
childs[3].position = pos[2];
Transform temp = childs[3];
childs[3] = childs[2];
childs[2] = childs[1];
childs[1] = temp;
}
private void ForWardChange()
{
childs[1].position = pos[3];
Transform temp = childs[1];
childs[1] = childs[2];
childs[2] = childs[3];
childs[3] = temp;
}
生成圆形展示队列
void Start()
{
count = 360 / changeAngle;
for (int i = 0; i < count; i++)
{
Vector3 center = transform.position;
GameObject cube = Instantiate(models[i]);
//计算弧度
float hudu = 2 * Mathf.PI * r * angle / 360;
//计算角度
float degree = hudu / r;
//圆上任意点坐标公式:P(x,y) = (a+r*cos,b+r*sin)
float xx = center.x + r * Mathf.Cos(degree);
float yy = center.z + r * Mathf.Sin(degree);
cube.transform.position = new Vector3(xx, center.y, yy);
cube.transform.LookAt(center);
angle += changeAngle;
cube.transform.Rotate(0, 180, 0);
cube.transform.SetParent(transform);
}
}