当前位置:   article > 正文

Unity移动端手势操作——旋转3D物体_cocos 手指拖动3d旋转

cocos 手指拖动3d旋转

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

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. public class RotateControl : GestureControl
  6. {
  7. protected override void InputCheck()
  8. {
  9. #region 单点触发旋转(真实模型旋转)
  10. if (Input.touchCount == 1)
  11. {
  12. //触摸为移动类型
  13. if (Input.GetTouch(0).phase == TouchPhase.Moved)
  14. {
  15. status = 1;
  16. try
  17. {
  18. StartCoroutine(CustomOnMouseDown());
  19. }
  20. catch (Exception e)
  21. {
  22. Debug.Log(e.ToString());
  23. }
  24. }
  25. }
  26. #endregion
  27. #region 键盘A、D、W、S模拟旋转(真实模型旋转)
  28. if (Input.GetKeyDown(KeyCode.A))
  29. {
  30. transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.World);
  31. }
  32. if (Input.GetKeyDown(KeyCode.D))
  33. {
  34. transform.Rotate(Vector3.up, -45 * Time.deltaTime, Space.World);
  35. }
  36. if (Input.GetKeyDown(KeyCode.W))
  37. {
  38. transform.Rotate(Vector3.left, 45 * Time.deltaTime, Space.World);
  39. }
  40. if (Input.GetKeyDown(KeyCode.S))
  41. {
  42. transform.Rotate(Vector3.left, -45 * Time.deltaTime, Space.World);
  43. }
  44. #endregion
  45. }
  46. IEnumerator CustomOnMouseDown()
  47. {
  48. //当检测到一直触碰时,会不断循环运行
  49. while (Input.GetMouseButton(0))
  50. {
  51. //判断是否点击在UI上
  52. #if UNITY_ANDROID || UNITY_IPHONE
  53. if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
  54. #else
  55. if (EventSystem.current.IsPointerOverGameObject())
  56. #endif
  57. {
  58. Debug.Log("当前点击在UI上");
  59. }
  60. else
  61. {
  62. float XX = Input.GetAxis("Mouse X");
  63. float YY = Input.GetAxis("Mouse Y");
  64. #region
  65. //判断左右滑动的距离与上下滑动距离大小
  66. if (Mathf.Abs(XX) >= Mathf.Abs(YY))
  67. {
  68. //单指向左滑动情况
  69. if (XX < 0)
  70. {
  71. transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.World);
  72. }
  73. //单指向右滑动情况
  74. if (XX > 0)
  75. {
  76. transform.Rotate(-Vector3.up, 45 * Time.deltaTime, Space.World);
  77. }
  78. }
  79. else
  80. {
  81. //单指向下滑动情况
  82. if (YY < 0)
  83. {
  84. transform.Rotate(Vector3.left, 45 * Time.deltaTime, Space.World);
  85. }
  86. //单指向上滑动情况
  87. if (YY > 0)
  88. {
  89. transform.Rotate(-Vector3.left, 45 * Time.deltaTime, Space.World);
  90. }
  91. }
  92. #endregion
  93. }
  94. yield return new WaitForFixedUpdate();
  95. }
  96. }
  97. }


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号