Search Unity

Simple way to detect button release like onpointerup event

Discussion in 'Scripting' started by gustavopinent, Jul 29, 2019.

  1. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    I am looking for a solution of to implement an onpointerup in my buttons. These buttons are already in my canvas with sprite swap transitions, I need to detect when pressed (easy) and when released. In Flash or JavaScript, it's just a matter of an myButton.addeventlistener, but in Unity there is only onpointerdown or the onclick. I didn't find a way to know if the button is still being pressed, so I could check in each frame (also not sure if will work properly).

    I found a selectable.onpointerup that I don't know how to implement and some tutorials about custom buttons that I think won't be the case (also a bit advanced level for me). Is there a simple way to solve this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    I do believe there is! If I understand your question, you wanna stick one of these on your button:

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

    Note the docs specify two possible ways, one by inheriting from EventTrigger, one by just hanging on the events you want.
     
    gustavopinent likes this.
  3. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    Thanks. So I am following the example in baby steps. This is the code I wrote and added to each button:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class UsrEvent : EventTrigger
    5. {
    6.     //public GameObject target;
    7.  
    8.     public override void OnPointerDown(PointerEventData data)
    9.     {
    10.         Debug.Log("OnPointerDown called.");
    11.         //target.GetComponent<Usr>().BtnState("forward:down");
    12.     }
    13.  
    14.     public override void OnPointerUp(PointerEventData data)
    15.     {
    16.         Debug.Log("OnPointerUp called.");
    17.         //target.GetComponent<Usr>().BtnState("forward:up");
    18.     }
    19. }
    Thought I will be able to get the target code and call the function will handle with the user input. This code will mix keyboard input with button input. For instance: by clicking the arrow up button or pressing "w" key, a command dictionary in "forward" item will be set to true. There is another one to set true when released. So in the future I can add a controller easily by pointing events to the same central script, at list that's the idea.

    But the public var "target" won't appear, I have to set all by the editor and didn't work so far. I drag the object to the component, the function is found but won't be called when the event is fired (I know it is fired because of the log).
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    I think that's because EventTrigger has a whacky custom editor function.

    Maybe instead just try hooking a little stub script (a Monobehavior, not an EventTrigger, but placed on the button as well) and receive those events from the buttons, and use that script to put any extra info you need into the calls you forward onto your Usr??
     
    gustavopinent likes this.
  5. gustavopinent

    gustavopinent

    Joined:
    Jan 14, 2019
    Posts:
    38
    What I did and worked was forgetting about the editor (deleted those Event Types) and just writing a way out inside the event handler:
    Code (CSharp):
    1. public override void OnPointerDown(PointerEventData data)
    2.     {
    3.         Debug.Log("OnPointerDown called.");
    4.         //target.GetComponent<Usr>().BtnState("forward:down");
    5.         GameObject.Find("Usr").GetComponent<Usr>().BtnState("forward:down");
    6.     }
    Maybe is just what you was thinking, right? The only issue is developers don't like the extensive use of GameObject.Find() but is what I got so far. This way is easy to implement because I just have to change the name of the command (E.G: forward to background) to use it in another button and so on.
    I am happy (like a baby) with this result by now, thanks!
    But I confess Unity turn my head upside down. My last game was made in the opposite way: I build a class of my object (my space ship) and than everything else is added on top of if, including the image of the ship among with events as collisions. In Unity I create the image of the ship and drag everything else later, including the script, on top of the image... that's weird to me!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Excellent! That's the best news I've heard all day.

    Yes, I agree... you have to think of everything differently.

    But I promise you, the more you use Unity, the more you will like it. Almost everything is just a Component plugged into a GameObject, and optionally connected in various ways, like you have done above.

    Welcome!
     
    gustavopinent likes this.