Author Topic: UITable and NGUITools.Destroy  (Read 4496 times)

imperso

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
UITable and NGUITools.Destroy
« on: January 01, 2015, 05:26:07 PM »
Hi, Aren, I'm using paid version of NGUI and this is my first question here (so far I have been able to find all answers in existing posts).

I have empty UITable which I fill with prefab like this:

  1. var cardGO = NGUITools.AddChild(CardSelectTable.gameObject, CardPrefab); //Yes, only one object

The UITable settings are set to make its content centered. So I expect the position of inserted GameObject to be 0,0,0.

Instead I get strange non symmetrical shifts in position of inserted GameObject (like 70, -125, 0).

But after I do this

  1.        while (CardSelectTable.gameObject.transform.childCount > 0){
  2.             NGUITools.Destroy(CardSelectTable.gameObject.transform.GetChild(0).gameObject);

and fill the table next time I get position right (0,0,0).
I want to understand what NGUITools.Destroy really does with UITable and do the same thing manually before first instantiation of prefab to eliminate undesired position shifts.
P.S. I don't know it it's important, but at run I always have UITable script disabled (although it's enabled at start and I'm not changing its state in code).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable and NGUITools.Destroy
« Reply #1 on: January 01, 2015, 07:28:56 PM »
You need to call Reposition() on the table after adding new children to it.

NGUITools.Destroy removes the object from the parent's hierarchy before destroying it. You have the source code, just look inside that function. Regular Object.Destroy doesn't remove the item, so going through the list of transform's children will still show it as being there.

imperso

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: UITable and NGUITools.Destroy
« Reply #2 on: January 02, 2015, 05:49:27 AM »
Thanks for the answer. I don't use regular Object.Destroy so it's not the case.

Well, when I tried to call Reposition() my object is never centered.
What leads me to conclusion that I'm expecting from UITable some behavior I shouldn't.
When I add a single object to the table with settings like on screenshot above - what object position I supposed to get if I'm doing all the things right? Should the object local position in table be (0,0,0) or not?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable and NGUITools.Destroy
« Reply #3 on: January 02, 2015, 10:03:21 PM »
Just by instantiating a prefab via NGUITools.AddChild, local position will be reset to (0, 0, 0). It's a part of NGUITools.AddChild -- just look inside that function.

imperso

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: UITable and NGUITools.Destroy
« Reply #4 on: January 03, 2015, 03:14:53 PM »
Just by instantiating a prefab via NGUITools.AddChild, local position will be reset to (0, 0, 0). It's a part of NGUITools.AddChild -- just look inside that function.
I always get the object non-centered no matter if I comment "t.localPosition = Vector3.zero;" in NGUITools.AddChild or not and no matter if I call Reposition() or not (although Reposition() gives me slightly smaller offsets).
Looks like UITable just overrides it for some reason and I can't figure what property of initiated object could cause it (obviously it's not prefab initial position as it's 0,0,0).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITable and NGUITools.Destroy
« Reply #5 on: January 04, 2015, 07:40:35 AM »
You yourself said that the table is disabled, so I am not sure why it would be changing anything. Have you tried removing the table to see what happens?

imperso

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: UITable and NGUITools.Destroy
« Reply #6 on: January 04, 2015, 04:25:18 PM »
You yourself said that the table is disabled, so I am not sure why it would be changing anything. Have you tried removing the table to see what happens?
If I instantiate my child object in an empty GameObject with UITable component disabled I get it centered.
So I'm sure that it's UITable what causes offset.
What is more interesting that UITable component gets disabled somewhere in the middle of the process.
(In the middle because otherwise NGUITools.AddChild will get an exception while trying to add something in disabled UITable (declared in script as UITable type)).

imperso

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 16
    • View Profile
Re: UITable and NGUITools.Destroy
« Reply #7 on: January 07, 2015, 06:01:45 PM »
Well, in case anyone interested I gave up and switched to UIGrid and so far it works.
More code to write but at least no unexplainable behavior.