Question about creating a button and reacting to button being held down

how can i create a button that increments a value as long as it is pressed and influences a slider as a visual component?
as soon as you let go, an action should be executed. at the moment i have a button on a canvas but i don’t really know how I should realize the pressed and held because I only have the buttons on click() as an event.
is there possibly another better way?

Original Post on Discord

by user 313407509618622466

Hello there – we have an experimental AI bot :robot: that might be able to help you with your question. Would you like to try this out?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

Hello Zerker94, to create a button that increments a value as long as it’s pressed, you can create a custom component that implements the IPointerEventHandler interface. You can use onPointerDown() to start the increment and onPointerUp() to perform the final action and stop incrementing. The Button class from Needle Engine has onPointerEnter, onPointerExit, onPointerDown, onPointerUp, and onPointerClick methods that you can use for this purpose (see the documentation).

Here’s an outline of the steps:

  1. Create a component that implements IPointerEventHandler.
  2. In onPointerDown, start incrementing the value and update the slider.
  3. In onPointerUp, stop incrementing and perform the final action.
  4. Ensure that your button’s interactable state is enabled.

Now, here’s a TypeScript example of how to implement this:


i struggle to get the slider component as it wont be recognized by getComponent. I managed to get the GameObject holding the component but can’t access the component to edit it’s value. any idea?

by user 313407509618622466

How did you create the slider and how do you try to access it? The Unity Canvas slider is currently not supported: Core Components | Needle Engine Documentation

oh i see thanks. i just created a Slider object on my canvas object.
i have it like that
—> ShootButton (my component that wants to access the slider is here)
—> Slider
i tried to to grab the GameObject with this.slider = this.gameObject.parent?.getObjectByName(“Slider”);
It seems to retrieve the gameobject named Slider but there is no way to access it component. (atleast not with this.slider.getComponent(Slider))
but sure if its not supported it will not work. is there any alternative? i want something like a powerbar that charges while you press down the button and
for that i wanted to use the slider

by user 313407509618622466

Would using an Image component anchored to the left side work? then you can just modify it’s width (via accessing the RectTransform)

thanks for the input. i will try it

by user 313407509618622466

seems promising. i managed to anchor it properly and play around with it in my scene view however i struggle to set the width of the RectTransform Component via code but im definitely able to get it via getComponent. When i try to write to it with this.sliderRectTransform.width = 100; it throws an error “Cannot set property width of # which has only a getter”

by user 313407509618622466

Ah sorry can you try sizeDelta ?

that works fine thank you! i had to do a bit of trial and error because it wasn’t showed as option/autocompletion in vscode but this.sliderRectTransform.sizeDelta.x seems to do the trick

by user 313407509618622466