Unity Particle Effects On Canvas

送分小仙女□ 提交于 2020-01-25 06:51:09

问题


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?


回答1:


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:

  • Add a new Layer ParticleEffect for the particles.

  • 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

    • And the RenderParticleseffect component

  • On the normal MainCamera remove ParticleEffect from the Culling Mask

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

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

Result:

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




回答2:


By default the render mode setting is selected as Screen Space- Overlay. This render mode places UI elements on the screen rendered on top of the scene due to which the particle effects are not visible.

You have to change the render mode setting of Canvas in the inspector to Screen Space- Camera and also give reference of camera in the scene to render camera property of canvas. In this render mode the Canvas is placed a given distance in front of a specified Camera.



来源:https://stackoverflow.com/questions/58830542/unity-particle-effects-on-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!