Search Unity

an equivalent of OnMouseUp/Down for Touch?

Discussion in 'Scripting' started by lewislepton, Apr 1, 2015.

Thread Status:
Not open for further replies.
  1. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    this is something i have never seen. even checked the new docs. maybe im missing something.

    but i would have thought there would be a function called OnTouchDown, OnTouchUp for well, touch control, the same way mouse control works etc. but is there an equivalent of OnMouseDown/Up for touches?

    thanks for your help
     
  2. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    to make this clearer in a duff function sense

    Code (CSharp):
    1. void OnTouchDown(){
    2.    //do code stuff when finger has touched down on screen
    3. }
    4.  
    5. void OnTouchUp(){
    6.    //do other touch code but when pulling finger off of touchscreen
    7. }
    8.  
    9. void OnTouchDrag(){
    10.    ///do code stuff when you drag your finger. could even be OnTouchMove instead
    11. }
    12.  
    thanks
     
    sean244 likes this.
  3. L-Tyrosine

    L-Tyrosine

    Joined:
    Apr 27, 2011
    Posts:
    305
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    The TouchPhase might interest you. I tend to make my own touch manager that reads Input.touches than check the phase of the touches or do any detection of gestures that I want, than I have this manager fire off some events that other objects can subcribe to.

    Rather simple approach but I like working with C# events for these kinda systems
     
  5. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    oooh thanks a bunch guys, ill have a look see. forgot about TouchPhase. but really would have thought that unity would have something like OnTouchUp/Down/Drag etc. seems to make a bit of sense in my mind, but probably not in everyone elses ;)

    thanks again
     
    Yavuz_Selim likes this.
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    When I get home I could show you how to make a manager script that would allow you go subscribe any object you want to OnTouchDown, and OnTouchUp.

    Though you will have to decide how to handle multiple touches.
     
  7. lewislepton

    lewislepton

    Joined:
    Mar 25, 2015
    Posts:
    34
    that would be more than fantastic.

    touch stuff is just not my first thing really. and without something like say OnTouchUp/Down etc, it just a bit like walking in a forest blindfolded.

    but that would be very nice. thank you
     
  8. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Ya I get home to my main computer in a few hours and can just pull a sample from one of my projects.

    It would help knowing the purpose, ui work or are you trying to do something like raycast to the world when ever there is a touch event?
     
    Tixal likes this.
  9. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    hey mate, little quick and dirty but you could try this.

    put this code on a object in your scene, and make sure there is only one instance of it.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TouchManager : MonoBehaviour
    5. {
    6.    #region singaltonStuff
    7.     private static TouchManager _instance;
    8.     public static TouchManager Instance
    9.     {
    10.         get
    11.         {
    12.             if (_instance == null)
    13.                 _instance = GameObject.FindObjectOfType<TouchManager>();
    14.             return _instance;
    15.         }
    16.     }
    17.     #endregion
    18.  
    19.     public delegate void TouchDelegate(Touch eventData);
    20.     public static event TouchDelegate OnTouchDown;
    21.     public static event TouchDelegate OnTouchUp;
    22.     public static event TouchDelegate OnTouchDrag;
    23.  
    24.     private void Update()
    25.     {
    26.         if (Input.touchCount > 0)
    27.         {
    28.             Touch touch = Input.GetTouch(0);
    29.             if (touch.phase == TouchPhase.Began)
    30.             {
    31.                 if (OnTouchDown != null)
    32.                     OnTouchDown(touch);
    33.             }
    34.             else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
    35.             {
    36.                 if (OnTouchUp != null)
    37.                     OnTouchUp(touch);
    38.             }
    39.             else if (touch.phase == TouchPhase.Moved)
    40.             {
    41.                 if (OnTouchDrag != null)
    42.                     OnTouchDrag(touch);
    43.             }
    44.         }
    45.     }
    46. }
    47.  
    than in the class you want to implemeant your OnTouchDown, OnTouchUp, OnTouchDrag methods in simply subcribe to the events in your Start, Awake or OnEnable Methods with a method that fits the delgate signature.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ReadTouch : MonoBehaviour
    5. {
    6.     private void OnEnable()
    7.     {
    8.         // Subcribe to events when object is enabled
    9.         TouchManager.OnTouchDown += OnTouchDown;
    10.         TouchManager.OnTouchUp += OnTouchUp;
    11.         TouchManager.OnTouchDrag += OnTouchDrag;
    12.     }
    13.  
    14.     private void OnDisable()
    15.     {
    16.         // Unsubcribe from events when object is disabled
    17.         TouchManager.OnTouchDown -= OnTouchDown;
    18.         TouchManager.OnTouchUp -= OnTouchUp;
    19.         TouchManager.OnTouchDrag -= OnTouchDrag;
    20.     }
    21.  
    22.     private void OnTouchDown(Touch eventData)
    23.     {
    24.         Debug.Log("OnTouchDown!");
    25.     }
    26.  
    27.     private void OnTouchUp(Touch eventData)
    28.     {
    29.         Debug.Log("OnTouchUp!");
    30.     }
    31.  
    32.     private void OnTouchDrag(Touch eventData)
    33.     {
    34.         Debug.Log("OnTouchDrag");
    35.     }
    36. }
    37.  
    this should do the job for you, and i added in the Touch eventData so you also get access to information about a touch in the event callbacks.

    Like i mentioned before if you are doing multi touch you would have to decide how to handle extra touchs, such as just makking the manager loop all touches and fire events for all of them, or putting some code in there to handle gestures.
     
  10. strider1983

    strider1983

    Joined:
    Jul 14, 2017
    Posts:
    1
    You saved my game MAN!!!!!
     
  11. tiwari0nath

    tiwari0nath

    Joined:
    May 30, 2020
    Posts:
    1
    Thanks passerbycmc for your wonderfull code, Since OnTouchDown etc.. didn't worked for me but your code did.
    Love you and your code.
    you saved me and my game
     
  12. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Nice necro of an old thread.

    You can implement interfaces that handle these kinds of events.
    IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IDragHandler, IDropHandler
     
  13. MrBeto

    MrBeto

    Joined:
    Jun 12, 2019
    Posts:
    1
    This makes sense, I saw a tutorial on Interfaces but I'm kind of lost.
    Can you please do an example of like IpointerDownHandler to get an idea
     
  14. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    Unity kind of forgot the examples in the new documention. Only IPointerClickHandler has an example there.
    https://docs.unity3d.com/Packages/c...Engine.EventSystems.IPointerClickHandler.html

    Older documentation gives the examples as well:
    https://docs.unity3d.com/2019.1/Documentation/ScriptReference/EventSystems.IPointerDownHandler.html

    You can also use Unity Learn and find information about Interfaces there are a lot of C# scripting tutorials around.
     
  15. ash_jrf

    ash_jrf

    Joined:
    Oct 25, 2019
    Posts:
    2
    Please help me!!
    I want to display name of the object that is touched in the console. Like i have dog, cat, sheep etc. If i touch each animal, its name should be displayed on the console.
    I provided unique tag for each animal. I attached the TouchManager script to these animal objects and ReadTouch script to the LeapHandController. I initially tried with one animal, but touch is not recognized and nothing got printed on console. I do not what to do next. Please help me...
    I am using Leap motion in unity 5.6.
    Thanks.
     
  16. UL_Dan

    UL_Dan

    Joined:
    Aug 5, 2020
    Posts:
    6
    If you wish to interact with objects within Unity using Leap motion hand tracking I recommend importing the Unity Modules, specifically the Interaction Engine.

    The Interaction Engine does some or all of the work for you to allow users to be able to hover near, touch, or grasp objects in your application in some way, the documentation can be found here and the Unity Modules downloaded from here.

    If you are using an older version of Unity, such as 5.6, you can find a list of previous Unity Asset releases here.
     
  17. ash_jrf

    ash_jrf

    Joined:
    Oct 25, 2019
    Posts:
    2
    Thanks for the reply.
    Will try with Interaction Engine
     
  18. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    589
    Ugh. This looked promising and it's still hard to believe that coding an app for mouse/touch doesn't just 'work'. Well, using OnMouseDrag does work for touch, but generates errors and warnings about being deprecated or hurting performance.

    The latest documentation does not seem to have IPointerClickHandler, at leas that I can find:

    https://docs.unity3d.com/2020.2/Documentation/ScriptReference/UIElements.IPointerEvent.html

    I think I will just implement the TouchManager from above, and set it up to implement OnMouse functons in the editor and Touchmanager on the device. Such a common need, why is this so hard for Unity to implement properly?
     
  19. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    The documentation you're referring to is from "UI Elements" which has already been renamed to UI Toolkit.
    And UI Toolkit is a whole different package that uses its own Input System AFAIK.

    It completely depends on what your requirements are. But IPointerDownHandler, IPointerUpHandler and IPointerClickHandler are easy to implement. But the documentation has been reworked since 2019.2. This is due to the fact that uGUI became a package and since 2019.2 refers to the package documentation. It ain't as extensive as the old documentation. The old documentation is still valid though as not much has changed.

    If you need these events on a 3D mesh game object you'll require a Physics Raycaster on your Camera.
    If you need them on a UI game object made with uGUI, they should work already.
    Don't forget to have an event system in your scene (mesh or not). "GameObject/UI/Event System" menu
    I can agree on that the documentation isn't clear but it ain't that hard to implement these events once you know how.
     
    Last edited: Dec 31, 2020
  20. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    589
    Thanks! This got me thinking in the right direction and I ultimately solved my problem.

    Boy howdy though! The documentation on everything is just so confusing to sort through. This is for an AR project and so I thought surely the AR Foundation Samples would have an example implementation using a similar method. They do – the InputSystem_PlaceOnPlane.cs script and uses the ARRaycastManager that's already attached to the camera in my scene. Great!

    Then, after dragging the script into my project I remembered why I didn't already use that example - it uses the New Input system and upgrading an existing project is painful. I was already using the UI Toolkit, so decided to take the plunge and update the Input system.

    Not only do you have to update all your events, you need to create profiles and settings, add an Input manager, double-click various settings and set up the Events. It's quite an elaborate amount of steps and I have no idea where to find the documentation, i.e. UI Elements, Unity UI Packages, etc. The best I could do was to compare the AR Foundation example scene and just hook everything up as they did.

    Painful, but it's all working now and I finally got rid of my OnMouse methods!
     
  21. worapop

    worapop

    Joined:
    Aug 29, 2020
    Posts:
    1
    //Attach this script to the GameObject you would like to have mouse clicks detected on
    //This script outputs a message to the Console when a click is currently detected or when it is released on the GameObject with this script attached

    using UnityEngine;
    using UnityEngine.EventSystems;

    public class Example : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    {
    //Detect current clicks on the GameObject (the one with the script attached)
    public void OnPointerDown(PointerEventData pointerEventData)
    {​
    //Output the name of the GameObject that is being clicked
    Debug.Log(name + "Game Object Click in Progress");​
    }

    //Detect if clicks are no longer registering
    public void OnPointerUp(PointerEventData pointerEventData)
    {​
    Debug.Log(name + "No longer being clicked");​
    }​
    }


    ***********
    I think it's similar to void OnMouseDown() , void OnMouseUp()
     
  22. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,092
    besides necroposting a 2 year old (and already necroposted before) thread, you're posting code without code tags.
    Please use code tags.

    I already previously linked the documentation to
    IPointerDownHandler
    and
    IPointerUpHandler
    which contains the same example that you're posting.
     
    Last edited: Oct 26, 2022
    OrinocoE and Kurt-Dekker like this.
Thread Status:
Not open for further replies.