Press and Hold, on a button that also has standard OnClick? (C#)

Right, so I’m working on a mobile app currently, and one aspect is buttons that you press to add certain amounts to the score. What I’m trying to accomplish is when you hold down the button that adds to the score, it actually takes away the score instead. So basically a standard onClick is what I have in play now, and I’m trying to do an onClickAndHold (not a thing, I know, but the concept) on the same button. (Also if you have a easier way to do my ridiculous method calling, that would be appreciated.

public class MakeAndMissButton : MonoBehaviour {

    public Button goalButton;
    public Button savesButton;
    public Button assistsButton;
    public Button stealsButton;
    public int goalWorth;
    public int savesWorth;
    public int assistWorth;
    public int stealsWorth;

    private int totalScore;
   
    // Use this for initialization
    void Start () {
        goalButton.onClick.AddListener(IncreasePointsGoal);
        savesButton.onClick.AddListener(IncreasePointsSaves);
        assistsButton.onClick.AddListener(IncreasePointsAssists);
    }
    public void IncreasePointsGoal()
    {
        IncreasePoints(goalWorth);
    }
    public void IncreasePointsSaves()
    {
        IncreasePoints(savesWorth);
    }
    public void IncreasePointsAssists()
    {
        IncreasePoints(assistWorth);
    }

    public void IncreaseStat(int points)
    {
        //increase the counts variable by one when the button is pressed
        points++;
    }
    public void DecreaseStat(int points)
    {
        points--;
    }
    public void IncreasePoints(int score)
    {
        //increase the points by 2 when the button is pressed
        totalScore += score;
    }
    public void DecreasePoints(int score)
    {
        totalScore -= score;
    }
}

You can use the IPointer[…]Handler interfaces.

https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.html

1 Like

Ok that seems promising. Sorry, but I am pretty new to coding, and so I’m not sure how to use that to check if it was held down? Do I use a time variable, where I store timesincelevelload minus the time since the button was clicked, and then check if that amount if greater than a certain time amount, like 2 seconds?

Interfaces are basically a contract that says “some method or variable will exist on it if the script uses this Interface”. When you make a script that uses IPointerClickHandler you’ll be obligated by the compiler to create a method in that script called void OnPointerClick ( [PointerEventData](https://docs.unity3d.com/ScriptReference/EventSystems.PointerEventData.html) pointerEventData )

Now when Unity gets a ‘Pointer Click’ input it will look under the mouse for a UI item that has this interface and if it finds something then it will invoke the method on it.

You can put whatever intelligence you want into that method to make it behave like you want it to. The other Interfaces handle different things, such as IPointerClickHandler, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler, etc.

IPointerDownHandler sounds like what you want, to know that something was clicked, but they have not yet released the button. When they do release the button you can capture that inside the IPointerUpHandler method and halt counting points or whatever you like. You need to take time to understand how you expect this to work, what callbacks are available to you and how you can use them to accomplish what you need. There’s a lot of information about Interfaces online, and I’m pretty sure Unity probably has some tutorials on this in the Learn Section.

1 Like

Wow thank you for that response. I’m gonna look into it and see what I can work together. There’s so much to learn :slight_smile: Thanks for your help!

1 Like