Author Topic: NGUI UI Overlapping Problem  (Read 3328 times)

anubhab123

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
    • Game Design Portfolio
NGUI UI Overlapping Problem
« on: June 05, 2014, 08:38:44 AM »
I have different UIRoots setup across different scenes in my project in order to draw different UIs for different screens and a singleton-like gamemanager object managing all the screens to help transition from one screen to the next. When I go from one screen to the next screen I have the gamemanager object GameObject.FindObjectWithTag("UIRoot") and Destroy() the current UIRoot gameobject which has a tag on it of "UIRoot" so that it does not appear on the next screen(this screen has a seperate UIRoot Object dedicated to drawing the UI for that Screen).

Everything seems to be working great in the Unity Editor however on stand-alone windows builds and web player builds the UI for one screen remains even after transitioning to the next screen. The two UIs are therefore overlapping each other in subsquent screens.

I was wondering if anyone had any idea as to where the problem might be? Should I use a different design pattern and keep one UIRoot object alive across all scenes as oppossed to having different ones for each screen?

Thanks for any help!




ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI UI Overlapping Problem
« Reply #1 on: June 06, 2014, 02:06:16 AM »
Multiple UIRoots is a strange way to go. Instead of doing that, instantiate UI prefabs underneath a single root. You can set this root to be DontDestroyOnLoad if you like.

Even if you don't, what you're doing can be achieved by simply having a script like this:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.     static Test instance;
  6.  
  7.     void OnEnable()
  8.     {
  9.         if (instance != null && instance != this)
  10.         {
  11.             Destroy(instance.gameObject);
  12.         }
  13.         instance = this;
  14.     }
  15.  
  16.     void OnDisable() { if (instance == this) instance = null; }
  17. }

anubhab123

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
    • Game Design Portfolio
Re: NGUI UI Overlapping Problem
« Reply #2 on: June 06, 2014, 03:37:17 PM »
Hey so for the script you showed me I would attach that to the persistent UIRoot object or the individual prefabs?

One thing i'm actually confused about is whether onEnable/onDisable is called when gameobjects are active/inactive or when the individual component is enabled/disabled.

Thanks alot for the help so far!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI UI Overlapping Problem
« Reply #3 on: June 06, 2014, 09:53:38 PM »
OnEnable is called when the script gets enabled -- in this case on starup. It checks -- does the object already exist? If so, destroy the previous one before assigning this one to be the new one.

Generally a singleton is done similarly, except that it destroys the second object to be created, not the first.