Joystick.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. namespace UnityStandardAssets.CrossPlatformInput
  5. {
  6. public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
  7. {
  8. public enum AxisOption
  9. {
  10. // Options for which axes to use
  11. Both, // Use both
  12. OnlyHorizontal, // Only horizontal
  13. OnlyVertical // Only vertical
  14. }
  15. public int MovementRange = 100;
  16. public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
  17. public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
  18. public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
  19. Vector3 m_StartPos;
  20. bool m_UseX; // Toggle for using the x axis
  21. bool m_UseY; // Toggle for using the Y axis
  22. CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
  23. CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input
  24. void OnEnable()
  25. {
  26. CreateVirtualAxes();
  27. }
  28. void Start()
  29. {
  30. m_StartPos = transform.position;
  31. }
  32. void UpdateVirtualAxes(Vector3 value)
  33. {
  34. var delta = m_StartPos - value;
  35. delta.y = -delta.y;
  36. delta /= MovementRange;
  37. if (m_UseX)
  38. {
  39. m_HorizontalVirtualAxis.Update(-delta.x);
  40. }
  41. if (m_UseY)
  42. {
  43. m_VerticalVirtualAxis.Update(delta.y);
  44. }
  45. }
  46. void CreateVirtualAxes()
  47. {
  48. // set axes to use
  49. m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
  50. m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
  51. // create new axes based on axes to use
  52. if (m_UseX)
  53. {
  54. m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
  55. CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
  56. }
  57. if (m_UseY)
  58. {
  59. m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
  60. CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
  61. }
  62. }
  63. public void OnDrag(PointerEventData data)
  64. {
  65. Vector3 newPos = Vector3.zero;
  66. if (m_UseX)
  67. {
  68. int delta = (int)(data.position.x - m_StartPos.x);
  69. delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
  70. newPos.x = delta;
  71. }
  72. if (m_UseY)
  73. {
  74. int delta = (int)(data.position.y - m_StartPos.y);
  75. delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
  76. newPos.y = delta;
  77. }
  78. transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
  79. UpdateVirtualAxes(transform.position);
  80. }
  81. public void OnPointerUp(PointerEventData data)
  82. {
  83. transform.position = m_StartPos;
  84. UpdateVirtualAxes(m_StartPos);
  85. }
  86. public void OnPointerDown(PointerEventData data) { }
  87. void OnDisable()
  88. {
  89. // remove the joysticks from the cross platform input
  90. if (m_UseX)
  91. {
  92. m_HorizontalVirtualAxis.Remove();
  93. }
  94. if (m_UseY)
  95. {
  96. m_VerticalVirtualAxis.Remove();
  97. }
  98. }
  99. }
  100. }