Advertisement
Guest User

Untitled

a guest
May 15th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. // dnSpy decompiler from Assembly-CSharp.dll class: FlightKit.AirplaneTouchControl
  2. using System;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. using UnityStandardAssets.CrossPlatformInput;
  7.  
  8. namespace FlightKit
  9. {
  10. public class AirplaneTouchControl : MonoBehaviour, IPointerDownHandler, IEventSystemHandler, IPointerUpHandler
  11. {
  12. private void OnEnable()
  13. {
  14. this.CreateVirtualAxes();
  15. }
  16.  
  17. private void CreateVirtualAxes()
  18. {
  19. this.m_UseX = (this.axesToUse == AirplaneTouchControl.AxisOption.Both || this.axesToUse == AirplaneTouchControl.AxisOption.OnlyHorizontal);
  20. this.m_UseY = (this.axesToUse == AirplaneTouchControl.AxisOption.Both || this.axesToUse == AirplaneTouchControl.AxisOption.OnlyVertical);
  21. if (this.m_UseX)
  22. {
  23. this.m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(this.horizontalAxisName);
  24. CrossPlatformInputManager.RegisterVirtualAxis(this.m_HorizontalVirtualAxis);
  25. }
  26. if (this.m_UseY)
  27. {
  28. this.m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(this.verticalAxisName);
  29. CrossPlatformInputManager.RegisterVirtualAxis(this.m_VerticalVirtualAxis);
  30. }
  31. }
  32.  
  33. private void UpdateVirtualAxes(Vector3 value)
  34. {
  35. if (this.m_UseX)
  36. {
  37. this.m_HorizontalVirtualAxis.Update(value.x);
  38. }
  39. if (this.m_UseY)
  40. {
  41. this.m_VerticalVirtualAxis.Update(value.y);
  42. }
  43. }
  44.  
  45. private void Update()
  46. {
  47. if (!this.m_Dragging)
  48. {
  49. return;
  50. }
  51. if (this.m_Dragging)
  52. {
  53. Vector3 a = Input.touches[this.m_Id].position;
  54. Vector3 vector = a - this.m_Center;
  55. float d = Mathf.Min(vector.magnitude / (this.maxHandleDistance + 0.01f), 1f);
  56. vector.Normalize();
  57. vector *= d;
  58. vector.x *= this.Xsensitivity;
  59. vector.y *= -1f * this.Ysensitivity;
  60. this.UpdateVirtualAxes(vector);
  61. Vector3 localPosition = a - this.m_Center;
  62. if (localPosition.magnitude > this.maxHandleDistance)
  63. {
  64. localPosition = localPosition.normalized * this.maxHandleDistance;
  65. }
  66. this.handleImage.transform.localPosition = localPosition;
  67. }
  68. }
  69.  
  70. public void OnPointerDown(PointerEventData data)
  71. {
  72. this.m_Dragging = true;
  73. this.m_Id = data.pointerId;
  74. if (this.baseImage != null)
  75. {
  76. this.baseImage.enabled = true;
  77. this.baseImage.transform.position = data.position;
  78. this.handleImage.enabled = true;
  79. this.handleImage.transform.position = this.baseImage.transform.position;
  80. }
  81. this.m_Center = data.position;
  82. }
  83.  
  84. public void OnPointerUp(PointerEventData data)
  85. {
  86. this.m_Dragging = false;
  87. this.m_Id = -1;
  88. this.UpdateVirtualAxes(Vector3.zero);
  89. if (this.baseImage != null)
  90. {
  91. this.baseImage.enabled = false;
  92. this.handleImage.enabled = false;
  93. }
  94. }
  95.  
  96. private void OnDisable()
  97. {
  98. if (CrossPlatformInputManager.AxisExists(this.horizontalAxisName))
  99. {
  100. CrossPlatformInputManager.UnRegisterVirtualAxis(this.horizontalAxisName);
  101. }
  102. if (CrossPlatformInputManager.AxisExists(this.verticalAxisName))
  103. {
  104. CrossPlatformInputManager.UnRegisterVirtualAxis(this.verticalAxisName);
  105. }
  106. }
  107.  
  108. public AirplaneTouchControl.AxisOption axesToUse;
  109.  
  110. public string horizontalAxisName = "Horizontal";
  111.  
  112. public string verticalAxisName = "Vertical";
  113.  
  114. public float Xsensitivity = 1f;
  115.  
  116. public float Ysensitivity = 1f;
  117.  
  118. [Header("Virtual joystick base to indicate steering center.")]
  119. public Image baseImage;
  120.  
  121. [Header("Virtual joystick handle to indicate steering direction.")]
  122. public Image handleImage;
  123.  
  124. [Header("How far can the handle move from the center of the base image.")]
  125. public float maxHandleDistance = 100f;
  126.  
  127. private Vector3 m_StartPos;
  128.  
  129. private Vector2 m_PreviousDelta;
  130.  
  131. private Vector3 m_JoytickOutput;
  132.  
  133. private bool m_UseX;
  134.  
  135. private bool m_UseY;
  136.  
  137. private CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;
  138.  
  139. private CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis;
  140.  
  141. private bool m_Dragging;
  142.  
  143. private int m_Id = -1;
  144.  
  145. private Vector3 m_Center;
  146.  
  147. public enum AxisOption
  148. {
  149. Both,
  150. OnlyHorizontal,
  151. OnlyVertical
  152. }
  153. }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement