TouchController.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. using UnityEngine.EventSystems;
  3. using System.Collections;
  4. using System.Linq;
  5. namespace OpenCVForUnityExample
  6. {
  7. public class TouchController : MonoBehaviour
  8. {
  9. public GameObject Cube;
  10. public float Speed = 0.1f;
  11. void Update ()
  12. {
  13. #if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)
  14. //Touch
  15. int touchCount = Input.touchCount;
  16. if (touchCount == 1)
  17. {
  18. Touch t = Input.GetTouch (0);
  19. if (EventSystem.current.IsPointerOverGameObject (t.fingerId))
  20. return;
  21. switch (t.phase)
  22. {
  23. case TouchPhase.Moved:
  24. float xAngle = t.deltaPosition.y * Speed;
  25. float yAngle = -t.deltaPosition.x * Speed;
  26. float zAngle = 0;
  27. Cube.transform.Rotate (xAngle, yAngle, zAngle, Space.World);
  28. break;
  29. }
  30. }
  31. #else
  32. //Mouse
  33. if (Input.GetMouseButton (0)) {
  34. if (EventSystem.current.IsPointerOverGameObject ())
  35. return;
  36. float xAngle = Input.GetAxis ("Mouse Y") * Speed * 80;
  37. float yAngle = -Input.GetAxis ("Mouse X") * Speed * 80;
  38. float zAngle = 0;
  39. Cube.transform.Rotate (xAngle, yAngle, zAngle, Space.World);
  40. }
  41. #endif
  42. }
  43. }
  44. }