Author Topic: best practices for NGUI across scenes?  (Read 7277 times)

duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
best practices for NGUI across scenes?
« on: July 03, 2013, 07:46:28 AM »
In a way this is almost a general question about Unity development. The situation is that I want (or assumed I should) several scenes that hold my levels for a game. Each scene file in Unity is a level with geometry, particle systems, NPC setups, etc.

Anyway, the NGUI portion of the project is fairly complicated with a deep hierarchy and dozens and dozens of settings. I realized that if I needed to make a change to the GUI, I'd need to change it in all of the scene files. So I figured I could stuff the whole NGUI tree into a prefab. Then I can just edit once instance and have the changes propagate everywhere. This seems to work.

I just wanted to ask, is this the best way to handle it?

Gregzo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 64
    • View Profile
Re: best practices for NGUI across scenes?
« Reply #1 on: July 03, 2013, 03:56:26 PM »
If you need the UI to persist through scenes, why not set it to DontDestroyOnLoad?

Simplest example : attach the following script to your UICamera in the first loaded level, no need for UI's in other scenes :

  1. public class DontDestroyOnLoadThis : Monobehaviour
  2. {
  3.      void Awake ()
  4.      {
  5.           DontDestroyOnLoad ( this.gameObject );
  6.      }
  7. }

Or pseudo-singleton version, on the camera in every scene :
  1. public class DontDestroyOnLoadUI : Monobehaviour
  2. {
  3.      static DontDestroyOnLoadUI _instance;
  4.  
  5.      void Awake ()
  6.      {
  7.           if ( _instance != null )
  8.           {
  9.                 Destroy ( this.gameObject );
  10.                 return;
  11.           }
  12.  
  13.           DontDestroyOnLoad ( this.gameObject );
  14.           _instance = this;
  15.      }
  16. }
This last example would allow having a UI in every scene for ease of development, but a single persistent UI when actually playing the game.
« Last Edit: July 03, 2013, 04:02:50 PM by Gregzo »

duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: best practices for NGUI across scenes?
« Reply #2 on: July 03, 2013, 11:50:32 PM »
Nice. This was my first time needing to have different scenes in a project and so I wasn't even aware of dontdestroy...I'll try it out. Thanks!

duncanx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: best practices for NGUI across scenes?
« Reply #3 on: July 04, 2013, 01:56:55 AM »
So I noticed that I had a handful of things that I needed to persist across level loads. But this script either creates duplicates (the first one) or kills off everything except for one thing (the second one). I figured the simplest solution was to put everything I wanted to persist under a single game object. It feels weird since that collection of objects aren't normally related to each other, but I can't think of any problems so it seems ok.

riksteri

  • Guest
Re: best practices for NGUI across scenes?
« Reply #4 on: July 04, 2013, 02:58:52 AM »
So I noticed that I had a handful of things that I needed to persist across level loads. But this script either creates duplicates (the first one)

I just dealt with this yesterday in my project, actually. If it's the same problem as I had, it's because when loading a new scene, the game both keeps around the stuff from the previous scene, and also creates new instances of everything that's set to be in the new scene's object hierarchy in the editor. My solution was to create a prefab of the persistent stuff, not insert it in the hierarchy at all in the editor, and Instantiate it at the beginning of the first scene (which inserts it in the hierarchy dynamically). My UI has a way to return to the first scene from the following scenes, so the before the Instantiation I need to check if it's the first time the scene is entered, otherwise it still creates a dublicate of the prefab every time the first scene is returned to.

Gregzo

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 64
    • View Profile
Re: best practices for NGUI across scenes?
« Reply #5 on: July 04, 2013, 03:24:12 AM »
When you load a scene, Unity destroys everything that is not marked DontDestroyOnLoad and loads your new scene fully, including potential duplicates.

  1. DontDestroyOnLoad ( this.gameObject )
affects the object and all it's children. Similarly, Destroy ( gameObject ) will destroy the object and all it's children. Destroy ( this ) will simply destroy the component ( the script ), not the GameObject.

So, for a persistent UI, parent all your UI to one object that is set to DontDestroyOnLoad. When you work on your levels, it can be handy to have the UI in every scene ( play level 5 directly, for example ), but that can lead to duplicates, hence my second script above : check if the UI exists in the scene, if it does, self destruct.

Hope it helps!