TouchController.cs 1.5 KB

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