Hello Guest

Author Topic: UI events not responding correctly  (Read 5262 times)

enervation

  • Newbie
  • *
  • Posts: 2
    • View Profile
UI events not responding correctly
« on: July 29, 2013, 05:00:45 am »
Hello,

I'm having difficulties with the UI event system. Mainly, when I press down on an object with a UIItem component, both the OnDown and OnHoverOut events are sent, even when I do not move the cursor after pressing down on the object. I can see the boundaries of the box collider on the object, and it is definitely a large enough area to keep the cursor in.

Also, does OnRelease return the UIItem that was originally sent to OnDown, or does it return the UIItem that is currently under the cursor? It seems it's the former. I am trying to create a drag-n-drop effect, but there seems to be no way of getting the current UI object that's under the curse when you release the mouse button (even OnHover stops sending events if the mouse button in pressed down).

Thanks in advance for your help!

gary-unikronsoftware

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 74
    • View Profile
Re: UI events not responding correctly
« Reply #1 on: July 29, 2013, 11:28:39 am »
Hi,

Yes, the code is working as expected, in that once you click down with the mouse button you are no longer 'hovering'.  Also the OnRelease will return the item you originally clicked down on. 

If you are trying to create a drag-and-drop effect, would it be possible to add something to your script like this (in the Update function): 

      if(Input.GetMouseButton (0))
      {
         Ray ray = gameCam.ScreenPointToRay(Input.mousePosition);
   
         RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity);
         
         Debug.Log(hits.Length);
         foreach (RaycastHit hit in hits)
         {
            // Do something
         }
      }

So basically this will return all colliders the ray hits whilst the mouse button is held down (although the colliders aren't guaranteed to be in any particular order).

enervation

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: UI events not responding correctly
« Reply #2 on: July 29, 2013, 07:21:28 pm »
Hi,

Yes, the code is working as expected, in that once you click down with the mouse button you are no longer 'hovering'.  Also the OnRelease will return the item you originally clicked down on.

I see. I was assuming that the behavior of the event was only triggered by exiting the collider boundaries.

If you are trying to create a drag-and-drop effect, would it be possible to add something to your script like this (in the Update function): 

      if(Input.GetMouseButton (0))
      {
         Ray ray = gameCam.ScreenPointToRay(Input.mousePosition);
   
         RaycastHit[] hits = Physics.RaycastAll(ray, Mathf.Infinity);
         
         Debug.Log(hits.Length);
         foreach (RaycastHit hit in hits)
         {
            // Do something
         }
      }

So basically this will return all colliders the ray hits whilst the mouse button is held down (although the colliders aren't guaranteed to be in any particular order).

This is definitely possible (adding a layer mask to the raycast to make sure it is only the UI returned). However, this defeats the purpose of using the 2dUI event system to interact with UI objects. I was hoping to avoid it.

Thanks for the clarification.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: UI events not responding correctly
« Reply #3 on: July 29, 2013, 11:14:59 pm »
We've been trying to avoid drag & drop so far, but if there is enough interest we'll add an official solution.