using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.UIElements; public class TouchscreenInput : MonoBehaviour { public UnityEvent MoveEvent; public UnityEvent LookEvent; public UnityEvent JumpEvent; public UnityEvent SprintEvent; private UIDocument m_Document; private VirtualJoystick m_MoveJoystick; private VirtualJoystick m_LookJoystick; private void Awake() { m_Document = GetComponent(); } private void Start() { var joystickMove = m_Document.rootVisualElement.Q("JoystickMove"); var joystickLook = m_Document.rootVisualElement.Q("JoystickLook"); m_MoveJoystick = new VirtualJoystick(joystickMove); m_MoveJoystick.JoystickEvent.AddListener(mov => { MoveEvent.Invoke(mov); });; m_LookJoystick = new VirtualJoystick(joystickLook); m_LookJoystick.JoystickEvent.AddListener(mov => { LookEvent.Invoke(mov); }); var jumpButton = m_Document.rootVisualElement.Q("ButtonJump"); jumpButton.RegisterCallback(evt => { JumpEvent.Invoke(true); }); jumpButton.RegisterCallback(evt => { JumpEvent.Invoke(false); }); var sprintButton = m_Document.rootVisualElement.Q("ButtonSprint"); sprintButton.RegisterCallback(evt => { SprintEvent.Invoke(true); }); sprintButton.RegisterCallback(evt => { SprintEvent.Invoke(false); }); } } public class VirtualJoystick { public VisualElement BaseElement; public VisualElement Thumbstick; public UnityEvent JoystickEvent = new(); public VirtualJoystick(VisualElement root) { BaseElement = root; Thumbstick = root.Q("JoystickHandle"); BaseElement.RegisterCallback(HandlePress); BaseElement.RegisterCallback(HandleDrag); BaseElement.RegisterCallback(HandleRelease); } void HandlePress(PointerDownEvent evt) { BaseElement.CapturePointer(evt.pointerId); } void HandleRelease(PointerUpEvent evt) { BaseElement.ReleasePointer(evt.pointerId); Thumbstick.style.left = Length.Percent(50); Thumbstick.style.top = Length.Percent(50); JoystickEvent.Invoke(Vector2.zero); } void HandleDrag(PointerMoveEvent evt) { if (!BaseElement.HasPointerCapture(evt.pointerId)) return; var width = BaseElement.contentRect.width; var center = new Vector3(width / 2, width / 2); var centerToPosition = evt.localPosition - center; if (centerToPosition.magnitude > width/2) { centerToPosition = centerToPosition.normalized * width / 2; } var newPos = center + centerToPosition; Thumbstick.style.left = newPos.x; Thumbstick.style.top = newPos.y; centerToPosition /= (width / 2); //we invert y as the y of UI goes down, but pushing the joystick up is expected to give a positive y value centerToPosition.y *= -1; JoystickEvent.Invoke(centerToPosition); } }