Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

user interface - Unity Particle Effects On Canvas

Is is possible to use the particle effects system on your UI elements. For instance on the Canvas? I'd like to make some animations and whatnot for my UI elements and the particle system would be nice, but it doesn't seem to support this. Am I correct in assuming this? Is there another solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  • Add a new Layer ParticleEffect for the particles.

    enter image description here

  • 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

    • And the RenderParticleseffect component

  • On the normal MainCamera remove ParticleEffect from the Culling Mask

    enter image description here

  • Set the Particles to the layer ParticleEffect so now it will only be rendered by the ParticleCamera

    enter image description here

  • Finally reference the target particleImage from the UI in the RenderParticlesEffect component on the ParticlesCamera

    enter image description here

Result:

enter image description here

It doesn't matter if the Canvas is Screenspace Overlay or not.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...