Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: justin on February 08, 2016, 01:12:50 PM

Title: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: justin on February 08, 2016, 01:12:50 PM
My project has 2 scenes.

Scene 1:
Immediately loads Scene 2 additively and then unloads itself.

Scene 2:
Displays a UI Sprite.

This works fine in editor. However, in Windows standalone builds, the sprite does not render in Scene 2.

This is the transition code:

  1.        
  2. IEnumerator Start()
  3. {
  4.         var op = SceneManager.LoadSceneAsync ("Scene2", LoadSceneMode.Additive);
  5.  
  6.         while (!op.isDone)
  7.         {
  8.                 yield return null;
  9.         }
  10.                        
  11.         SceneManager.UnloadScene ("Scene1");
  12. }
  13.  

Oddly, if I comment out the last line (SceneManager.UnloadScene), the sprite renders OK.

Unity version 5.3.2p1
NGUI version 3.9.6d
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: ArenMook on February 09, 2016, 02:55:23 AM
I think this would be a question for Unity as it's something to do with how Unity 5's new scene management works. If I understand you correctly, scene 2 is the one with the UI. Scene 1 has no UI, no UICamera, no UIRoot, nothing -- right? It merely loads the second scene? I can only suggest checking your cameras. If your first scene has a camera that clears color, make sure that the second scene does too, and that their depths are set to what you'd expect.
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: justin on February 09, 2016, 11:20:45 AM
No luck.

Scene 1 has nothing except for the script attached in my original post. I mucked with the Clear Flags for every camera. I even removed all of the cameras except for the UI Camera in the second scene. All of the NGUI widget and camera settings are default, except for the clear flags which I tried every combination of.

This only seems to happen with NGUI widgets. The fact that it works in editor and fails silently in standalone builds is a huge problem and makes debugging a nightmare.  :-\

I set my UI Camera to render everything and tossed a cube in the scene, which gave me some weird results (screenshot attached).

Hopefully you have some more ideas. My original post has the example project attached if you want to see for yourself.
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: justin on February 09, 2016, 09:00:40 PM
Finally an error! This occurred in my main project where I first noticed the issue (again, only in standalone):

NullReferenceException
  at (wrapper managed-to-native) UnityEngine.Component:get_transform ()
  at UIDrawCall.get_cachedTransform () [0x00011] in ...\NGUI\Scripts\Internal\UIDrawCall.cs:185
  at UIPanel.UpdateDrawCalls () [0x001a6] in ...\Assets\NGUI\Scripts\UI\UIPanel.cs:1507
  at UIPanel.LateUpdate () [0x00080] in ...\NGUI\Scripts\UI\UIPanel.cs:1240
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: justin on February 10, 2016, 12:22:18 AM
Like you said, it was a Unity issue after all.

After quite a bit of digging I managed to find a related bug on the Unity issue tracker: https://issuetracker.unity3d.com/issues/unloading-a-scene-can-destroy-non-instantiated-prefabs-that-are-indirectly-referred-to-by-other-currently-loaded-scene

I upgraded to Unity beta 5.4.0b5 and it fixed this issue.


Edit: The Null Reference Exception in the above post still occurs from time to time. At this point I have no clue if it's related or not.
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: justin on February 11, 2016, 10:39:01 PM
Ok. I seem to have "fixed" that error. The problem had something to do with the scene manager cleaning up draw calls during scene unloading.

Here's my working code:

  1.         public IEnumerator LoadNextScene()
  2.         {
  3.             // load next scene
  4.             SceneManager.LoadScene(nextScene.ToString(), LoadSceneMode.Additive);
  5.  
  6.             // wait one frame for scene to load
  7.             yield return null;
  8.  
  9.             // set the active scene as soon as it's ready
  10.             var scene = SceneManager.GetSceneByName(nextScene.ToString());
  11.             SceneManager.SetActiveScene(scene);
  12.  
  13.             // move existing draw calls to new active scene
  14.             foreach(var dc in UIDrawCall.activeList)
  15.             {
  16.                 SceneManager.MoveGameObjectToScene(dc.gameObject, scene);
  17.             }
  18.  
  19.             foreach (var dc in UIDrawCall.inactiveList)
  20.             {
  21.                 SceneManager.MoveGameObjectToScene(dc.gameObject, scene);
  22.             }
  23.  
  24.             // unload previous scene
  25.             SceneManager.UnloadScene(previousScene.ToString());
  26.         }
  27.  
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: serhilk on March 18, 2016, 09:53:34 AM
I'm currently working in Unity 5.3.4f1 and I have a Scene that loads 2 scenes additive using SceneManager and in some moment I want to use UnloadScene (using SceneManager to) on a or both scenes and I'm having the same problems you reported:

  1. NullReferenceException
  2.   at (wrapper managed-to-native) UnityEngine.Component:get_transform ()
  3.   at UIDrawCall.get_cachedTransform () [0x00011] in ...\NGUI\Scripts\Internal\UIDrawCall.cs:185
  4.   at UIPanel.UpdateDrawCalls () [0x001a6] in ...\Assets\NGUI\Scripts\UI\UIPanel.cs:1507
  5.   at UIPanel.LateUpdate () [0x00080] in ...\NGUI\Scripts\UI\UIPanel.cs:1240

Trying to fix, it seems that the patch you recomended about moving uidrawcalls to an active scene is currently fixing this problem, but I found that in very low android devices that this bug happens randomly loading normally some base scenes (I don't need to force the Unloadscene to get this bug in this cases)

To avoid applying the patch, I tried too, to don't unload the additive scenes and it seems that for android the problem is resolved but for Ios the problem is still there and I'm forced to use the patch again.... :P

Do you have what's happening here? Is this bug fixed in the latest version of NGUI?

Thanks in advance!
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: serhilk on April 06, 2016, 04:28:41 AM
I upgraded to 5.3.4p1 and the same is shown... has someone else the same problem? any good and official solutions?
Title: Re: Sprites do not render in standalone builds after scene switch (works in editor)
Post by: ArenMook on April 06, 2016, 03:10:04 PM
There is no "official" solution for this because it's specific to how you're using Unity. As Justin pointed out, you can fix the problem by not simply loading the scene additively followed by unloading a previous scene, but actually transferring NGUI's draw calls to that new scene first. Since you are the one who knows which scene should stay and which scene should be unloaded, it's up to you to do that. If you still get issues even with the fix, then I suggest submitting a bug report to Unity so that they can address the problem.