Author Topic: [Solved] Placing prefab on different panel  (Read 2977 times)

Kerozard

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
[Solved] Placing prefab on different panel
« on: August 05, 2014, 02:56:32 AM »
Unity3D: 4.5.2f1
NGUI: 3.6.8

I am running a setup with 3 panels and cameras in my game. One each for Background, Game and Foreground (e.g. Dialogs). I have a prefab which I initially created on the Game Panel. Now I want to display the prefab in my Foreground-Panel. The prefab consists of labels, sprites, textures etc.

  1. GameObject CardInstance = NGUITools.AddChild(target, (GameObject)Resources.Load("Prefabs/CardDisplay"));
  2.  

This works as the card is displayed in the dialog on the right panel. "target" is a GameObject holding all objects in the dialog. Sadly I get a warning for each child widget:

  1. You can't place widgets on a layer different than the UIPanel that manages them.
  2. If you want to move widgets to a different layer, parent them to a new panel instead.

I tried to find a solution by myself, but the only thing that I found was the advice to use ParentHasChanged:

  1. CardInstance.GetComponent<UIWidget>().ParentHasChanged();
  2.  

That does not have any apparent effect. Traversing dozens of nested Gameobjects on the search for different NGUI elements to call that function on seems like overkill and is hopefully the wrong approach. How can I fix this issue?

Thanks in advance,

Patrick
« Last Edit: August 05, 2014, 06:11:49 AM by Kerozard »

rain

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 79
    • View Profile
Re: Placing prefab on different panel
« Reply #1 on: August 05, 2014, 05:30:25 AM »
Hi Patrick!

Have you checked whether the prefab's layer matches the panel's layer you're placing it under or not?
I've attached a screenshot to clarify what I'm talking about.

Kerozard

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: Placing prefab on different panel
« Reply #2 on: August 05, 2014, 06:11:27 AM »
Hey rain,

thank you for your reply. The layers did not match, so I wrote an GameObject extension method to set the layer recursively and the warnings were gone.

For anyone interested, this is the extension method. No magic at all, but some people like copy&paste examples.

  1. public static void SetLayerRecursively(this GameObject go, int layerNumber)
  2. {
  3.     foreach (Transform trans in go.GetComponentsInChildren<Transform>(true))
  4.     {
  5.         trans.gameObject.layer = layerNumber;
  6.     }
  7. }
  8.  

And this is how I used it.

  1. public GameObject CreateAt(GameObject target, string cardId)
  2. {
  3.     GameObject prefab = (GameObject)Resources.Load("Prefabs/CardDisplay");
  4.     prefab.SetLayerRecursively(target.layer);
  5.     GameObject CardInstance = NGUITools.AddChild(target, prefab);
  6.     return CardInstance;
  7. }
  8.  

Thank you again for your help. :-)

Patrick

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Solved] Placing prefab on different panel
« Reply #3 on: August 05, 2014, 01:40:22 PM »
NGUI already has this function -- NGUITools.SetLayer.

You should also just change the layer of your prefab in inspector to begin with, not do it at run-time.