Author Topic: [SOLVED] Scroll View not clipping dynamically added children  (Read 6568 times)

PnzrDrgoon

  • Guest
Hello,

I'm trying to instantiate a prefab multiple times and then add those instances to a grid.  This seems to work just fine through the use of GameObject newItem = NGUITools.AddChild(itemsGrid.gameObject, prefabItem); but the parent draggable panel of itemGrid doesn't clip the new children.  I've searched the forum and have seen it mentioned that the prefab should probably not have it's own panel, but every time I add a label or button or any NGUI object it automatically adds a panel to my prefab.  Could someone please point out where I'm making the mistake?  Here's my layout:

+ ItemPrefab (DragPanelContents linked to ItemsDraggablePanel)
+ -- NameLabel
+ -- StatusSprite
+ -- ImageButton

+ ItemsDraggablePanel (Hard Clip)
+ -- ItemsGrid

The section of code that makes new items and adds them to the grid is:
private void OnGetItemssSuccess(List<Item> items)
    {      
      if( itemPrefab == null )
      {
         Debug.LogError("Could not find ItemPrefab!");//linked through Unity magic
         return;
      }
      
        foreach (Item item in items)
        {
            GameObject newItem = NGUITools.AddChild(itemsGrid.gameObject, itemPrefab);
         
            UILabel[] labels = newItem.GetComponentsInChildren<UILabel>();
            foreach (UILabel label in labels)
            {
                switch (label.name)
                {
                    case "ItemLbl":
                        label.text = item.name;
                        break;
                    case "LevelLbl":
                        label.text = item.level;
                        break;
                    default:
                        break;
                }
            }

            isOnline = item.online == 1;

            UISprite[] sprites = newItem.GetComponentsInChildren<UISprite>();
            foreach (UISprite sprite in sprites)
            {
                switch (sprite.name)
                {
                    case "ItemSprite"://for future use
                        break;
                    case "StatusSprite":
                        sprite.name = (isOnline) ? "PSPS_StatusLight_Green" : "PSPS_StatusLight_Red";
                        break;
                    default:
                        break;
                }
            }
         itemsGrid.Reposition();
         itemsDraggablePanel.ResetPosition();
        }
      
}

Okay, so here's the solution!  The prefab that's being instantiated cannot have a panel, but NGUI objects need one and will add it automatically if one doesn't exist in one of their parents.  The trick is to add a game object with a panel as a parent to the prefab and then remove the panel from the prefab:

+ ItemPrefabParent (UIPanel)
+ -- +ItemPrefab (DragPanelContents only) (call NGUITools.AddChild on this!)
+     + -- NameLabel
+     + -- StatusSprite
+     + -- ImageButton
« Last Edit: May 09, 2013, 01:02:10 PM by PnzrDrgoon »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [SOLVED] Scroll View not clipping dynamically added children
« Reply #1 on: May 10, 2013, 12:04:15 AM »
The question is what's making the panel get added? NGUITools.AddChild parents the object right after its Awake() function, so the only way a panel gets added is if you do something in Awake() that forces a panel to get added.

MGB

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: [SOLVED] Scroll View not clipping dynamically added children
« Reply #2 on: July 09, 2013, 11:05:42 AM »
I've just had some fun with this :-/

The problem is when using a prefab as draggable content.  If you put the prefab in the scene to edit it, NGui auto-adds a new panel component to the base game object.  When you then apply the changes to your item, it stops the draggable list clipping working due to the new panel.

The workaround is to remove the rogue panel component from the prefab in-situ without adding one to the scene first.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [SOLVED] Scroll View not clipping dynamically added children
« Reply #3 on: July 09, 2013, 10:24:06 PM »
Proper workflow involves dropping your panel into a proper place in the UI hierarchy :)