Search Unity

adding an Update function to an Event Trigger

Discussion in 'UGUI & TextMesh Pro' started by josephmknight, Aug 2, 2016.

  1. josephmknight

    josephmknight

    Joined:
    Oct 1, 2013
    Posts:
    3
    My Goal: a UI button that when pressed and held, pans the camera in one direction

    Yes, I'm not much of a coder, but I do my best. I've been searching these forums for a solution, and I have found solutions from a couple of years ago. But I don't quite understand what's suggested, missing some essential understanding, and the threads are so old I assume no one will answer.

    I'm using the "New UI" Event Trigger component on the UI button, and acting to translate a GameObject I call "POV" which includes the camera.

    If I include a public void function I call MoveRight in a script, the movement happens just at the press, and doesn't continue as the button is held. So of course I need something that occurs with each frame, i.e. in an Update function.

    The problem is that the Event Trigger UI (under "On Click") does not allow me to select Update functions. The solutions I found include recurring steps in Update, and tell me to use THAT function.

    Where did I go wrong?
     
    Last edited: Aug 2, 2016
  2. josephmknight

    josephmknight

    Joined:
    Oct 1, 2013
    Posts:
    3
    Could it be that the default Event Trigger will only accept "On Click" - that is won't execute until MouseUp? And that I need to create a new custom Event Trigger instead?

    Someone please confirm.
     
  3. josephmknight

    josephmknight

    Joined:
    Oct 1, 2013
    Posts:
    3
    Perhaps I have one new Event Trigger for Pointer Down, and in that function just loop a translation, and then a second Event Trigger for Pointer Up, to cancel that loop?
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    You can implement the IPointerDownHandler, IPointerUpHandler interfaces to figure out when you button gets pressed down and up.
    Then you can do your code in the update function while the button is pressed down.
    It could look like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using System;
    5.  
    6. public class DirectionButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    7. {
    8.     private bool heldDown;
    9.  
    10.     public void OnPointerDown(PointerEventData eventData)
    11.     {
    12.         heldDown = true;
    13.     }
    14.  
    15.     public void OnPointerUp(PointerEventData eventData)
    16.     {
    17.         heldDown = false;
    18.     }
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.  
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update () {
    27.  
    28.         if (heldDown)
    29.         {
    30.             //Do stuff
    31.         }
    32.     }
    33. }
    34.  
     
    tokar_dev likes this.
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you might need to implement the IPointerExitHandler as well in case the user leaves the button, seem to remember there being threads on the "inconsistency" of the Down/Up handling in those cases.
     
  6. tofanity

    tofanity

    Joined:
    Jul 23, 2021
    Posts:
    1

    I'm using exact same code, but when I touch my button the OnPointerDown funciton is called and heldDown is true, and without being called OnPointerUp function, when my Update function is executed heldDown is false. I don't understand why this is happening. Can you help me? Also, if instead of using Update function, I use FixedUpdate everything is fine, but I really need Update function because I need this to be executed even if Time.scaleTime is 0.