Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Do custom input modules work?

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by mweldon, Nov 19, 2014.

  1. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    Seems like they should be pretty simple based on the code from here: https://gist.github.com/stramit/76e53efd67a2e1cf3d2f

    I'm trying to make an event that fires when you are holding down the left button, but not dragging the mouse. Based on the debug log, the code runs all the way up to where my CustomInputModule calls ExecuteEvents.Execute but it never actually calls CustomInputEvents.Execute.

    CustomInputModule, derived from StandaloneInputModule. I replaced the Standalone on the EventSystem object with this, and all the normal events seem to work fine.
    Code (CSharp):
    1. public class CustomInputModule : StandaloneInputModule
    2. {
    3.     [SerializeField]
    4.     private bool enablePointerHoldEvent = true;
    5.  
    6.     // called each tick on active input module
    7.     public override void Process()
    8.     {
    9.         base.Process();
    10.  
    11.         if( enablePointerHoldEvent )
    12.         {
    13.             bool leftIsPressed = Input.GetMouseButton( 0 );
    14.             if( leftIsPressed )
    15.             {
    16.                 var mouseData = GetMousePointerEventData();
    17.                 var leftButtonData = mouseData[PointerEventData.InputButton.Left];
    18.                 ProcessPointerHoldEvent( leftButtonData );
    19.             }
    20.         }
    21.     }
    22.  
    23.     private void ProcessPointerHoldEvent( MouseButtonEventData data )
    24.     {
    25.         var pointerEvent = data.buttonData;
    26.  
    27.         if( pointerEvent.dragging )
    28.         {
    29.             return;
    30.         }
    31.  
    32.         var currentOverGob = pointerEvent.pointerCurrentRaycast.gameObject;
    33.  
    34.         Debug.Log( "Calling Execute: " + currentOverGob.name );
    35.         ExecuteEvents.Execute( currentOverGob, new PointerHoldEventData( eventSystem, 1.0f ), CustomInputEvents.PointerHoldEventHandler );   // THIS IS AS FAR AS IT GETS
    36.     }
    37.  
    38. }
    The code will call ExecuteEvents.Execute but never goes into this Execute function:
    Code (CSharp):
    1. public static class CustomInputEvents
    2. {
    3.     // call that does the mapping
    4.     private static void Execute( IPointerHoldHandler handler, BaseEventData eventData )
    5.     {
    6.         Debug.Log( "Executing PointerHoldEventHandler" );
    7.         // The ValidateEventData makes sure the passed event data is of the correct type
    8.         handler.OnPointerHold( ExecuteEvents.ValidateEventData<PointerHoldEventData>( eventData ) );
    9.     }
    10.  
    11.     // helper to return the functor that should be invoked
    12.     public static ExecuteEvents.EventFunction<IPointerHoldHandler> PointerHoldEventHandler
    13.     {
    14.         get
    15.         {
    16.             Debug.Log( "Getting functor" );
    17.             return Execute;
    18.         }
    19.     }
    20. }
    21.  
    My custom event:
    Code (CSharp):
    1. public interface IPointerHoldHandler : IEventSystemHandler
    2. {
    3.     void OnPointerHold( PointerHoldEventData eventData );
    4. }
    5.  
    6. public class PointerHoldEventData : BaseEventData
    7. {
    8.     public float holdTime;
    9.  
    10.     public PointerHoldEventData( EventSystem eventSystem, float holdTime )
    11.         : base( eventSystem )
    12.     {
    13.         this.holdTime = holdTime;
    14.     }
    15. }
    16.  
     
  2. mweldon

    mweldon

    Joined:
    Apr 19, 2010
    Posts:
    109
    Yes they do work.

    Turns out the problem was that my event handler script was not attached to "currentOverGob". It was attached to its parent, which happens to be the game camera. For some reason all the other events like OnDrag and OnPointerDown would fire but not my custom event. Which is strange because it seems like none of the events should have be firing at all.

    I moved the handler script to the collider object, now everything works fine.
     
  3. alestane

    alestane

    Joined:
    Sep 26, 2014
    Posts:
    13
    Depending on the desired behavior of your input module, you may need to use ExecuteHierarchy instead of Execute. This will walk up to parents looking for handlers, where Execute searches only the specified object.
     
    Fattie likes this.