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

自己写的一套用于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();
        }
    }
}


  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值