Well what you could do would be letting a Camera render the Particle effects on a different layer to a RenderTexture
and show it in a RawImage
in your UI.
Combined with a hint from this answer: By default RenderTexture
has only a colordepth of 24-bit but we need 32-bit for alpha the simplest way is just generating one via code:
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Camera))]
public class RenderParticlesEffect : MonoBehaviour
{
// Here reference the camera component of the particles camera
[SerializeField] private Camera particlesCamera;
// Adjust the resolution in pixels
[SerializeField] private Vector2Int imageResolution = new Vector2Int(256, 256);
// Reference the RawImage in your UI
[SerializeField] private RawImage targetImage;
private RenderTexture renderTexture;
private void Awake()
{
if (!particlesCamera) particlesCamera = GetComponent<Camera>();
renderTexture = new RenderTexture(imageResolution.x, imageResolution.y, 32);
particlesCamera.targetTexture = renderTexture;
targetImage.texture = renderTexture;
}
}
My example Hierachy looks like this:
![enter image description here](https://i.stack.imgur.com/mVnXd.png)
Add a new Layer ParticleEffect
for the particles.
![enter image description here](https://i.stack.imgur.com/3lDT8.png)
The ParticleCamera
is a new Camera
. Here
remove the AudioListener
component since there may only be one in the Scene.
Set ClearFlag to Solid Color
and set a desired color. Particles won't be completely transparent but always bloat a bit the background color of this camera on the edges. Make sure the alpha is set to 0
.
Set Culling Mask
to only ParticleEffect
so this camera renders nothing else from the scene
![enter image description here](https://i.stack.imgur.com/HWPOB.png)
And the RenderParticleseffect
component
On the normal MainCamera
remove ParticleEffect
from the Culling Mask
![enter image description here](https://i.stack.imgur.com/HNBX8.png)
Set the Particles
to the layer ParticleEffect
so now it will only be rendered by the ParticleCamera
![enter image description here](https://i.stack.imgur.com/hraaK.png)
Finally reference the target particleImage
from the UI in the RenderParticlesEffect
component on the ParticlesCamera
![enter image description here](https://i.stack.imgur.com/lyhf3.png)
Result:
![enter image description here](https://i.stack.imgur.com/7P2Q4.gif)
It doesn't matter if the Canvas is Screenspace Overlay
or not.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…