问题
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
ParticleEffectfor the particles.The
ParticleCamerais a newCamera. Hereremove the
AudioListenercomponent since there may only be one in the Scene.Set ClearFlag to
Solid Colorand 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 to0.Set
Culling Maskto onlyParticleEffectso this camera renders nothing else from the sceneAnd the
RenderParticleseffectcomponent
On the normal
MainCameraremoveParticleEffectfrom theCulling MaskSet the
Particlesto the layerParticleEffectso now it will only be rendered by theParticleCameraFinally reference the target
particleImagefrom the UI in theRenderParticlesEffectcomponent on theParticlesCamera
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