private void OnDisable() { // When we disable, we just unregister our axis // It also happens before the game object is Destroyed How_To_Do_Touch_InputManager.UnregisterVirtualAxis(_horizintalAxis); How_To_Do_Touch_InputManager.UnregisterVirtualAxis(_verticalAxis); }
private void Update() { // Now this is a bit tricky // As we are completely REPLACING the uGUI event system for the Editor, we need to handle both Touch and Mouse inputs // For every touch out there for (int i = 0; i < How_To_Do_Touch_InputManager.TouchCount; i++) { var touch = How_To_Do_Touch_InputManager.GetTouch(i); PointerEventDataCache.position = touch.position; // Check if it's inside our rectangle if (RectTransformUtility.RectangleContainsScreenPoint(_linkedButtonRectTransform, touch.position, UiRootCamera)) { // If it's inside, it's just started AND the button has not been pressed yet if (touch.phase == TouchPhase.Began && LastFingerId == -1) { // We press the button _linkedButton.OnPointerDown(PointerEventDataCache); // Remember our pressed finger id LastFingerId = touch.fingerId; return; } } // If it's just been lifted AND this is the finger that was pressing this button if (touch.phase == TouchPhase.Ended && touch.fingerId == LastFingerId) { // We release the button _linkedButton.OnPointerUp(PointerEventDataCache); // Reset finger ID so we can Press again LastFingerId = -1; return; } } // Mouse input here // Same logic, but mouse is considered to be the finger with id of 255 so it's definitely won't interfere with actual fingers PointerEventDataCache.position = Input.mousePosition; if (RectTransformUtility.RectangleContainsScreenPoint(_linkedButtonRectTransform, PointerEventDataCache.position, UiRootCamera)) { if (Input.GetMouseButtonDown(0)) { _linkedButton.OnPointerDown(PointerEventDataCache); LastFingerId = 255; return; } } if (Input.GetMouseButtonUp(0) && LastFingerId == 255) { _linkedButton.OnPointerUp(PointerEventDataCache); LastFingerId = -1; } }
private void OnEnable() { // When we enable, we get our virtual axis _horizintalAxis = _horizintalAxis ?? new VirtualAxis(HorizontalAxisName); _verticalAxis = _verticalAxis ?? new VirtualAxis(VerticalAxisName); // And register them in our input system How_To_Do_Touch_InputManager.RegisterVirtualAxis(_horizintalAxis); How_To_Do_Touch_InputManager.RegisterVirtualAxis(_verticalAxis); }
public void Update() { // Now this is a bit tricky // As we are completely REPLACING the uGUI event system for the Editor, we need to handle both Touch and Mouse inputs // For every touch out there for (int i = 0; i < How_To_Do_Touch_InputManager.TouchCount; i++) { var touch = How_To_Do_Touch_InputManager.GetTouch(i); PointerEventDataCache.position = touch.position; PointerEventDataCache.delta = touch.deltaPosition; // Check if it's inside our rectangle if (RectTransformUtility.RectangleContainsScreenPoint(_touchpadTouchRect, touch.position, UiRootCamera)) { // If it's inside, it's just started AND the joystick is not being tweaked yet if (touch.phase == TouchPhase.Began && LastFingerId == -1) { // We press the joystick _linkedTouchpad.OnPointerDown(PointerEventDataCache); // Remember our pressed finger id LastFingerId = touch.fingerId; return; } } // If it's just been lifted AND this is the finger that was tweaking this joystick if (touch.phase == TouchPhase.Ended && touch.fingerId == LastFingerId) { // We release the joystick _linkedTouchpad.OnPointerUp(PointerEventDataCache); // Reset finger ID so we can Press again LastFingerId = -1; return; } if (touch.phase == TouchPhase.Moved && touch.fingerId == LastFingerId) { _linkedTouchpad.OnDrag(PointerEventDataCache); return; } } // Mouse input here // Same logic, but mouse is considered to be the finger with id of 255 so it's definitely won't interfere with actual fingers PointerEventDataCache.position = Input.mousePosition; PointerEventDataCache.delta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); PointerEventDataCache.delta *= 10f; if (RectTransformUtility.RectangleContainsScreenPoint(_touchpadTouchRect, PointerEventDataCache.position, UiRootCamera)) { if (Input.GetMouseButtonDown(0)) { _linkedTouchpad.OnPointerDown(PointerEventDataCache); LastFingerId = 255; return; } } if (Input.GetMouseButtonUp(0) && LastFingerId == 255) { _linkedTouchpad.OnPointerUp(PointerEventDataCache); LastFingerId = -1; return; } if (Input.GetMouseButton(0) && LastFingerId == 255 && PointerEventDataCache.delta.sqrMagnitude > 0.000000001f) { _linkedTouchpad.OnDrag(PointerEventDataCache); } }
/// <summary> /// When we disable, we unregister our button /// </summary> private void OnDisable() { How_To_Do_Touch_InputManager.UnregisterVirtualButton(_virtualButton); }
/// <summary> /// It's pretty simple here /// When we enable, we register our button in the input system /// </summary> private void OnEnable() { _virtualButton = _virtualButton ?? new VirtualButton(ButtonName); How_To_Do_Touch_InputManager.RegisterVirtualButton(_virtualButton); }