看了UWA视频,实现的是将粒子的shader部分做裁剪处理,修改其renderQueue夹杂在不同item之间,附上粒子部分代码:
using UnityEngine;using System.Collections;
[ExecuteInEditMode]//在编辑器下生效
[RequireComponent(typeof(ParticleSystemRenderer))]
public class UIParticle : MonoBehaviour {
private UIPanel panel;
private ParticleSystemRenderer pRenderer;
private Material dyMaterial;
private UIWidget cover;
// Use this for initialization
void Start (){
panel = GetComponentInParent();
pRenderer = GetComponent();
dyMaterial = new Material(Shader.Find("Hidden/Unlit/Transparent Colored 1"))
{
renderQueue = 4000,
mainTexture = pRenderer.sharedMaterial.mainTexture
};
pRenderer.material = dyMaterial;
}
// OnWillRenderObject is called once per frame before rendering
void OnWillRenderObject()
{
if (panel != null && panel.hasClipping)
{
if (cover != null && cover.isActiveAndEnabled && cover.drawCall != null)
{
dyMaterial.renderQueue = cover.drawCall.renderQueue;
}
Vector4 cr = panel.drawCallClipRange;
Vector2 soft = panel.clipSoftness;
Vector2 sharpness = new Vector2(1000.0f, 1000.0f);
if (soft.x > 0f) sharpness.x = cr.z / soft.x;
if (soft.y > 0f) sharpness.y = cr.w / soft.y;
float scale = 1.0f / transform.lossyScale.x;
Vector3 position = -panel.transform.position * scale;
dyMaterial.SetVector(Shader.PropertyToID("_ClipRange0"), new Vector4(-cr.x / cr.z + position.x / cr.z, -cr.y / cr.w + position.y / cr.w, 1f / cr.z * scale, 1f / cr.w * scale));
dyMaterial.SetVector(Shader.PropertyToID("_ClipArgs0"), new Vector4(sharpness.x, sharpness.y, 0, 1));
}
}
// Destroy the dyMaterial to avoid leaking
void OnDestroy()
{
DestroyImmediate(dyMaterial);
dyMaterial = null;
}
}
代码部分中new一个material(dyMaterial = new Material)是因为直接修改本身的material的话,unity也会自动创建一个material,而到最后这个material如果不手动销毁,那么就会驻存在内存中,当加载关卡切换场景或是unload的时候才会释放掉,如果直接destroy则不清楚到底销毁的是哪个材质,这样直接new出来的话就清晰多了。