rive-app / rive-unity

MIT License
87 stars 8 forks source link

Issue with Pointer Up in Rive #66

Closed tursiITT closed 1 week ago

tursiITT commented 2 weeks ago

Hello everyone, I've created a slider in Rive that works using two listeners that detect the "pointer down" and "pointer up" events, modifying the isDragging state and allowing the slider to update its position when it's true. However, this mechanism doesn't seem to work correctly, as it doesn't always detect the pointer up event. Do you have any suggestions for a workaround?

damzobridge commented 2 weeks ago

Could you share the script you're using to display the Rive file?

tursiITT commented 2 weeks ago

The slider logic I developed all within the Rive app, I followed this example and even here the pointerup is not always detected: https://editor.rive.app/preview/12119-23045-how-to-foldunfold-a-brompton-bike/10040669?mode=animate&artboard=Artboard&animation=State%20Machine%201

On unity I simply associated the Mouse Position and Mouse Down via this script:

private void Update()
    {
        m_helper?.UpdateTextureHelper();
        if (m_artboard == null)
        {
            return;
        }

        Camera camera = gameObject.GetComponent<Camera>();
        if (camera != null)
        {
            Vector3 mousePos = camera.ScreenToViewportPoint(Input.mousePosition);
            Vector2 mouseRiveScreenPos = new Vector2(mousePos.x * camera.pixelWidth, (1 - mousePos.y) * camera.pixelHeight);

            if (m_lastMousePosition != mouseRiveScreenPos)
            {
                Vector2 local = m_artboard.LocalCoordinate(mouseRiveScreenPos,new Rect(0, 0, camera.pixelWidth, camera.pixelHeight), fit, alignment);
                m_stateMachine?.PointerMove(local);
                m_lastMousePosition = mouseRiveScreenPos;
            }
            if (Input.GetMouseButtonDown(0))
            {
                Vector2 local = m_artboard.LocalCoordinate(mouseRiveScreenPos,new Rect(0, 0, camera.pixelWidth, camera.pixelHeight), fit, alignment);
                m_stateMachine?.PointerDown(local);
                m_wasMouseDown = true;
            }
            else if (m_wasMouseDown)
            {
                m_wasMouseDown = false;
                Vector2 local = m_artboard.LocalCoordinate(mouseRiveScreenPos, new Rect(0, 0, camera.pixelWidth, camera.pixelHeight), fit, alignment);
                m_stateMachine?.PointerUp(local);
            }
        }

        m_stateMachine?.Advance(Time.deltaTime);

    }
damzobridge commented 2 weeks ago

Thanks! It looks like the script is using Input.GetMouseButtonDown(0), if you switch that to Input.GetMouseButton(0), does that fix things for you?