4
\$\begingroup\$

I want to add a nitro in my arcade game and i want to detect press UI button instead of click. unfortunately only onClick() event is available for UI button

enter image description here

what kind of options do I have ?

Thank you

\$\endgroup\$
8
\$\begingroup\$

You can use OnPointerDown. Here is the example: http://forum.unity3d.com/threads/touch-and-hold-a-button-on-new-ui.266065/#post-1758312

public static bool mouseDown;
public float timeMouseDown;

void Update(){
    if(mouseDown)
       timeMouseDown += time.deltaTime;
}

void OnPointerDown(){
      mouseDown = true;
}
void OnPointerUp(){
      mouseDown = false;
      timeMouseDown = 0;
}
\$\endgroup\$
3
  • \$\begingroup\$ they've shown the solution, but i don't know how to add that event handler to the button. is the code should be dragged to the onClick() event ? \$\endgroup\$ Sep 17 '15 at 15:54
  • 1
    \$\begingroup\$ Look a little further, at #17 post. There is a class PointerListener that can be attached to UI button. \$\endgroup\$
    – Skyblade
    Sep 17 '15 at 15:59
  • \$\begingroup\$ Perfect solution. \$\endgroup\$
    – YesBarry
    Jul 14 at 20:42
2
\$\begingroup\$

Check out the AddListener stuff. You don't have to use the inspector click if you do not want to. But when you do it fires when it is pressed

.onClick.AddListener()//to add a listener

Also you can put a script on any gameobject as a nitro button and use

OnMouseDown(){} //fire on mouse down or tap

to answer your comment. you would drag the game object that has the script with the method to fire to the button click slot and as long as it is a public method you can scroll in and find the method to fire on click it will execute the code in that method. It looks like your button is set up properly was it not firing? if it does not fire right click and create a UI EventSystem to solve.

\$\endgroup\$
0
1
\$\begingroup\$

OnPointerDown didn't work for me - For some reason, it only triggered when the button was released.

This worked great though:

public void CustomOnPressed()
{
    Debug.LogError("I got pressed!");
}

Then add an Event Trigger (Script) to your object in the editor (instead of Button), and select Pointer Down:

Event Trigger visualization

\$\endgroup\$
1
\$\begingroup\$

You can implement your own click handler to load a scene like this:

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

//Be sure to change YOUR_SCRIPT_NAME_NAME to the name of your script.
public class YOUR_SCRIPT_NAME_HERE : MonoBehaviour, IPointerClickHandler
{
    public void OnPointerClick(PointerEventData eventData)
    {
        //I named my button the name of the scene i want to load
        SceneManager.LoadScene(eventData.pointerPress.name);
        Debug.Log("Switching level: " + eventData.pointerPress.name);
    }
}
\$\endgroup\$
0
0
\$\begingroup\$

I just removed the Button component and added the PressButton component. Now I can call the public IsPressed() method to check the state.

using UnityEngine.UI;

public class PressButton : Button
{
    public bool PublicIsPressed()
    {
        return base.IsPressed();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.