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

Correct OnPointerDown and OnPointerUp implementations in JS?

Discussion in 'UGUI & TextMesh Pro' started by shivansps, Jan 21, 2015.

  1. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Hi guys, what i need is to know when im clicking over an UI element or not, i used to use hotcontrol with the legacy system, still was bad because it only worked with buttons.

    With the new UI it seems that OnPointerUP/Down callbacks do exactly what i need and i came across with scritp that do exactly what i need

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class PointerDownTracker : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    5. {
    6. public static bool pointerDown = false;
    7.  
    8. public void OnPointerDown(PointerEventData eventData)
    9. {
    10.     pointerDown = true;
    11. }
    12.  
    13. public void OnPointerUp(PointerEventData eventData)
    14. {
    15.     pointerDown = false;
    16. }
    17. }
    That works, but i cant use it because my other scritps are JS, and i need to able to change that value on another JS scritp instead of having it in the scritp itselft.

    So i tried to use the documentation to do it in JS, but it does not work.
    What i did is something like this in JS

    Code (JavaScript):
    1.  
    2. private var Mouse : MouseControl;
    3. function Start ()
    4. {
    5.     var cam=GameObject.Find("MainCamera");
    6.     Mouse=cam.GetComponent(MouseControl);
    7. }
    8.  
    9. public function OnPointerDown(eventData: UnityEngine.EventSystems.PointerEventData)
    10. {
    11.     Mouse.MouseOnUI = true;
    12. }
    13.  
    14. public function OnPointerUp(eventData: UnityEngine.EventSystems.PointerEventData)
    15. {
    16.     Mouse.MouseOnUI = false;
    17. }
    18.  
    but that does not work, it wants to use UnityEngine.EventSystems.PointerEventData, that seems ok, but it does not work, and i dont get it.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    You do need to convert everything; leaving stuff out causes it to not work.

    Code (javascript):
    1. import UnityEngine.EventSystems;
    2.  
    3. class PointerDownTracker extends MonoBehaviour implements IPointerDownHandler, IPointerUpHandler
    4. {
    5.     static var pointerDown = false;
    6.      
    7.     function OnPointerDown(eventData : PointerEventData)
    8.     {
    9.        pointerDown = true;
    10.     }
    11.      
    12.     function OnPointerUp(eventData : PointerEventData)
    13.     {
    14.        pointerDown = false;
    15.     }
    16. }
    --Eric
     
    SimonDarksideJ likes this.
  3. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Strange, it says there is an error so it cant add the script, but it does no say what error and i dont see anything wrong on it.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    I guess in one of your other scripts.

    --Eric
     
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
  6. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Is this one, im sure.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No, because I tried it and it works fine.

    --Eric
     
  8. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Interesting, maybe its one of these times i need to restart the pc because Unity is doing some wierd thing, because the editor does not gives any errors, but as soon as i launch it says "The referenced script on this Behaviour is missing!", And is this script thats "missing".

    Out of curiosity, there is another way to check when clicking over the new UI?
     
    Last edited: Jan 22, 2015
  9. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    On man im feeling so silly now, i changed the name of the script, since im not used to use either c# where this has to be done all the time and not used to use JS classes, so i had a different class and script name, thats why it did not work, it works now, thanks.

    EDIT: the only strange thing it does not work with child buttons inside the UI, i have to attach it to the Canvas AND every button inside that canvas.
     
    Last edited: Jan 23, 2015
  10. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Correct, yes.
    When you add a button to a scene (or any UI control), you have to manually parent that in a chain under a canvas through script.
    The Unity Editor scripts for adding UI are a great example of this in the source
     
  11. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    mmmm i found a very weird thing with this, it could be a bug, it seems that "OnPointerDown" in a child does not like to work with a parent gameobject that is using a Event Trigger for "OnPointerDown" or click .

    I found this while converting my old physics based unit selection to the event system, the problem is, the problem is, even if i keep my mouse button down, as soon as i move the mouse that is tracked as "OnPointerUp" on childs, and im 100% sure it can be only be the event system because if i remove the component and just use my multi selection system (still physics based), that does not happen, and both systems execute the same functions.


    EDIT: and it works well if i also use a event system component on child to execute those, it only does not work if i do the "onpointerdown" on script.
     
    Last edited: Feb 16, 2015
  12. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Yeah it definately its some kind of bug with the "OnPointerDown" when used in the script, setting it in a event system component works ok.
     
  13. Undertaker-Infinity

    Undertaker-Infinity

    Joined:
    May 2, 2014
    Posts:
    112
    I'm seeing this issue now... both OnPointerDown and OnPointer up are flakey when used with a ScrollRect.
    They respond every now and then.

    I'm using OnInitializePotentialDrag and OnEndDrag in the meantime, but this means I don't catch a touch release
    that didn't trigger a drag.

    That's no good.