Unity events for holding down mouse and letting go on a slider

I’m trying to add some features where, while you’re adjusting a slider, the screen will change, and then change back when you let go of the mouse. Specifically, I want a number of sliders that control different pieces of a machine and, while the player is clicking on a particular slider, I want the game to highlight the machine component that that slider controls, and then stop highlighting the object when the player lets go.

However, the Unity documentation for sliders says they only come with one event, onValueChanged, which is triggered when the value of the slider changes, not when it’s clicked on or when the mouse is let go. I know that, separate from the slider, there are mouse events like MouseDownEvent and MouseUpEvent, but I’m not sure how to connect those events to a particular slider.

I think you can get what you want by adding an EventTrigger:

EDIT: yes, verified… that’s the sauce. The OnValueChanged still works fine, when the value changes, but you get the ptr up / ptr down messages correctly.

My TouchSlider script:

using UnityEngine;
using UnityEngine.UI;

// @kurtdekker

public class TouchSlider : MonoBehaviour
{
    public Renderer rend;

    public void OnValueChanged( float data)
    {
        Debug.Log( "data:" + data);
    }

    public void SetHighlighted( bool foo)
    {
        var mtl = rend.material;

        mtl.color = foo ? Color.red : Color.white;

        rend.material = mtl;
    }
}
1 Like