Author Topic: Instanced Prefab UIGrid/UILabel elements going crazy  (Read 4121 times)

stapledon

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Instanced Prefab UIGrid/UILabel elements going crazy
« on: February 13, 2014, 05:59:07 PM »
When setting up my UIGrid (also trying UITable) in a vertical ScrollView, I've done so with prefabs of the items that will fill it, which are instantiated at runtime, and the instances are re-parented to the Grid/Table.

When I run this, the vertical UIGrid and UITable both exhibit the same behaviour: the instances are way out of position and scale (see attached: "Container -1" is the prefab, "Score Entry 1" is the instance).

I've done this plenty times with a horizontal grid, no problems, and can't see anything different other than these prefabs have a Widget at the top level (I normally use a sprite). I understand widgets with dimensions are fairly new - are they ok to use like this?

edit:
Tested with UISprite prefabs instead of UIWidgets, same result.
Also tested with the UITable outside the ScrollView - it doesn't appear to be UIScrollView related.

Cheers




« Last Edit: February 14, 2014, 04:09:00 AM by stapledon »

stapledon

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Instanced Prefab UIGrid/UILabel elements going crazy
« Reply #1 on: February 14, 2014, 05:19:28 AM »
I'm able to repeat this in an empty project with just NGUI 3.49

Add a UITable and a Sprite to UI Root, place several copies of the sprite in the UITable, run... all works.

Duplicate the Sprites while running via hierarchy and call UITable.Reposition() and it works as expected.

Instantiate the Sprites and attach them to the UITable, and call UITable.Reposition() and the new items are attached with a scale of 384.

Manually setting the localscale to 1, then calling Reposition() is a workaround.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Instanced Prefab UIGrid/UILabel elements going crazy
« Reply #2 on: February 14, 2014, 05:45:25 AM »
You can't instantiate things in the middle of nowhere and then reparent them. Your scales will be WAY off.

You need to use NGUITools.AddChild(parent, prefab) to create items. Doing so properly resets the scale.

stapledon

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Instanced Prefab UIGrid/UILabel elements going crazy
« Reply #3 on: February 14, 2014, 06:01:47 AM »
Ah heck I can't believe I've missed this; guess I've just been plain lucky to date. Thanks for the correction.