Author Topic: SoftClipping not working when adding prefabed objects  (Read 3963 times)

Phong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
SoftClipping not working when adding prefabed objects
« on: June 26, 2012, 06:34:02 PM »
I am trying to make a soft clipped scrollable panel. I followed the steps described here http://www.tasharen.com/?page_id=4444 carefully, except I didn't use the UIGrid and I am creating the objects under the soft clip panel with a script.

If I use a prefab to create the children of the soft clip panel then clipping doesn't work (I also notice that the UIPanel also does not report the new widget children in it's numWidgets field). If I build the widget by creating an empty game object and AddComponent(UILabel) then clipping does work. The labels labels created using each method are absolutely identical in the inspector (same layer, name, position everything) but one is clipped and one isn't.

Here is the code I use to create the NGUI objects (doesn't work):

            GameObject rowGO = (GameObject) Instantiate(listItemPrefab);
            rowGO.transform.parent = PanelSoftClip.transform;
            rowGO.transform.localPosition = new Vector3(0f,(float)rowY,0f);
            rowGO.transform.localScale = Vector3.one;

Is it O.K. to add widgets to panels like this? Should I be using the NGUI API?

Thanks.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SoftClipping not working when adding prefabed objects
« Reply #1 on: June 26, 2012, 07:20:53 PM »
If clipping doesn't work, then you likely have more panels than you think. Remove the panel from your prefab. Nested panels don't inherit clipping.

Phong

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: SoftClipping not working when adding prefabed objects
« Reply #2 on: June 26, 2012, 10:04:03 PM »
Thanks! that was it. Explains why the widgets weren't being counted as well.

electrodruid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 48
    • View Profile
Re: SoftClipping not working when adding prefabed objects
« Reply #3 on: April 03, 2013, 08:21:23 AM »
(Apologies for necro-ing this thread. I've just run into exactly the same issue and I thought it'd be useful for people searching for this problem in future to see my question along with Phong's)

So... At the risk of sounding dumb... Exactly how does one go about removing panels from prefabs to avoid the clipping problem? The root of the prefab I'm using to populate a scrollable panel has the usual stuff you'd expect - a Box Collider, a UIDragPanelContents, a custom component to control the display of some of the widgets, and that object has a number of widgets as children. Because my object has the widgets as children, NGUI automatically adds a UIPanel to it. I delete the panel component, and NGUI immediately puts it back again. What can I do to tell NGUI that I don't want it to create a panel for this object?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SoftClipping not working when adding prefabed objects
« Reply #4 on: April 03, 2013, 03:00:30 PM »
NGUITools.AddChild to instantiate the object, making it parented in the right place. Don't use Instantiate. If you use Instantiate, and the parenting process is delayed for any reason, NGUI will create a panel if there is no panel present in the hierarchy.

electrodruid

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 48
    • View Profile
Re: SoftClipping not working when adding prefabed objects
« Reply #5 on: April 03, 2013, 03:21:47 PM »
I was already using NGUITools.AddChild rather than instantiating and re-parenting by hand (Unity has that horrible habit of resizing stuff when you try to re-parent things, so AddChild is a good way to fix that).

The problem I was having was that my prefab already had a UIPanel on it, and I couldn't work out how to remove it because NGUI kept re-adding it. I've found a workaround for that which I'll describe here in case it's useful. The trick is, when creating or editing a prefab like this, you need a temporary dummy root GameObject. So in your scene, it looks like

Dummy
- PrefabRoot
--Widget1
--Widget2
... etc

When creating prefabs, the UIPanel should end up on Dummy, not on PrefabRoot. Or, if you're editing a prefab to get rid of the UIPanel, just drag it into the scene as a child of Dummy. When you remove the panel from PrefabRoot, NGUI re-creates the panel on Dummy, leaving your prefab with no panel, and then it will clip properly! One thing to watch out for is that you'll also need to set the Layer for Dummy to be the same layer as the object you'll be parenting the prefabs to at runtime, and check that PrefabRoot and all of your widgets also end up on the same layer (otherwise NGUI spits out a bunch of warnings). When you're done creating/editing the prefab, you can delete Dummy and everything should work.