Author Topic: Fake HoverIn  (Read 6138 times)

salepate

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Fake HoverIn
« on: August 06, 2014, 10:31:05 AM »
Hi,

similar to this message : http://www.tasharen.com/forum/index.php?topic=4721.msg22743#msg22743

I am making a menu appear right below the mouse, and intend to make it disappear when mouse hovers out, unfortunately this wont work. HoverOut event does not get triggered.
I tried making a similar edit to the above mentioned thread :

  1.                 UICamera.hoveredObject = gameObject;
  2.                 gameObject.SendMessage("OnHover", true, SendMessageOptions.DontRequireReceiver);
  3.  

Unfortunately this does not work! I mean this does indeed fire the event, but then it won't fire the Hover Out event when I get out of the collider. Possibly because private UICamera.mHover is still set to null. Had I access to this variable this would be different, but I'm sure this would lead to NGUI malfunctions
Any help is appreciated
Regards,
« Last Edit: August 06, 2014, 10:44:04 AM by salepate »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fake HoverIn
« Reply #1 on: August 07, 2014, 01:42:37 AM »
This shouldn't be necessary at all. Hover checks are periodic, they happen in update. Your newly opened menu should get the event. I just did a simple test:

1. ALT+SHIFT+S, ALT+SHIFT+C.
2. Another ALT+SHIFT+S, ALT+SHIFT+C covering the first one. Added UIButton to it to see hover working.
3. Attached this script to #1:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         public GameObject go;
  6.         void OnClick () { NGUITools.SetActive(go, true); }
  7. }
4. Referenced 'go' to be the sprite from #2 and disabled that game object.

When I hit Play and click on the visible sprite, it makes the second one appear and it immediately gets highlighted because it gets OnHover handled by the button. Did you perhaps forget to add a collider to your covering menu?

salepate

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fake HoverIn
« Reply #2 on: August 07, 2014, 02:37:33 AM »
Hi,

I'm using a UIEventTrigger for detecting hovers, but that's not the problem anyway. I do have a collider set properly. So I did some tests.

when I activate the gameObject, the UIEventTrigger indeed fire the "OnHoverOver" event as soon as possible. But it will never fire the "OnHoverOut" event when my cursor gets out of the collider.

Sadly, I need the OnHoverOut event to hide the menu, the OnHoverOver would serve me no purpose here.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fake HoverIn
« Reply #3 on: August 07, 2014, 04:02:20 AM »
There is no such thing as OnHoverOver or OnHoverOut in NGUI.

OnHover(bool isOver) has a boolean parameter that tells you whether you've hovered over the object or out.

Edit: I realized you're using UIEventTrigger, so I'm guessing by OnHoverOut you mean the actual event delegate inside. If that's the case, then look inside UIEventTrigger. OnHoverOut delegate is called from inside OnHover (bool isOver), and the event does fire properly in my test example, otherwise the button wouldn't get un-highlighted when I hover away.

salepate

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fake HoverIn
« Reply #4 on: August 07, 2014, 07:22:00 AM »
What if this was caused by your 2nd sprite which trigger the first one?

I tried recreating your example, a simple sprite with a hover texture. But instead of making the NGUI Sprite appear by clicking another NGUI button, I simply created a game object with this Update routine :
  1.         void Update () {
  2.                 if (Input.GetMouseButtonDown(0))
  3.                 {
  4.                         NGUITools.SetActive(sprite, true);
  5.                 }
  6.         }
  7.  

The sprite then appear below my cursor, and this time, none of the Hover events get fired!

My point is, in your case, the hover event was triggered because you were moving from the button collider to the "hover" collider. you have 2 colliders which allow HoverEvents to be fired properly. Maybe this scheme might help :


Also, I am using 3.6.8


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fake HoverIn
« Reply #5 on: August 08, 2014, 07:24:56 AM »
Hover events don't fire when the mouse or touch is active. You get drag events instead.

Try changing the "Drag Over" state on the covering button to "Press", and you will see it get pressed. Note that it's pressed, not hovered. You aren't hovering when you're holding a button.

salepate

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fake HoverIn
« Reply #6 on: August 08, 2014, 07:59:37 AM »
I think I'm being misunderstood here.

I have made a short video to explain further the problem : https://www.youtube.com/watch?v=mTnlYaKLUlU

I am not keeping the mouse pressed or whatsoever. Here's the toggle script that i use in the video :
  1. public class TestScript : MonoBehaviour {
  2.         public GameObject sprite;
  3.  
  4.         private bool toggle = false;
  5.  
  6.         // Use this for initialization
  7.         void Start () {
  8.        
  9.         }
  10.        
  11.         // Update is called once per frame
  12.         void Update () {
  13.                 if (Input.GetMouseButtonDown(0))
  14.                 {
  15.                         toggle = !toggle;
  16.                         NGUITools.SetActive(sprite, toggle);
  17.                 }
  18.         }
  19. }
  20.  

the NGUI is as you said :
1. ALT + SHIFT + S
2. ALT + SHIFT + C
3. Add a UI Button with a hover texture.
4. That's all.

Quite simple isn't it? and I am not even using EventTrigger here, cause a simple UI Button will fail to behave the way its expected. (or maybe its supposed to not work in this case? You tell me)

Regards

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Fake HoverIn
« Reply #7 on: August 08, 2014, 09:04:27 AM »
You're bypassing NGUI's events by querying Input yourself, so you're leaving the widget in a bad state in this case (it never gets the button down).

Change GetMouseButtonDown to GetMouseButtonUp instead, and everything will work as expected.

salepate

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Fake HoverIn
« Reply #8 on: August 08, 2014, 09:33:32 AM »
I see, this indeed work with GetMouseButtonUp()

Sadly I'm not given the choice, as I was transmitting events from another framework to NGUI. I thank you for your answers.

So as a final solution, I'll use the double collision system to make it work in my case.

Regards, thank you again.