Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: electrodruid on March 04, 2013, 09:09:34 AM

Title: NGUITools.AddChild and composite objects
Post by: electrodruid on March 04, 2013, 09:09:34 AM
Hi,

I've got a scene not unlike Example 7 - there's a UIDraggablePanel containing a UIGrid of objects (I'll call these "items") which have a BoxCollider and a UIDragPanelContents, and then a selection of children which are sprites and labels. Additionally, elsewhere on the screen, I've got a number of sprites representing "slots". The idea is that when you click on an item in the draggable panel, it gets duplicated and positioned in an empty "slots" to show that you've selected the item. I thought that something like this would work:

NGUITools.AddChild(slotObjects[freeSlotIndex], selectedItemObject);

i.e. clone selectedItemObject, position the clone in the same place as the appropriate slotObject, and set the slotObject as the parent of the selectedItemObject. This works, but doesn't give the result I wanted or expected, since although the whole composite item is cloned, it gets broken apart. Only the root of the item ends up correctly positioned and parented. All of its child widgets get left behind and just sit in the top of the screen hierarchy, which obviously looks very wrong.

Am I missing something about how AddChild works? Is there an easy way to use it without breaking cloned composite objects apart? Or do I have to write some recursive function of my own to hold these objects together as I move them around in the hierarchy?
Title: Re: NGUITools.AddChild and composite objects
Post by: electrodruid on March 04, 2013, 09:50:01 AM
Seems like I fixed the problem, although I'm still not completely sure what was going wrong. I realised that adding children to the slots was a bad idea if the slots were sprites, since sprites have a scale which would get passed on to the children. Instead, I made a bunch of simpler objects (just a transform and an achor to the slot sprite so they'd be in the right place, but no size or widgets), and I add my cloned items to them instead. Not only does that keep the scaling sane, it also seems to stop the items from breaking apart, which is good.

So, nothing to see here  ;D Except maybe a cautionary tale that you should be careful what GameObjects you want to add children to...