【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(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;
        }
    }
}

你可能感兴趣的:(Unity3D,C#)