Unity NGUI系统中是没有对粒子进行排序的,如:
怎么解决这个问题呢?
思路是把粒子的渲染层级,相对于UI组件的层级进行一个偏移。
解决后的效果如下:
代码如下:
1 using System.Collections.Generic; 2 using UnityEngine; 3 4 public class UIRenderQueueSorter : MonoBehaviour 5 { 6 [SerializeField] 7 UIWidget m_source; 8 [SerializeField] 9 int m_offset; 10 [SerializeField] 11 List<Renderer> m_renderers = new List<Renderer>(); 12 13 public void Initialize() 14 { 15 Renderer[] renders = transform.GetComponentsInChildren<Renderer>(true); 16 m_renderers.AddRange(renders); 17 } 18 19 void Update() 20 { 21 if (m_source && m_renderers.Count > 0) 22 { 23 for (int i = 0; i < m_renderers.Count; i++) 24 { 25 Renderer r = m_renderers[i]; 26 if (r && r.sharedMaterial && m_source.drawCall != null) 27 r.sharedMaterial.renderQueue = m_source.drawCall.renderQueue + m_offset; 28 } 29 } 30 } 31 32 public void AddRenderer(Renderer r) 33 { 34 if (!m_renderers.Contains(r)) 35 { 36 m_renderers.Add(r); 37 } 38 } 39 40 public void ClearAllRenderers() 41 { 42 m_renderers.Clear(); 43 } 44 45 public UIWidget Source 46 { 47 set { m_source = value; } 48 } 49 50 public int Offset 51 { 52 set { m_offset = value; } 53 } 54 }
用法:
1 // 特效排序 2 var sorter = m_effect.AddComponent<UIRenderQueueSorter>(); 3 sorter.Source = m_itemTexture; 4 sorter.Offset = 1; 5 sorter.Initialize();
此脚本将挂在需要排序的粒子的父节点上: