画布上的 Unity 粒子效果 [英] Unity Particle Effects On Canvas
问题描述
是否可以在您的 UI 元素上使用粒子效果系统.例如在画布上?我想为我的 UI 元素制作一些动画等等,粒子系统会很好,但它似乎不支持这一点.我的假设是否正确?还有其他解决方案吗?
好吧,您可以做的是让相机将不同层上的粒子效果渲染到
添加一个新的
ParticleCamera
是一个新的Camera
.这里删除
AudioListener
组件,因为场景中可能只有一个.将 ClearFlag 设置为
Solid Color
并设置所需的颜色.粒子不会完全透明,但总是会在边缘使此相机的背景颜色膨胀一点.确保 alpha 设置为0
.将
Culling Mask
设置为仅ParticleEffect
以便此相机不渲染场景中的其他任何内容和
RenderParticleseffect
组件
在普通的
移除MainCamera
上从Culling Mask
ParticleEffect
将
渲染Particles
设置为ParticleEffect
层,所以现在它只会由ParticleCamera
最后从
上的ParticlesCamera
RenderParticlesEffect
组件中的 UI 引用目标particleImage
结果:
Canvas 是否为 Screenspace Overlay
并不重要.
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?
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 newCamera
. Hereremove 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 to0
.Set
Culling Mask
to onlyParticleEffect
so this camera renders nothing else from the sceneAnd the
RenderParticleseffect
component
On the normal
MainCamera
removeParticleEffect
from theCulling Mask
Set the
Particles
to the layerParticleEffect
so now it will only be rendered by theParticleCamera
Finally reference the target
particleImage
from the UI in theRenderParticlesEffect
component on theParticlesCamera
Result:
It doesn't matter if the Canvas is Screenspace Overlay
or not.
这篇关于画布上的 Unity 粒子效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!