Author Topic: UITable aligning child widget the wrong way  (Read 5546 times)

Chris_Entropy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
UITable aligning child widget the wrong way
« on: January 19, 2014, 09:28:55 AM »
I have a problem with UITable. I am trying to arrange some widgets with the table, that are instantiated at runtime.
My object setup is something like this:

UI Root
|-UI Widget (as a container)
     |-Scroll View
           |-Table
                |-Label
                |-UI Widget (as a Container)
                      |- Button (Instantiated)
                      |- Button (Instantiated)
                      |- Button (Instantiated)
                      |- ...

The buttons are at the right position, but the label is completely off several thousand units. The strange thing is that the buttons are at the correct position relatively to the the label, as if the label was at the correct position.
On another note, the labels of the buttons are invisible, although there is no other Sprite to overlap them.
Do the table child objects need some kind of "table content" component I don't know of? How are they sorted?

Any ideas?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable aligning child widget the wrong way
« Reply #1 on: January 20, 2014, 12:54:23 AM »
They are arranged alphabetically if you turn on sorting on the table. Make sure the container actually covers all of your buttons. You can also remove the container widget altogether.

Chris_Entropy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UITable aligning child widget the wrong way
« Reply #2 on: January 20, 2014, 02:03:56 AM »
Ok, but somehow the label in this structure is placed several thousand units away from the buttons. It is totally not where I want to have it ;-)

I think that I am doing something profoundly wrong, but I can't tell what it is. Please help me!
« Last Edit: January 20, 2014, 03:22:24 AM by Chris_Entropy »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable aligning child widget the wrong way
« Reply #3 on: January 20, 2014, 07:11:18 AM »
Without seeing the scene view there isn't much I can do here. All I can do is guess, and say that all your buttons end up pushing the label. Table simply takes the dimensions of the children's content when positioning things.

Also... pivot point of everything should match. So if the label uses top-left, make sure that your container also uses top-left, and all buttons go down and to the right.

Chris_Entropy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UITable aligning child widget the wrong way
« Reply #4 on: January 20, 2014, 11:45:39 AM »
I was able to reproduce the strange behaviour in a test project. Attached is a unity package with the setup, including image files, scripts, prefabs and a scene. Not included are the NGUI files, so it has to be added to a new project with NGUI.
The whole thing should look like the attached Screenshot, but if you start it you will see that there is only the background sprite, since all elements are somewhere off screen.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable aligning child widget the wrong way
« Reply #5 on: January 21, 2014, 01:49:38 AM »
Never use Instantiate.

Use NGUITools.AddChild(parent, prefab).

Look inside that function to figure out why.

Your objects were instantiated with a huge scale, and in the middle of nowhere.
  1.                 int maxInfo = Random.Range(1, 3);
  2.                 for (int i = 0; i < maxInfo; i++)
  3.                         NGUITools.AddChild(buttonParent.gameObject, infoPrefab);
  4.  
  5.                 int maxButton = Random.Range(4, 6);
  6.                 for (int i = 0; i < maxButton; i++)
  7.                         NGUITools.AddChild(buttonParent.gameObject, buttonPrefab);

Chris_Entropy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UITable aligning child widget the wrong way
« Reply #6 on: January 21, 2014, 04:06:52 AM »
Never use Instantiate.

Use NGUITools.AddChild(parent, prefab).

Thank you a thousand times, this was exactly what I was looking for :) I will recommend your tool on behalf of your support alone :)
Is there a way to give you a thumbs up or something?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable aligning child widget the wrong way
« Reply #7 on: January 21, 2014, 04:21:01 AM »
A positive review is always the best way to give a thumbs up :)