1. 程式人生 > >【Unity開發】粒子效果穿透最上層UI的解決辦法

【Unity開發】粒子效果穿透最上層UI的解決辦法

只需將下面的指令碼動態或者靜態載入到NGUI的粒子上。並設定一下粒子顯示在某個UI上,就可以解決穿透的問題了
using UnityEngine;
using System.Collections;

public class EffectDepth : MonoBehaviour {

    //粒子放置的UI位置
    public UIWidget target;

    //記錄渲染深度
    private int oldRQ;
	
	// Update is called once per frame
	void Update () {
        AdjustRQ();
	}

    public void AdjustRQ()
    {
        try
        {
            if (target == null && target.drawCall == null)
            {
                return;
            }
            if (target.drawCall.renderQueue == oldRQ)
            {
                return;
            }
            Renderer[] rends = transform.GetComponentsInChildren<Renderer>(true);
            if (rends == null)
            {
                return;
            }
            for (int i = 0; i < rends.Length; i++)
            {
                //將粒子的渲染佇列+1
                if (rends[i].material != null)
                {
                    rends[i].material.renderQueue = target.drawCall.renderQueue + 1;
                }
            }
            oldRQ = target.drawCall.renderQueue;
        }
        catch (System.Exception)
        {
            throw;
        }
    }
}