Search Unity

Event Handlers — A Suggestion

Discussion in 'UGUI & TextMesh Pro' started by podperson, Sep 14, 2014.

  1. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    It seems to me that the options for passing parameters back from events are not very useful. The information you generally want about an event is stuff about the event itself, e.g. which control sent the event? what was the state of input devices at that moment? Look at the event structures that get passed around by default in the DOM APIs, iOS, Mac OS X, Windows, Mac OS Classic. Instead, we get the option of passing a pre-specified Object of some kind (which almost certainly won't be very helpful) or nothing.

    As things stand now, if we get a PointerUp event, we need to interrogate the EventSystem to figure out elementary stuff about the event, some of which is deeply nested or (as far as I can tell) unavailable.

    I'd suggest adding the option of an event record structure (possibly including a custom object or string or whatever) containing this key information. It would probably solve 95% of needs without much effort at all. (Want to respond to the same event from different widgets in different ways? Use the widget's name.)
     
    Last edited: Sep 14, 2014
  2. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I notice that there is an Event class which looks very promising. How does one go about receiving these? It seems like the obvious thing to pass to an event handler (rather than some specific string, float, or Object).
     
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4. public class Item : MonoBehaviour, IPointerClickHandler
    5. {
    6.     public Flow Flow;
    7.     private Transform _t;
    8.     private void Start()
    9.     {
    10.         _t = transform;
    11.     }
    12.     public void OnPointerClick(PointerEventData e)
    13.     {
    14.         e.Use();
    15.         Flow.TweenTo(_t);
    16.     }
    17. }
    18.  
     
  4. SilentWarrior

    SilentWarrior

    Joined:
    Aug 3, 2010
    Posts:
    107
    Just to point out that what Rakkarage is showing is that using the Interfaces, you can implement the old "onClick", "onHover" etc functions that you applied on colliders.

    Say, if you want to also implement hover effects you implement the IPointerEnterHandler and the IPointerExitHandler, get PointerEventData out of it.

    You can also implement IDragHandler, and look for the PointerEventData delta field that tells you how much it has moved.

    The dragHandler also has begin drag and enddrag, very useful. You can also put all of those on a gameobject further up the chain and listen to those events there, they will be caught for children (who dont consume them).

    Example:
    This will turn off map functions if any of the map GUI is clicked, hovered, dragged, etc. You can move my map around with mouse drag and zoom it with mouse wheel, so, those functions need to be turned off when the GUI for the map is beeing used.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class TurnOffMapFunctionsOnHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IEndDragHandler, IBeginDragHandler
    5. {
    6.  
    7.         public static int Hovers = 0;
    8.  
    9.         static bool debug = false;
    10.  
    11.         public void OnPointerEnter (PointerEventData eventData)
    12.         {
    13.                 mouseOnGui = true;
    14.                 if (Input.GetMouseButton (0)) {
    15.                         mouseCameNotFromGUI = true;
    16.                 } else {                      
    17.                         Hovers++;
    18.                 }
    19.                 if (debug) {
    20.                         Debug.Log ("OnPointerEnter. Hovers:" + Hovers);
    21.                 }              
    22.         }
    23.  
    24.         public void OnPointerExit (PointerEventData eventData)
    25.         {
    26.                 mouseOnGui = false;
    27.                 Hovers--;  
    28.                 if (debug) {
    29.                         Debug.Log ("OnPointerExit. Hovers:" + Hovers);
    30.                 }          
    31.         }
    32.  
    33.  
    34.         public void OnPointerDown (PointerEventData eventData)
    35.         {
    36.                 Hovers++;  
    37.                 if (debug) {
    38.                         Debug.Log ("OnPointerDown. Hovers:" + Hovers);
    39.                 }
    40.         }
    41.  
    42.  
    43.         public void OnPointerUp (PointerEventData eventData)
    44.         {
    45.                 Hovers--;
    46.                 if (debug) {
    47.                         Debug.Log ("OnPointerUp. Hovers:" + Hovers);
    48.                 }
    49.         }
    50.  
    51.  
    52.         public void OnBeginDrag (PointerEventData eventData)
    53.         {
    54.                 Hovers++;
    55.                 if (debug) {
    56.                         Debug.Log ("OnBeginDrag. Hovers:" + Hovers);
    57.                 }
    58.         }
    59.  
    60.         public void OnEndDrag (PointerEventData eventData)
    61.         {
    62.                 Hovers--;
    63.                 if (debug) {
    64.                         Debug.Log ("OnEndDrag. Hovers:" + Hovers);
    65.                 }
    66.         }
    67.  
    68.  
    69.  
    70.         bool mouseOnGui = false;
    71.         bool locked = false;
    72.         bool mouseCameNotFromGUI = false;
    73.  
    74.         void Update ()
    75.         {
    76.                 if (Hovers < 0)
    77.                         Hovers = 0;
    78.                 if (Input.GetMouseButton (0) && mouseOnGui) {
    79.                         if (!mouseCameNotFromGUI)
    80.                                 locked = true;
    81.                 }
    82.                 if (!Input.GetMouseButton (0)) {
    83.                         locked = false;
    84.                         mouseCameNotFromGUI = false;
    85.                 }
    86.  
    87.  
    88.                 MouseScroll.canScroll = !locked && Hovers == 0;
    89.                 MapClick.canMove = !locked && Hovers == 0;
    90.         }
    91.  
    92.         void OnGUI ()
    93.         {
    94.                 if (debug)
    95.                         GUI.Label (new Rect (Screen.width / 2, Screen.height - 20f, 300, 20), "canScroll = " + MouseScroll.canScroll + " canMove = " + MapClick.canMove);
    96.         }
    97. }