Author Topic: Strange behaviour when changing a button's target tween? [SOLVED]  (Read 2435 times)

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
In my inventory system, items could take more than one slot.
A slot prefab has:
1- UIButton script attached to it.
2- A 'background' as a child, which has a UISprite script attached to it.
3- An 'icon' as a child, which also has a UISprite script attached to it.

The way I setup the 'items take more than one slot' system, is that when I place an item in a place where it fits, I take the first slot background and icon and enlarge them to meet the item's dimensions, and for the rest of the slots it took, I disable their background and icon, take a look at the attachments.

But notice, I don't enlarge the collider to meet the item's dimensions, I leave the slots colliders to detect which slot I'm clicking at (I enlarge them a bit just to close down the spaces between the slots which will be left without a collider), this means, if I hover over a slot, other than the top-left one, the item won't highlight! - which is why whenever I add an item to some place, I make all its required slots 'refer to' the top-left one, and when I do that, I change their tween target, so that if I hover over any slot, the background of the top-left slot will get highlighted.

  1. public Slot ReferTo
  2. {
  3.         get { return referTo; }
  4.         set
  5.         {
  6.                 referTo = value;
  7.                 // if I'm referring to myself, my target tween is my background
  8.                 if (referTo == this)
  9.                         _button.tweenTarget = background.gameObject;
  10.                 // else my target tween is the bg of what I'm 'referring to'
  11.                 else _button.tweenTarget = referTo.background.gameObject;
  12.         }
  13. }
  14.  

This all works well, however the problem arises when I pickup/hold the item with my mouse, when I do so, I free the slots that the item has taken (of course, when a slot is free, it 'refers to' itself now, which means its target tween is its background), when I do that, the slot I originally clicked on to add the item, will still have its highlight on! Refer to the "TheProblem" attachment.

PLEASE help me fix this, been struggling for about a week! I can't make progress cuz I'm stuck :((( - Do I have to mess with the UIButton script's code?

Thanks a lot for the help, I would really appreciate it.

« Last Edit: August 19, 2013, 11:58:52 PM by vexe »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Strange behaviour when changing a button's target tween?
« Reply #1 on: August 19, 2013, 09:40:31 PM »
Don't use multiple colliders. Disable individual slot colliders, and use NGUITools.AddWidgetCollider to create a new one over the item. When you pick up the item, get rid of this collider and re-enable the colliders you disabled.

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Strange behaviour when changing a button's target tween?
« Reply #2 on: August 19, 2013, 10:27:45 PM »
That's how I done it before, it worked. But previously I used to detect which slot I'm clicking on via some obscure calculations (finding the dist between the mouse and the top-left item slot, and dividing by slotSize+spaceBetweenSlots to get the index of the slot that I'm clicking on) - but now, with multiple colliders, all that is unnecessary! - I could easily get the index of the slot that I click on because it still has its collider. Please tell me there's another solution :( - Using multiple colliders made me do a lot of things, a LOT more easier...

Note that the highlight goes away again if I hover the mouse again over the slot! - So, there should be a way to, somehow programmatically send a signal or something to the slot, that the mouse is over you, even though it's really not.
« Last Edit: August 19, 2013, 11:05:23 PM by vexe »

vexe

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 153
    • View Profile
Re: Strange behaviour when changing a button's target tween?
« Reply #3 on: August 19, 2013, 11:58:31 PM »
I solved the issue by manually calling OnHover(false) on all the former required slots of the item after I pick it up, I think one must get himself familiar with NGUI's code anyway :)