Author Topic: UIButton UILabel depth dropping behind UISprite  (Read 4427 times)

ironpencil

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
UIButton UILabel depth dropping behind UISprite
« on: July 19, 2014, 06:42:36 PM »
I have a complex (nice way of saying messy) UI for a game I am trying to make in a month. Here is part of my hierarchy:

UIPanel
---UIWidgetContainer
------UISprite1 (Depth of 12 in Editor)
---------UILabel1 (Depth of 13 in Editor)
------UISprite2 (Depth of 14 in Editor)
---------UILabel2 (Depth of 15 in Editor)

The UISprites and UILabels are the components that automatically get added when I add the UIButton prefab from the toolbar.

I'm experiencing some behavior where the depth of the above components, the sprites and labels, are changing during play, and sometimes (not always) this is causing the UILabel to drop to a lower depth than the UISprite. This appears to happen when I call BringForward() on an object on a completely different panel. It looks like it's probably happening in the NormalizeWidgetDepths() function? But trying to figure out how this is sorting/adjusting is a little beyond me. All I know is I see the UILabel jump from like depth 15 to around depth 52, and then sometimes it ends up behind the UIButton so it's not visible.

Can anyone explain to me in relatively simple terms what's happening here and perhaps how I should be handling this differently? I am creating several objects dynamically (using NGUI to build a card game) so I'm sure there are objects on the same depth and so maybe that is what is causing this clash/re-order? I am fine with the depth of the components changing dynamically, it's only a problem because it's causing the label to get covered by the button.

Thanks,
Jim

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton UILabel depth dropping behind UISprite
« Reply #1 on: July 20, 2014, 06:08:57 PM »
The way widgets are brought forward when you pass a game object to that function in NGUITools, is all widget underneath get a depth of +1000 higher than what they had. So your 12 becomes 1012, 13 becomes 1013 etc. After that all the depths are collected and put in a consecutive order. Since the widgets with 1000+ depth are obviously last, they also end up last when they're sorted.

ironpencil

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIButton UILabel depth dropping behind UISprite
« Reply #2 on: July 20, 2014, 08:42:11 PM »
That doesn't seem to explain the behavior I'm seeing, where widgets unrelated to the one I'm bringing forward has their Depth values changed, even though they are on a different panel. And it definitely doesn't explain the label on a button getting sent behind the button sprite to become invisible. Any idea what might be causing this? I just created the panel and buttons the other day and haven't done any weird scripting on them that I would think would change the depth.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButton UILabel depth dropping behind UISprite
« Reply #3 on: July 21, 2014, 04:52:34 PM »
Have a look at the NGUITools.AdjustDepth function. What it does inside is it first checks -- is there a panel on the game object? If so, then it adjusts the depth of panels, otherwise -- the depth of widgets. In either case it gathers up all the child components underneath then adjusts the depth by the chosen amount (1000 in the example I mentioned). Note that this function currently does not consider child panels, and will adjust their widget's depth as well. I'll change it to ignore child panels just to be on the safe side.

ironpencil

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: UIButton UILabel depth dropping behind UISprite
« Reply #4 on: July 23, 2014, 10:07:40 AM »
Hey just wanted to let you know that I figured out the issue. I put some debug calls in the NormalizeWidgetDepths() function, and what was happening is that this function normalizes only ACTIVE widgets.

The panel I was working with had optional buttons, and when I didn't want a button to display I was setting it to Inactive, which meant when the rest of the panel was normalized those buttons stayed at the same depth. Then later, when the buttons WERE active, their old depth was behind the background sprite's new depth, and they would then get normalized to behind it.

I guess the proper way to make the buttons not visible is to just set their Alpha to 0 and set isEnabled = false.

But for future reference, if anybody is working with widgets that should stay at a certain depth relative to eachother, make sure they are either all active or all inactive at the same time so they don't get normalized to different depths.