Author Topic: UIButton overriding color with UIDragDropItem + UISprite  (Read 4535 times)

Wisteso

  • Full Member
  • ***
  • Thank You
  • -Given: 21
  • -Receive: 3
  • Posts: 103
    • View Profile
UIButton overriding color with UIDragDropItem + UISprite
« on: June 08, 2015, 03:05:43 PM »
Hey folks.

NGUI 3.8.0 with Unity 5.0.2

I have a GameObject with UIDragDropItem that also has a UISprite on the game object. I want to change the color of the UISprite depending on the value of UICamera.hoveredObject. For the most part, this works fine.

However, UIDragDropItem also requires UIButton for some reason which has been causing lots of annoyances, including this particular one.

I'm noticing that for the first 10 or so frames of dragging the object, any calls I make to GetComponent<UISprite>().color are being overwritten by the value of GetComponent<UIButton>().disabledColor. This is bad because the disabled color sticks there (forever) if the user moves the UIDragDropItem into position very quickly and then totally stops. If they drag continuously OR slowly the issue doesn't happen.

  1. ...
  2. sprite = GetComponent<UISprite>();
  3. ...
  4.  
  5. protected override void OnDragDropMove(Vector2 delta)
  6. {
  7.         bool isHovering = (UICamera.hoveredObject == unitSelected.gameObject);
  8.  
  9.         // this will be overwritten by GetComponent<UIButton>().disabledColor first few frames of drag
  10.         sprite.color = isHovering ? Color.blue : Color.red;
  11. }
  12.  
Basically I need a way to tell UIButton to leave the colors alone. Better yet, a way to use UIDragDropItem without requiring UIButton at all.



Update:
  1.     protected void OnEnable()
  2.     {
  3.         GetComponent<UIButton>().tweenTarget = null;
  4.     }

The above code serves as a really ugly workaround for the issue. Setting the tween target to null in the inspector does not work - it gets reverted to the UISprite when running. Setting it to null in the OnEnable does work because it is not reverted.
« Last Edit: June 08, 2015, 03:20:32 PM by Wisteso »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton overriding color with UIDragDropItem + UISprite
« Reply #1 on: June 09, 2015, 01:21:34 AM »
You don't need UIButton. It's optional, and is only used by UIDragDropItem to disable the collider. Check the code -- if the UIButton isn't there, it interacts with the collider directly.