TouchPad.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. namespace UnityStandardAssets.CrossPlatformInput
  6. {
  7. [RequireComponent(typeof(Image))]
  8. public class TouchPad : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
  9. {
  10. // Options for which axes to use
  11. public enum AxisOption
  12. {
  13. Both, // Use both
  14. OnlyHorizontal, // Only horizontal
  15. OnlyVertical // Only vertical
  16. }
  17. public enum ControlStyle
  18. {
  19. Absolute, // operates from teh center of the image
  20. Relative, // operates from the center of the initial touch
  21. Swipe, // swipe to touch touch no maintained center
  22. }
  23. public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
  24. public ControlStyle controlStyle = ControlStyle.Absolute; // control style to use
  25. public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
  26. public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
  27. public float Xsensitivity = 1f;
  28. public float Ysensitivity = 1f;
  29. Vector3 m_StartPos;
  30. Vector2 m_PreviousDelta;
  31. Vector3 m_JoytickOutput;
  32. bool m_UseX; // Toggle for using the x axis
  33. bool m_UseY; // Toggle for using the Y axis
  34. CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
  35. CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
  36. bool m_Dragging;
  37. int m_Id = -1;
  38. Vector2 m_PreviousTouchPos; // swipe style control touch
  39. #if !UNITY_EDITOR
  40. private Vector3 m_Center;
  41. private Image m_Image;
  42. #else
  43. Vector3 m_PreviousMouse;
  44. #endif
  45. void OnEnable()
  46. {
  47. CreateVirtualAxes();
  48. }
  49. void Start()
  50. {
  51. #if !UNITY_EDITOR
  52. m_Image = GetComponent<Image>();
  53. m_Center = m_Image.transform.position;
  54. #endif
  55. }
  56. void CreateVirtualAxes()
  57. {
  58. // set axes to use
  59. m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
  60. m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
  61. // create new axes based on axes to use
  62. if (m_UseX)
  63. {
  64. m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
  65. CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
  66. }
  67. if (m_UseY)
  68. {
  69. m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
  70. CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
  71. }
  72. }
  73. void UpdateVirtualAxes(Vector3 value)
  74. {
  75. value = value.normalized;
  76. if (m_UseX)
  77. {
  78. m_HorizontalVirtualAxis.Update(value.x);
  79. }
  80. if (m_UseY)
  81. {
  82. m_VerticalVirtualAxis.Update(value.y);
  83. }
  84. }
  85. public void OnPointerDown(PointerEventData data)
  86. {
  87. m_Dragging = true;
  88. m_Id = data.pointerId;
  89. #if !UNITY_EDITOR
  90. if (controlStyle != ControlStyle.Absolute )
  91. m_Center = data.position;
  92. #endif
  93. }
  94. void Update()
  95. {
  96. if (!m_Dragging)
  97. {
  98. return;
  99. }
  100. if (Input.touchCount >= m_Id + 1 && m_Id != -1)
  101. {
  102. #if !UNITY_EDITOR
  103. if (controlStyle == ControlStyle.Swipe)
  104. {
  105. m_Center = m_PreviousTouchPos;
  106. m_PreviousTouchPos = Input.touches[m_Id].position;
  107. }
  108. Vector2 pointerDelta = new Vector2(Input.touches[m_Id].position.x - m_Center.x , Input.touches[m_Id].position.y - m_Center.y).normalized;
  109. pointerDelta.x *= Xsensitivity;
  110. pointerDelta.y *= Ysensitivity;
  111. #else
  112. Vector2 pointerDelta;
  113. pointerDelta.x = Input.mousePosition.x - m_PreviousMouse.x;
  114. pointerDelta.y = Input.mousePosition.y - m_PreviousMouse.y;
  115. m_PreviousMouse = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
  116. #endif
  117. UpdateVirtualAxes(new Vector3(pointerDelta.x, pointerDelta.y, 0));
  118. }
  119. }
  120. public void OnPointerUp(PointerEventData data)
  121. {
  122. m_Dragging = false;
  123. m_Id = -1;
  124. UpdateVirtualAxes(Vector3.zero);
  125. }
  126. void OnDisable()
  127. {
  128. if (CrossPlatformInputManager.AxisExists(horizontalAxisName))
  129. CrossPlatformInputManager.UnRegisterVirtualAxis(horizontalAxisName);
  130. if (CrossPlatformInputManager.AxisExists(verticalAxisName))
  131. CrossPlatformInputManager.UnRegisterVirtualAxis(verticalAxisName);
  132. }
  133. }
  134. }