Hello Guest

Author Topic: Button Colour on event?  (Read 6289 times)

bochicoine

  • Newbie
  • *
  • Posts: 15
    • View Profile
Button Colour on event?
« on: October 22, 2013, 09:35:13 am »
Greetings and apologies if this is already answered elsewhere in the forums.

As of now I am now using 2.2.3 - up to date, verified integrity.

What is the best method for changing the colour of a button sprite on an event (i.e. MouseDown)? Or at least swapping out the sprite with another one duging the event...

Thanks,

BC
« Last Edit: October 22, 2013, 09:43:55 am by bochicoine »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Button Colour on event?
« Reply #1 on: October 22, 2013, 10:10:47 am »
Check the UIUpDownHoverButton component on the checkbox, or the draggable button in demo scene #1. Both have sprite switches during an event.

bochicoine

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Button Colour on event?
« Reply #2 on: October 25, 2013, 11:00:17 am »
I've gotten it working, but there seems to be an error when the button is pressed at runtime.

Quote
Coroutine couldn't be started because the game object 'ButtonGraphic_down' is inactive!

In this case, I have two sprites setup for each state of the button... ButtonGraphic_down is just the sprite for when the button is pressed.

I'm using UIUpDownButton as no hover is needed.

Any ideas why the error might be occurring?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Button Colour on event?
« Reply #3 on: October 25, 2013, 12:41:13 pm »
Have you set up the hierarchy in exactly the same way? This looks like perhaps the behaviour is attached to the button graphic_down object or is a child of it.

bochicoine

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Button Colour on event?
« Reply #4 on: October 25, 2013, 12:56:42 pm »
The script is attached to the parent object (in this case BasicButton.)

like this:

BasicButton <--- script is held in here
    SlicedSprite_up
        text
    SlicedSprite_down
        text

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Button Colour on event?
« Reply #5 on: October 25, 2013, 01:37:28 pm »
Do you have a UIIItemTween script attached to the button graphics?

bochicoine

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Button Colour on event?
« Reply #6 on: October 25, 2013, 02:46:44 pm »
Yes, as I copied the original button graphic that came with the demo Button and just renamed it. The whole thing is held inside the Camera... as that seems to be the only way Anchors work:

Camera
   Anchor
      BasicButton <--- script is held in here
          SlicedSprite_up
             text
          SlicedSprite_down
             text

I'm surprised this hasn't come up as an issue before, considering a button rollover is such a common thing to have in UI's. In the past I used the nGUI plugin which had similar problems showing/hiding objects in Unity (in soem cases it wouldn't work at all) so I'm dubious as to whether 2dTK is actually to blame here. Truth be told, it does work fine, it's just that those errors could be a problem on some devices when the game is released, so I'd like to deal with them now if possible.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Button Colour on event?
« Reply #7 on: October 25, 2013, 03:03:11 pm »
Ok.
That all makes sense now.
That error actually does mean something - you won't be able to see the scaling effect when it happens, but otherwise should be harmless.

How you want to set this up is -

Button (tk2dUIItem, tk2dUISound, tk2dUIUpDownButton)
   GraphicRoot - tk2dUITweenItem
        SlicedSprite_Up + text (Remove UITweenItem if copied from basic sprite)
        SlicedSprite_Down + text (Remove UITweenItem if copied from basic sprite)

or

Button (tk2dUIItem, tk2dUISound, tk2dUIUpDownButton)
   GraphicRoot - tk2dUITweenItem
        SlicedSprite_Up
        SlicedSprite_Down
        Text - use this method if text doesn't need to change between up and down

Since its swapping between the up and down states, the scale tween should be performed by something that does it on both of them and DOESN'T get disabled. I.e. the Root object above. Example attached.


bochicoine

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Button Colour on event?
« Reply #8 on: October 25, 2013, 04:07:17 pm »
Works well, thanks!