Author Topic: Hitting UI Root  (Read 23705 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hitting UI Root
« Reply #15 on: June 29, 2015, 07:03:42 AM »
It seems your issue then lies in you not using NGUI properly. Whatever custom input you have going on, needs to go through NGUI. Note how UICamera has various callbacks, such as UICamera.onCustomInput? This is what you should be using to set your custom input sources. Look at UICamera.ProcessFakeTouches for an example.

You can also override UICamera's GetKey, GetAxis, GetKeyUp, GetKeyDown functions if your input comes from an alternate source, such as the InControl plugin.

I'm not sure how the hoveredObject ever worked properly for you to begin with if you weren't letting NGUI handle your input code.

While you can install an older version of NGUI (and Pro users have access to the entire Github repository with all the history), I would strongly advise you to correct the issue properly rather than hacking around it and only making it worse.

OnDragOut must always fire if OnDragOver was fired. Otherwise the object will always be in the "drag over" state even when the event ends.

In your generic OnDragOver simply record what you're currently over, if you prefer. Then simply use this value in your function the same way you were using hoveredObject. You don't even need to change to null it in OnDragOut.
  1. using UnityEngine;
  2.  
  3. public class HoveredObjectHelper : MonoBehaviour
  4. {
  5.     static public currentObject;
  6.  
  7.     void Awake ()
  8.     {
  9.         UICamera.onDragOver = MyDragOver;
  10.     }
  11.  
  12.     void MyDragOver (GameObject go, GameObject obj)
  13.     {
  14.         if (go != null && obj != null) Debug.Log(obj.name + " dragged over " + go.name);
  15.         currentObject = go;
  16.     }
  17. }

stevej

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Hitting UI Root
« Reply #16 on: June 30, 2015, 04:21:31 AM »
Okay, thanks! Slowly getting there. Drag and drop is now sorted out (i.e. the "Drop" part is sorted).

Still a bit of a problem with the drag over part though (for triggering tooltips for example).

Here's some code:

  1.     private void Awake()
  2.     {
  3.         UICamera.onDragOver = CustomDrag;
  4.     }
  5.  
  6.     private void CustomDrag(GameObject go, GameObject obj)
  7.     {
  8.         if (go != null)
  9.             Debug.Log(go.name);
  10.     }
  11.  

Problem is, when the mouse cursor ENTERS the object it prints "UI Root" to the console. When I then move OUT of the object, I get the name of the object as I was expecting to get when dragging IN (i.e. what I'd expect from a DragOut, not a DragOver).

Can you check this quickly and make sure it's not a bug. It definitely seems to be to me.

stevej

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Hitting UI Root
« Reply #17 on: June 30, 2015, 04:29:02 AM »
Check out this video - watch the console in the bottom-right corner.

The code above is what's in place.

http://www.stevejarman.com/temp/ngui_dragover.mp4

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hitting UI Root
« Reply #18 on: July 02, 2015, 08:50:55 PM »
I'll have another look at it in the morning...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hitting UI Root
« Reply #19 on: July 03, 2015, 11:24:38 AM »
Had another look, works as expected.

1. Opened Example 11.
2. Attached this script to UIRoot:
  1. using UnityEngine;
  2.  
  3. public class HoveredObjectHelper : MonoBehaviour
  4. {
  5.     static public GameObject currentObject;
  6.  
  7.     void Awake ()
  8.     {
  9.         UICamera.onDragOver = MyDragOver;
  10.     }
  11.  
  12.     void MyDragOver (GameObject go, GameObject obj)
  13.     {
  14.         if (go != null && obj != null) Debug.Log(obj.name + " dragged over " + go.name);
  15.         currentObject = go;
  16.     }
  17. }
  18.  
3. Hit Play, dragged an item -- saw messages "Item X dragged over Y". Exactly what I was expecting to see.

What did you do differently?

stevej

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Hitting UI Root
« Reply #20 on: July 04, 2015, 07:07:22 PM »
What did you do differently?

...good question :(

stevej

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 117
    • View Profile
Re: Hitting UI Root
« Reply #21 on: July 04, 2015, 09:17:43 PM »
Okay - I've gone full circle. onDragOver is all good now. onDragOut all good. onDragEnd... not so good. The event doesn't fire.

I've now got all this stuff set up on the UiButton itself using an Event Trigger component. For the "On Drag End" I have a bit of code that just does a Debug.Log, but nothing gets printed when I release the mouse button while hovered over the UiButton.

The "On Drag Over" and "On Drag Out" events both fire - I have them showing and hiding a tooltip and that's working.

Sorry to be such a pain...

Just a random thought, I think there were some changes to Input Manager in Unity 5.1 maybe? Is there any chance that's having an effect here?


EDIT: All sorted. Worked around the problem with OnDrop, some flags, and some duct tape.
« Last Edit: July 06, 2015, 06:20:21 AM by stevej »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Hitting UI Root
« Reply #22 on: July 06, 2015, 08:11:20 PM »
OnDrop fires on the object underneath the drag event. OnDragEnd fires on the dragged object.