Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: hkessock on May 28, 2014, 06:00:46 PM

Title: Noob question relating to cameras
Post by: hkessock on May 28, 2014, 06:00:46 PM
I would very much like to package up my individual NGUI UIs into prefabs, and on my first day have been very happy with how easy it is to get going with NGUI.

I did run into an issue though when instantiating prefabs.

I ASSumed that to create the prefab I should be dragging the UI root into an empty prefab, which means that the prefab has a child camera object.  This, of course, ensures that NGUI will draw the UI components.

My issue is that when I have more than one prefab (e.g., I have a splash screen NGUI prefab and a network file transfer dialog NGUI prefab) there end up being two NGUI cameras in the scene, and it appears that each camera renders both NGUI prefabs (the cameras don't just draw their child objects.)

Am I just screwing this up conceptually?

Worst case, I could simply ensure that at least one NGUI camera was in the scene and delete any new ones that appear after prefab instantiation (or the reverse, don't include any cameras and instantiate a camera when adding NGUI prefabs if I haven't done so already...)

I also presume that having multiple cameras would make optimizations difficult (render queuing, sorting, atlas usage, et cetera...)

Anybody prefab their dialogs like this?  If so, what's your approach?

BTW, NGUI is great, thanks for the hard work.

Cheers,

     Hans
Title: Re: Noob question relating to cameras
Post by: ArenMook on May 29, 2014, 06:10:21 AM
You should not be including UIRoot and camera in your prefab. Watch the tutorial videos I posted -- to create a prefab out of a button I just drag the button itself into the Prefab Toolbar.
Title: Re: Noob question relating to cameras
Post by: hkessock on May 30, 2014, 11:47:20 AM
If I don't include UI root, will NGUI automagically create one?  Where will the NGUI camera come from?
Title: Re: Noob question relating to cameras
Post by: ArenMook on May 30, 2014, 03:33:12 PM
UIRoot isn't need to draw the UI. Only the UIPanel is needed. NGUI automatically parents to a UIRoot when you drag any UI element into the scene. At run-time when instantiating, you should be passing a valid parent object to NGUITools.AddChild(parent, prefab). Make this parent your UIRoot or something similar underneath it.
Title: Re: Noob question relating to cameras
Post by: hkessock on May 30, 2014, 04:20:00 PM
I'm sorry, but what do you mean by "valid parent object" - any game object or...?
Title: Re: Noob question relating to cameras
Post by: ArenMook on May 31, 2014, 12:41:39 PM
A game object underneath your UIRoot or the UIRoot itself, such as UIRoot.list[0].gameObject.
Title: Re: Noob question relating to cameras
Post by: hkessock on May 31, 2014, 11:05:40 PM
I'm sorry I'm not understanding what you're saying.

When you use the term "UIRoot" you refer to it both as "your UIRoot" or "the UIRoot" - do you just mean that I simply need to call NGUITools.AddChild(parent, prefab) with "parent" being basically any gameobject?

In other words, as long as I use NGUITools.AddChild, the parent object could simply be created dynamically as an empty object just before that call?

E.g.

(1)GameObject m_oMyOwnUIRoot = new GameObject( "MyUIRoot" );
(2)NGUITools.AddChild( m_oMyOwnUIRoot, m_oMyNGUIDialogPrefab );

Cheers :)
Title: Re: Noob question relating to cameras
Post by: ArenMook on June 01, 2014, 05:16:05 PM
  1. NGUITools.AddChild(UIRoot.list[0].gameObject, yourPrefab);
No, you can't create the parent before calling AddChild. That defeats the whole purpose. The object must be underneath the UIRoot. The code I just pasted above will work.
Title: Re: Noob question relating to cameras
Post by: hkessock on June 02, 2014, 07:58:23 AM
This is the part that confuses me: "you can't create the parent before calling AddChild" and "the object must be underneath the UI Root."

Perhaps I should start over.

Imagine I have a brand new scene, and in that scene is only the default Main camera provided by Unity.

There's nothing else in the scene at this point, no UI Root, no other objects, nothing.

If I want to create an instance of my prefab dialog box (created with awesome NGUI components), and my prefab network file transfer UI (also created with awesome NGUI components), what is the process?

Thanks, and sorry I'm not "getting it" just yet.



Title: Re: Noob question relating to cameras
Post by: ArenMook on June 02, 2014, 11:12:48 PM
If you intend to instantiate UI elements, the scene should already have a default UI hierarchy present (UIRoot + UI Camera). If it doesn't, then it's up to you to create it (NGUITools.CreateUI).
Title: Re: Noob question relating to cameras
Post by: hkessock on June 03, 2014, 08:20:59 AM
"it's up to you to create it (NGUITools.CreateUI)." - BINGO!

Thanks Michael :)