UGUI 粒子特效與UI層級問題

遊戲中,界面上有些按鈕之上須要放置一個特效,或者有些區域顯示好比image上顯示一個特效,這時候若是再打開一個UI,咱們須要讓新的UI顯示在特效上層,而不是被特效遮擋,這是就須要設置特效的渲染順序。canvas

設置特效的order in layer,新版的unity中,particle system能夠直接設置render的order,可是特效中有些是使用particle system作的,有些又是直接用的圖像動畫等等,而相似MeshRender的order in layer是不能在編輯器直接設置的,編輯器

因此靠手工設置是不現實的,須要用腳本設置。ide

代碼:動畫

// 深度遍歷設置全部 Render 的 SortingLayer
        public static void setRenderSortingLayerByActor(GameObject actor, int order, bool isRecursion)
        {
            if (null != actor)
            {
                UtilApi.setSpriteRenderSortingLayerByActor(actor, order);
                UtilApi.setParticleSystemSortingLayerByActor(actor, order);
                UtilApi.setMeshRenderSortingLayerByActor(actor, order);

                int childCount = actor.transform.childCount;
                int idx = 0;
                Transform childTrans = null;

                if (isRecursion)
                {
                    for (idx = 0; idx < childCount; ++idx)
                    {
                        childTrans = actor.transform.GetChild(idx);
                        UtilApi.setRenderSortingLayerByActor(childTrans.gameObject, order, isRecursion);
                    }
                }
            }
        }

static public void setSpriteRenderSortingLayerByActor(UnityEngine.GameObject actor, int order)
        {
            if (null != actor)
            {
                SpriteRenderer render = null;
                render = actor.GetComponent<SpriteRenderer>();

                UtilApi.setSpriteRenderSortingLayerBySpriteRenderer(render, order);
            }
        }

/**
         * @brief  unity渲染層級關係小結
         * @ref http://blog.csdn.net/meegomeego/article/details/42060389
         */
        // 能夠在編輯器中設置
        static public void setSpriteRenderSortingLayerBySpriteRenderer(SpriteRenderer render, int order)
        {
            if (null != render && render.sortingOrder != order)
            {
                render.sortingOrder = order;
            }
        }

static public void setParticleSystemSortingLayerByActor(UnityEngine.GameObject actor, int order)
        {
            if (null != actor)
            {
                ParticleSystem particleSystem = null;
                particleSystem = actor.GetComponent<ParticleSystem>();

                UtilApi.setParticleSystemSortingLayer(particleSystem, order);
            }
        }

// 不能在編輯器中設置
        static public void setParticleSystemSortingLayer(ParticleSystem particleSystem, int order)
        {
            if (null != particleSystem)
            {
                Renderer render = particleSystem.GetComponent<Renderer>();
                if (null != render && render.sortingOrder != order)
                {
                    render.sortingOrder = order;
                }
            }
        }

static public void setMeshRenderSortingLayerByActor(UnityEngine.GameObject actor, int order)
        {
            if (null != actor)
            {
                UnityEngine.MeshRenderer meshRenderer = null;
                meshRenderer = actor.GetComponent<UnityEngine.MeshRenderer>();

                UtilApi.setMeshRenderSortingLayer(meshRenderer, order);
            }
        }

// 不能在編輯器中設置
        static public void setMeshRenderSortingLayer(UnityEngine.MeshRenderer meshRenderer, int order)
        {
            if (null != meshRenderer && meshRenderer.sortingOrder != order)
            {
                meshRenderer.sortingOrder = order;
            }
        }
View Code

使用時:UtilApi.setRenderSortingLayerByActor(this.mSelfGo, 1, true);   參數中,this.mSelfGo就是特效實例化的父節點,1就是設置的order in layer,由於ui的order默認都是0,因此這個設爲1,true循環設置子節點ui

按理說這個order in layer設置爲0我以爲應該也是正確的,但在個人遊戲中設置爲0就沒法顯示特效,我確實沒有理解。this

個人遊戲結點如圖:spa

ef_quan2all就是個人特效實例,按照這個順序應該也是最後渲染的,不明白設置爲0爲什麼就不顯示,不知有人可否告知。.net

 

order in layer設爲1後,特效顯示了,可是後面出現了一個問題,若是這時打開一個其餘的UI,特效會遮擋這個UI,坑爹。。code

這時就要使出殺手鐗了,由於UI上相似Image或者Button是沒法設置order in layer的,因此要給這個被遮擋的UI添加一個Canvas,Canvas能夠設置order,那麼將其設置爲2,比特效的大,不錯,能夠顯示了,可是又出現一個問題,這個界面上的按鈕事件被攔截了,擦。。。orm

又一招出手,給其再設置一個Graphic Raycster,參數以下

終於完美了。。,既能夠顯示特效,還不會遮擋UI

 

至此有個問題,若是界面不少都須要設置上述的canvas,那就須要本身實現一個管理類,給每一個ui設置它的order,不過canvas會影響合併批次,因此不能有太多,本身權衡。

相關文章
相關標籤/搜索