Author Topic: Clipping not working on dynamically added prefabs  (Read 8814 times)

KatieFish

  • Guest
Clipping not working on dynamically added prefabs
« on: May 30, 2012, 08:01:11 PM »
I have created a UIPanel with a UIGrid inside of it in the editor, and fill it with a prefab dynamically. the clipping area is set on the UIPanel of course, and the gizmo shows it in the right spot.

The prefab object is just a game object which contains a UIbutton, a UIlabel, a UISlider.

If i put a bunch of these prefabs into the UIGrid in the editor, the clipping works perfectly. If i add them dynamically, the clipping does not work at all, although the gizmo still shows as the correct size and placement.

I am new to NGUI and have the purchased version. Using unity 3.5.2f2 with NGUI 2.0.7c on OS Lion.

Any help would be appreciated! Is there anything you have to do? I have tried re-specifying the clipping data, which did not help. Below is a simplified version, since my version actually has UIGrids with UIGrids in them (so i can have a 3 columns by X rows grid)

// up in Start()

Game Object _panelParent = GameObject.Find("MyPanel");
UIPanel _panel = _panelParent.GetComponent<UIPanel>();
UIDraggablePanel _panelDragger = _panelParent.GetComponent<UIDraggablePanel>();

//this is repeated for many prefabs.
GameObject prefab = (GameObject)Instantiate (Resources.Load ("Prefabs/UI/MyPrefab"));
prefab.transform.parent = _uigridGO.transform;
prefab.layer = _panelParent.layer;
prefab.GetComponent<UIDragPanelContents> ().draggablePanel = _panelDragger;
prefab.transform.localScale = Vector3.one;
prefab.transform.localPosition = Vector3.zero;
prefab.transform.localRotation = Quaternion.identity;

_uigrid.repositionNow = true;
_panelDragger.repositionClipping = true;

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: Clipping not working on dynamically added prefabs
« Reply #1 on: May 30, 2012, 09:39:31 PM »
The first thing I'd check is whether or not UIPanels are being added to your prefabs when you instantiate them.  That would prevent the panel on your grid from being able to clip them.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Clipping not working on dynamically added prefabs
« Reply #2 on: May 31, 2012, 01:58:30 AM »
You should use NGUITools.AddChild to instantiate your prefab, not unity's Instantiate call. It does everything you're trying to do (parenting, layer change, transform reset), reducing your 6 lines of code down to 1.

As Ryan mentioned, make sure that you don't end up with more panels than intended. Panels are created by default if there are none in the hierarchy.

On an unrelated note, don't use GameObject.Find. Have a public variable on your script instead that you can drag & drop a value into in inspector.

KatieFish

  • Guest
Re: Clipping not working on dynamically added prefabs
« Reply #3 on: May 31, 2012, 01:49:02 PM »
Thank you for your suggestions! =) I will use NGUITools.AddChild() and switch the Find() to a public variable.

Writing out this hierarchy tree revealed the problem you both suggested: my prefab was a UIPanel. I just went and removed the UIPanels off the prefabs and it's clipping properly. Hooray! =)

As long as i have this hierarchy written out- i have a second question that may use it. I am struggling with Z order issues. I am using only a single atlas. Every object in the hierarchy has a Z of 1 in the Inspector. However the UITiledSpriteBG (below) appear in front of the prefabs. I can give the UITiledSpriteBG a depth of -30 and make sure that all the UILabels and UISprites in the prefab have a positive depth, but that does not seem to help.

Logically, i would expect to adjust a depth setting on the UIPanel since it is a sibling to UITiledSpriteBG. Is there a UIDepth object or something similar i can add to my UIPanel? (Or- some better way i should be thinking about the issue of depth?)

Hierarchy:

GameObjectTopLevel (has a small collider as i drag this menu around)
--GameObjectParent_A
----UIScrollBar_A
----UITilesSpriteBG_A
----UIPanel_A (Is also a UIDraggablePanel)
------UIGrid_A (vertical)
--------Prefab_A1 (dynamically added)
--------Prefab_A2 (dynamically added)
--------Prefab_Aetc (dynamically added)
--GameObjectParent_B
----UIScrollBar_B
----UITilesSpriteBG_B
----UIPanel_B (Is also a UIDraggablePanel)
------UIGridRows_B (vertical) (dynamically added)
--------UIGridColumns_B (horizontal) (dynamically added)
----------Prefab_B1 (dynamically added)
----------Prefab_B2 (dynamically added)
----------Prefab_Betc (dynamically added)

The first UIPanel just has a single vertical list of Prefab_A objects. The second UIPanel has vertical list of 3 columns of Prefab_B.

Prefabs A and B are pretty similar:

Prefab_AB (UIDragPanelContents) (UIPanel ** here was the clipping problem **)
--UISprite
--UILabel
--UIButton
--UISlider

Thanks again for your help!

ryan

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 90
    • View Profile
Re: Clipping not working on dynamically added prefabs
« Reply #4 on: May 31, 2012, 02:01:50 PM »
Even though they're using the same atlas, each panel is drawn separately.  (That's why clipping from one panel can't affect other panels.)  At that point the relative depths of widgets in different panels can't be honored, so you need to use different Z positions to fix the drawing order.  Higher Z values will be drawn behind lower Z values.  If everything currently has Z set to 1, then setting Z on your UIPanel_A and UIPanel_B to 0 should bring their contents in front of the tiled backgrounds.

KatieFish

  • Guest
Re: Clipping not working on dynamically added prefabs
« Reply #5 on: May 31, 2012, 05:01:18 PM »
Thank you =) Changing the Z did fix it (i changed the z of the background as opposed to the panels). I was hoping for something that wouldn't mess with the viewable size of the graphics or get in the way of rotation, but this is good for now!