您的位置:首页 > 移动开发 > Unity3D

Unity移动端手势操作——旋转3D物体

2016-10-14 15:14 771 查看
自己写的一套用于Unity移动端手势操作的判断,主要有单指移动3D物体、单指旋转3D物体、双指缩放3D物体,这里首先分开介绍单指旋转3D物体,如下所示:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;

public class RotateControl : GestureControl
{

protected override void InputCheck()
{
#region  单点触发旋转(真实模型旋转)
if (Input.touchCount == 1)
{
//触摸为移动类型
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
status = 1;
try
{
StartCoroutine(CustomOnMouseDown());
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}

}
#endregion

#region   键盘A、D、W、S模拟旋转(真实模型旋转)
if (Input.GetKeyDown(KeyCode.A))
{
transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.World);
}

if (Input.GetKeyDown(KeyCode.D))
{
transform.Rotate(Vector3.up, -45 * Time.deltaTime, Space.World);
}

if (Input.GetKeyDown(KeyCode.W))
{
transform.Rotate(Vector3.left, 45 * Time.deltaTime, Space.World);
}

if (Input.GetKeyDown(KeyCode.S))
{
transform.Rotate(Vector3.left, -45 * Time.deltaTime, Space.World);
}
#endregion
}

IEnumerator CustomOnMouseDown()
{
//当检测到一直触碰时,会不断循环运行
while (Input.GetMouseButton(0))
{
//判断是否点击在UI上
#if UNITY_ANDROID || UNITY_IPHONE
if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
if (EventSystem.current.IsPointerOverGameObject())
#endif
{
Debug.Log("当前点击在UI上");
}
else
{
float XX = Input.GetAxis("Mouse X");
float YY = Input.GetAxis("Mouse Y");
#region
//判断左右滑动的距离与上下滑动距离大小
if (Mathf.Abs(XX) >= Mathf.Abs(YY))
{
//单指向左滑动情况
if (XX < 0)
{
transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.World);
}
//单指向右滑动情况
if (XX > 0)
{
transform.Rotate(-Vector3.up, 45 * Time.deltaTime, Space.World);
}
}
else
{
//单指向下滑动情况
if (YY < 0)
{
transform.Rotate(Vector3.left, 45 * Time.deltaTime, Space.World);
}
//单指向上滑动情况
if (YY > 0)
{
transform.Rotate(-Vector3.left, 45 * Time.deltaTime, Space.World);
}
}
#endregion
}
yield return new WaitForFixedUpdate();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  手指 Unity 旋转