Author Topic: 3.0.9 f4 Creating a UIWidget with no UIRoot screws up layers and scale  (Read 2022 times)

AtomicBob

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 69
    • View Profile
When creating or enabling a UIWidget in a hierarchy that doesn't contain a UIRoot, it creates one and re-parents the hierarchy under it. This causes problems. We don't use a UIRoot because we do some more complicated scaling the results in pixel perfect UIs on a couple target devices, and then scales to fit based on both width and height on other devices. We also have a couple of stand alone colliders that intercept and process events. I've added UIWidgets to these colliders so it gets sorted correctly for events. The problem is, when the UIWidgets on these colliders are enabled (i.e. when the GameObject is enabled), they basically destroy hierarchy. There are two outcomes. 1) The widget doesn't find a UIRoot on the root object, so it creates a new UI and re-parents the current hierarchy under it. Problem is, the re-parenting resets all layers in the game to Default. We use a couple layers for masking events and this breaks all interaction. 2) The widget finds a disabled UIRoot on the root object and resets it's scale to 1,1,1 for whatever reason. Our custom scaling script was originally on the root object (effectively replacing UIRoot with a custom one) so this object has a specific scale set. Resetting the scale blows up the game.

I ended up setting up the hierarchy with a disabled dummy UIRoot as the root object, the GameScaler under that, then the UICamera under that. Like so:

  • Root (disabled UIRoot)
    • GameScaler (GameScaler)
      • Camera (UICamera)

And this doesn't break things.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: 3.0.9 f4 Creating a UIWidget with no UIRoot screws up layers and scale
« Reply #1 on: January 24, 2014, 09:26:38 AM »
If you use NGUITools.AddChild to instantiate your widget, then you can add it anywhere, as long as it's under a UIPanel.

You have to have a UIPanel in order for things to draw.

UIRoot is created for you if you have no panel.

AtomicBob

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 69
    • View Profile
Re: 3.0.9 f4 Creating a UIWidget with no UIRoot screws up layers and scale
« Reply #2 on: January 24, 2014, 09:31:36 AM »
This actually happens in the Editor as well as at runtime. Specifically, I have a static UIWidget on a GameObject that starts out disabled (it's not added at runtime, so I can't really use NGUITools.AddChild). As soon as the object is enabled, it creates a new UIRoot. It's not under a UIPanel because it's just a collider for catching events. It only has a UIWidget on it to prevent that weird depth issue. It doesn't draw anything, so it has no real need to be under a UIPanel.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: 3.0.9 f4 Creating a UIWidget with no UIRoot screws up layers and scale
« Reply #3 on: January 24, 2014, 09:32:54 AM »
All widgets need a panel to manage them. They can't exist without a panel, it's as simple as that.