Author Topic: Complicated prefab hierarchy including UIAnchors, widgets not rendering  (Read 4823 times)

drjeats

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 13
    • View Profile
I suppose complicated is relative, but it's mixed objects in here. I'm creating a UI prefab and having trouble getting it to render when I instante it via NGUITools.AddChild.

My test scene consists of MainCamera, GUICamera, and a SceneHUD gameobject which contains the UIRoot component.

I'm instantiating the prefab like this:

  1. var go = NGUITools.AddChild(sceneHUD, (GameObject)Resources.Load("RacingHUD"));
  2. foreach (var anchor in go.GetComponentsInChildren<UIAnchor>()) {
  3.         anchor.uiCamera = uiCamera; // this is a ref to GUICamera set in the inspector
  4. }

The prefab spawns, and the transforms of each element looks as though it should, but nothing is rendering.

If I include the toplevel SceneHUD (with UIRoot) in the prefab and instantiate that whole thing, everything works fine.

Is there something about including anchors or non UI-gameobjects in the prefab that I'm not picking up on?

The prefab's hierarchy:

SceneHUD (part of Scene, not in the prefab, the first argument to NGUITools.AddChild)
---RacingHUD (empty G.O.)
------CenteredAnchor
---------Panel
------------CountdownLabel (UILabel)
------TopLeftAnchor
---------Pane
------------FirepowerLabel (UILabel)
------------PrimaryPowerups (empty G.O.)
---------------Bomb (non-UI object with MeshRenderer)
------------SecondaryPowerups (empty G.O.)
---------------Boost (non-UI object with MeshRenderer)
------------powerupBOx (SlicedSprite)
------------powerupBOxActivated (SlicedSprite, with the sprite component disabled with the intentioning of enabling/disabling in code)
---------PowerupSelector (G.O. with display logic script attached)
« Last Edit: June 20, 2012, 03:56:07 PM by drjeats »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
All I can suggest is check your transforms. Your prefab may not be getting instantiated where you think it should be. Also double-check your layers -- make sure that the widget and panels are on proper UI-visible layers. I also suggest adding children underneath your UIAnchor, not have an anchor be present on your prefab. I am not sure why the anchor is necessary in that case anyway. You should only have one center-alighed anchor, and add your HUD stuff underneath it.

drjeats

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 13
    • View Profile
Okay, it's easy to break it up into the two prefabs then.

The reason I had the two different anchors was because I wanted some elements to be anchored center and some anchored top-left. Is this their intended use, or did I assume something that I shouldn't have?

What's weird is that I can pull the prefab (including anchors) out of the project explorer and drop it into the scene hierarchy and everything attaches no problem. Is there some other operation not included in NGUITools.AddChild that is required to hook everything up properly? The gizmos of the child elements seemed to appear in logical positions. I'd need to double check the actual positions but it doesn't look like that's the problem.

If you have any other hints (docs to read, specific code sections to read, etc.) about how the elements get rendered and how the anchors affect I'd really appreciate it. It's not a showstopper since I can have the anchors in the scene already like you suggested, but our team's about to do a massive amount of UI work in the next few weeks and I'm kinda the guy responsible for educating the rest of the team about the intricacies of NGUI. and I can see this question coming up again as we start making UI prefabs.

Oh, and thanks for making NGUI! The team was very happy to switch over from EZ GUI.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
You can leave the anchors as-is, but the prefabs you'll be adding should be added below the anchors, and shouldn't include anchors themselves. For example, your main menu would be added to the center anchor, but your abilities hotbar would be added to the bottom anchor.