If you want to dynamically alter a renderer's draw queue (Renderer or Particle System), you can use the following script:
using UnityEngine;
public class SetRenderQueue : MonoBehaviour
{
public int renderQueue = 3000;
Material mMat;
void Start ()
{
Renderer ren = renderer;
if (ren == null)
{
ParticleSystem sys = GetComponent<ParticleSystem>();
if (sys != null) ren = sys.renderer;
}
if (ren != null)
{
mMat
= new Material
(ren
.sharedMaterial); mMat.renderQueue = renderQueue;
ren.material = mMat;
}
}
void OnDestroy () { if (mMat != null) Destroy(mMat); }
}
I wrote this before. if anyone wants to place a SFX in front/back of a widget, it's very easy to do that at runtime;
RenderQueueModifier rqm = obj.AddComponent<RenderQueueModifier>();
rqm.m_target = whichWidget;
rqm.m_type = RenderType.FRONT;
using UnityEngine;
using System.Collections;
public class RenderQueueModifier : MonoBehaviour
{
public enum RenderType
{
FRONT,
BACK
}
public UIWidget m_target = null;
public RenderType m_type = RenderType.FRONT;
Renderer[] _renderers;
int _lastQueue = 0;
void Start ()
{
_renderers = GetComponentsInChildren<Renderer>();
}
void FixedUpdate() {
if( m_target == null || m_target.drawCall == null )
return;
int queue = m_target.drawCall.renderQueue;
queue += m_type == RenderType.FRONT ? 1 : -1;
if( _lastQueue != queue )
{
_lastQueue = queue;
foreach( Renderer r in _renderers )
{
r.material.renderQueue = _lastQueue;
}
}
}
}