joystick.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. public class joystick : MonoBehaviour
  6. {
  7. public bool touchDown;
  8. public bool autoConfigMaxDist;
  9. public float maxDist;
  10. public Vector2 input;
  11. public Vector2 deltaPos;
  12. public Vector2 startPos;
  13. public Vector2 endPos;
  14. public RectTransform joyBG;
  15. public RectTransform joyStick;
  16. public static Vector2 Input => instance.input;
  17. public static joystick instance;
  18. void Awake(){
  19. instance = this;
  20. }
  21. void Update()
  22. {/*
  23. if (autoConfigMaxDist)
  24. {
  25. maxDist = joyBG.sizeDelta.x / 2;
  26. }
  27. if (touchDown)
  28. {
  29. if (Input.touchCount > 0)
  30. {
  31. Touch[] tlist = Input.touches;
  32. int tId = 0;
  33. for (int i = 0; i < tlist.Length; i++)
  34. {
  35. if (tlist[tId].position.x < Screen.width / 2)
  36. {
  37. tId = i;
  38. continue;
  39. }
  40. }
  41. if(tlist[tId].position.x > Screen.width / 2) { return; }
  42. endPos = tlist[tId].position;
  43. }
  44. else if (Input.mousePosition.x < Screen.width / 2)
  45. {
  46. endPos = Input.mousePosition;
  47. }
  48. deltaPos = endPos - startPos;
  49. deltaPos = new Vector2(Mathf.Clamp(deltaPos.x, -maxDist, maxDist), Mathf.Clamp(deltaPos.y, -maxDist, +maxDist));
  50. }
  51. else
  52. {
  53. // deltaPos = Vector2.zero;
  54. }
  55. Vector2 _input = new Vector2(deltaPos.x / maxDist, deltaPos.y / maxDist);
  56. if(_input.magnitude < 0.1f)
  57. {
  58. return;
  59. }
  60. input = _input;
  61. input = (input.magnitude > 1.0f) ? input.normalized : input;
  62. joyStick.localPosition = new Vector2(input.x * (joyBG.sizeDelta.x / 2f), input.y * (joyBG.sizeDelta.y / 2f));
  63. */
  64. }
  65. public void onDrag(BaseEventData pointer)
  66. {
  67. PointerEventData ped = pointer as PointerEventData;
  68. endPos = ped.position;
  69. deltaPos = endPos - startPos;
  70. deltaPos = new Vector2(Mathf.Clamp(deltaPos.x, -maxDist, maxDist), Mathf.Clamp(deltaPos.y, -maxDist, +maxDist));
  71. Vector2 _input = new Vector2(deltaPos.x / maxDist, deltaPos.y / maxDist);
  72. if (_input.magnitude < 0.1f)
  73. {
  74. return;
  75. }
  76. input = _input;
  77. input = (input.magnitude > 1.0f) ? input.normalized : input;
  78. joyStick.localPosition = new Vector2(input.x * (joyBG.sizeDelta.x / 2f), input.y * (joyBG.sizeDelta.y / 2f));
  79. }
  80. public void pointerDown(BaseEventData pointer)
  81. {
  82. PointerEventData ped = pointer as PointerEventData;
  83. startJoy(ped);
  84. }
  85. public void pointerUp()
  86. {
  87. endJoy();
  88. }
  89. void startJoy(PointerEventData pointer)
  90. {
  91. startPos = pointer.position;
  92. joyBG.gameObject.SetActive(true);
  93. joyStick.gameObject.SetActive(true);
  94. joyStick.localPosition = Vector2.zero;
  95. joyBG.position = startPos;
  96. touchDown = true;
  97. }
  98. void endJoy()
  99. {
  100. // joyBG.gameObject.SetActive(false);
  101. // joyStick.gameObject.SetActive(false);
  102. input = Vector2.zero;
  103. touchDown = false;
  104. joyStick.localPosition = Vector2.zero;
  105. }
  106. }