Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Event System. Why not OnPointerMoved?

Discussion in 'UGUI & TextMesh Pro' started by eelstork, Dec 17, 2014.

  1. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    I tried using the new event system to implement targeting using the mouse pointer.

    So, there is OnPointerUp, OnPointerDown, OnPointerClicked. Why not OnPointerMoved? Or is there something that stands for it?
    I understand that the new system is extensible (pending improvements to the manual and/or tutorials).
    I understand that processing OnPointerMoved may be more intensive.

    I have a very simple use case. Targeting objects with the mouse pointer. A character on the screen is holding a gun and aiming at said objects. So, while the mouse is moving (whether button pressed or not) some feedback is needed to update animation state.

    Of course it's possible to do this the old way, using Physics.Raycast. But still...

    Aside, the process I describe here, about how to setup for interaction with 3D objects...

    http://unibbl.tumblr.com/post/105269678931/unity-4-6-eventsystem-and-raycaster

    ...is not intuitive. imho all this should work out of the box.
    If Unity allowed the programmer to instantiate separate scene-graphs and/or physics worlds, or create objects outside of the main scene graph, well that much boiler plate to process clicks and touches on 3D objects would be reasonable. But it doesn't, so I feel that this stands out a little too much. Allowing advanced users to disable this stuff to substitute a custom setup, instead of forcing everybody to add this and that, would be nice.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Here's a way to make your custom event without having to depend on raycast:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using System.Collections;
    4.  
    5. public class PointerController : MonoBehaviour {
    6.  
    7.     //Custom Event Class
    8.     public class PointerMovementEvent : UnityEvent<Vector2> {};
    9.     public static PointerMovementEvent OnPointerMove = new PointerMovementEvent();
    10.  
    11.     Vector2 lastPointerPosition;
    12.  
    13.     void Start(){
    14.         lastPointerPosition = Input.mousePosition;
    15.  
    16.         //Add an example listener to our move event
    17.         OnPointerMove.AddListener((Vector2 delta)=>{
    18.  
    19.             Debug.Log ("Pointer moved : "+delta.ToString());
    20.  
    21.         });
    22.     }
    23.  
    24.     void Update () {
    25.      
    26.         Vector2 pointerDelta = (Vector2)Input.mousePosition - lastPointerPosition;
    27.  
    28.         lastPointerPosition = Input.mousePosition;
    29.  
    30.         //If the magnitude of the pointerDelta is larger than zero, the pointer must have been moved
    31.         if (pointerDelta.magnitude > 0f) { /*Or epsilon, or whatever */
    32.  
    33.             OnPointerMove.Invoke(pointerDelta);
    34.  
    35.         }
    36.     }
    37.  
    38. }
    Just place this on any game object (I usually put my UI Controller classes on my camera) and when you need listeners attached, you can add them through the public static variable.
     
  3. eelstork

    eelstork

    Joined:
    Jun 15, 2014
    Posts:
    221
    Thanks for your input. Consistent with other pointer events the idea is to receive events while the mouse is hovering above a specific 3D object; this of course cannot be done without Raycast, whether manually or via the new event system.

    I wrote my own code for this anyway, surely we can patch till kingdom come.

    The problem that I hope can be solved is that the aforementioned API should be added to supported events in the standard kit. Since it's still in beta hopefully this can be fixed.
     
    esbenjon and xdegtyarev like this.