Author Topic: Getting OnHover and OnClick with bool states through button events?  (Read 1766 times)


  • Full Member
  • ***
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 113
    • View Profile
Components placed on UI objects with colliders enjoy extremely useful messages OnHover and OnPress that can be used to call void methods with isOver and isDown bool arguments. This allows me to easily hook complex animated behavior outside of UIButton scope to buttons, for example.

Unfortunately there is an inconvenience - I'm creating prefabbed UI elements with wrapper components (like ready for use buttons with labels), and I'd prefer the wrapper component to be alone on a parent object, with the label, sprite, collider and other stuff being in the hierarchy under it. Naturally, those neat messages stop arriving to my component when there is no collider on it. As a workaround, I'm trying out subscribing that component on a parent GameObject to the UIButton events, but I'm not sure if the same functionality is supported through that. I'm using the following simple code:

  1. referenceButton.onClick.Add (new EventDelegate (this, "OnPress"));

But as far as I understand, the button can't send that event with bool arguments describing whether a press has started or ended; and there is no hover event at all. Is there any way to get four state input response (hover entered area, hover left the area, click started, click ended) on a remote object or should I abandon use of events altogether and use a collider on a parent to intercept all those messages directly?

Very obvious workaround is to create a component placed onto the child GameObject with UISprite/Collider that will do absolutely nothing but catch OnHover/OnPress messages and relay them to the reference of my wrapper component - but it would be very nice if I could avoid cluttering the project with classes of that sort.


Edit: Oh, I see I have overlooked UIEventListener class.
I'm not sure I follow how it works though. The example contained in the description of that class is not working:

  1. UIEventListener.Get(gameObject).onClick += MyClickFunction;
Error: Operator `+=' cannot be applied to operands of type `UIEventListener.VoidDelegate' and `method group'


Edit 2: I think I got it working, looks like every single method called through needs a GameObject argument in addition to standard arguments like bools described in the event tutorial.
Could it be down to the fact that I'm subscribing on creation of the button in the editor? Maybe it only works when I subscribe ingame?


Edit 3: Yeah that was it, any and all subscriptions should be performed when the game is running.
« Last Edit: October 09, 2014, 05:29:42 AM by bac9 »