Author Topic: Unity's Sprite between NGUI layers  (Read 11034 times)

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Unity's Sprite between NGUI layers
« on: March 24, 2014, 08:51:55 AM »
Hi,
I read some posts but still don't get how can I sort different 'areas' using z-depth.
I'm using UISprite for background ( because is Tiled ) -> Unity's Sprites -> NGUI for HUD.
My GameObjects are organized like this:

2D ( Empty - Z:0 )
-Player ( Unity's Sprite - Z:-1 )
-Main Camera ( Depth Only - Depth: 1 - Size:3.8 - Layer:Default )
NGUI ( UIRoot - UIPanel [ Explicit:3000 ] - Z:0 )
-HUD Camera ( Depth Only - Depth: 1 - Size:1 - Layer:HUD )
-Background Panel ( UIPanel [ Explicit:3000 ] - Z:0 )
--Background ( UISprite - Z:0 )
-HUD Panel ( UIPanel [ Explicit:4000 ] - Z:-3 )

The only problem is that i can't see Unity's Sprites between the layers.
How could I solve this?
I read about using another camera, but how it would affect performance? What is the best approach?
Thank you!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #1 on: March 24, 2014, 05:27:48 PM »
Z is not used in NGUI by default. Everything is sorted by Depth instead. First, panel depth, then widget depth. Think of panels as folders containing files (widgets).

Assuming you know this, and that you're chosen your panels to use explicit queues as your post suggests, then Z also doesn't have any effect, because everything is now sorted by your explicitly chosen queues. Z-sorting only happens within the same render queue.

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #2 on: March 24, 2014, 08:37:25 PM »
Thank you for your explanation ArenMook, but I still could not solve this!
I changed:

NGUI ( UIRoot - UIPanel [ Render Q: Automatic ] - Z:0 )
- HUD Camera ( Depth Only - Depth: 1 - Size:1 - Layer:HUD )
- Background Panel ( UIPanel [ Render Q: Start At:0 ] - Z:0 )
-- Background ( UISprite - Z:0 )
- HUD Panel ( UIPanel [ Render Q: Start At:1 ] - Z:-3 )

My questions:
- Which is the Unity's render queue value?
- What is the difference between Render Q Explicit and Start At.

I was using Explicit because of what i read, but I actually don't know what to use!
Thank you!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #3 on: March 26, 2014, 03:16:51 AM »
"Start at 0"?

The default starting queue is 3000, which is = "Transparent".

All your 3 panels are using different render queues. Root is on 3000, background on 0, and HUD on 1. So first it draws the background, then it draws the HUD, and then it draws the UIRoot. Your "Z" has no effect.

If you want "Z" to have any effect, set ALL 3 panels to "Explicit", giving them ALL Render Queue of 3000.

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #4 on: March 26, 2014, 05:57:47 PM »
Almost there!
I think NGUI's part is OK now, i did what you said: All UIPanels Render Queue are set to "Explicit" : 3000.

- UIPanel Background [ Z:0 ]
- UIPanel HUD [ Z:-3 ]

But how can I see Unity's Sprites between these panels?
I have several Unity's Sprites at Z:-1 but i can't see them.
What I wanted to said is how can I use Z globally between NGUI and Non-NGUI objects, I'm sorry for the mess!
Thank you very much for your help!!


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #5 on: March 26, 2014, 08:18:39 PM »
Are they Unity2D sprites? If so, they're generally sorted by "Sort Order", not Z.

Other renderers and particle systems may need to have their Render Queue adjusted. I posted a script that can do that in the Useful Stuff sticky post.

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #6 on: March 27, 2014, 09:29:03 PM »
Thank you ArenMook,
I tried to use SetRenderQueue but didn't work. Now I'm using another camera for HUD.
I may be doing something wrong, I tried to change SetRenderQueue to get children and see changes in Unity Editor but it seems that my code didn't work:

  1.    
  2.     [ExecuteInEditMode]
  3.     public class SetRenderQueue : MonoBehaviour
  4.     {
  5.         public int RenderQueue = 3000;
  6.  
  7.         private List<Material> m_MaterialList = null;
  8.  
  9.  
  10.         private void Awake()
  11.         {
  12.             m_MaterialList = new List<Material>();
  13.         }
  14.  
  15.  
  16.         private void Start()
  17.         {
  18.             LoadMaterial();
  19.             LoadChildrenMaterial();
  20.         }
  21.  
  22.  
  23. #if !UNITY_EDITOR
  24.  
  25.  
  26.         private void OnDestroy()
  27.         {
  28.             if (m_MaterialList.Count > 0)
  29.             {
  30.                 for (int i = (m_MaterialList.Count - 1); i >= 0; i--)
  31.                 {
  32.                     Destroy(m_MaterialList[i]);
  33.                 }
  34.             }
  35.         }
  36.  
  37.  
  38. #endif
  39.  
  40.  
  41.         private void LoadMaterial()
  42.         {
  43.             var render = renderer;
  44.  
  45.             if (render == null)
  46.             {
  47.                 var particleSystem = GetComponent<ParticleSystem>();
  48.  
  49.                 if (particleSystem != null)
  50.                 {
  51.                     render = particleSystem.renderer;
  52.                 }
  53.             }
  54.  
  55.             if (render != null)
  56.             {
  57.                 var material = new Material(render.sharedMaterial);
  58.                 material.renderQueue = RenderQueue;
  59.                 render.material = material;
  60.  
  61.                 m_MaterialList.Add(material);
  62.             }
  63.         }
  64.  
  65.  
  66.         private void LoadChildrenMaterial()
  67.         {
  68.             var render = renderer;
  69.  
  70.             foreach (var child in gameObject.GetComponentsInChildren<Transform>())
  71.             {
  72.                 render = child.renderer;
  73.  
  74.                 if (render == null)
  75.                 {
  76.                     var particleSystem = child.GetComponent<ParticleSystem>();
  77.  
  78.                     if (particleSystem != null)
  79.                     {
  80.                         render = particleSystem.renderer;
  81.                     }
  82.                 }
  83.  
  84.                 if (render != null)
  85.                 {
  86.                     var material = new Material(render.sharedMaterial);
  87.                     material.renderQueue = RenderQueue;
  88.                     render.material = material;
  89.  
  90.                     m_MaterialList.Add(material);
  91.                 }
  92.             }
  93.         }
  94.     }

I'll keep it simple! Thank you very much!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #7 on: March 28, 2014, 12:59:25 AM »
If you are using a different camera for HUD than another object, there is physically no way to get things from the other camera to be in between of HUD elements. Think about it. First one camera renders, then another. The two never mix what they see.

Caio.Lib

  • Newbie
  • *
  • Thank You
  • -Given: 7
  • -Receive: 2
  • Posts: 26
    • View Profile
Re: Unity's Sprite between NGUI layers
« Reply #8 on: March 28, 2014, 07:59:22 AM »
You're right!! It's working now!
I didn't realize it, I'm using only one camera now and it's working even without SetRenderQueue I'm just changing Z.
Thank you very much ArenMook!!