画布上的Unity粒子效果 [英] Unity Particle Effects On Canvas
问题描述
可以在您的UI元素上使用粒子效果系统.例如在画布上?我想为我的UI元素制作一些动画,而不做任何事情,粒子系统会很好,但是它似乎不支持该动画.我认为正确吗?还有其他解决方案吗?
好吧,您可以做的是让Camera在不同的图层上将粒子效果渲染到 RawImage
.
结合了
我的示例Hierachy看起来像这样: 为粒子添加新的 Layer 删除 将ClearFlag设置为 仅将 和 在普通的 将 最后从 结果: 画布是否为 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 Combined with a hint from this answer: By default
My example Hierachy looks like this: Add a new Layer The remove the Set ClearFlag to Set And the On the normal Set the Finally reference the target Result: It doesn't matter if the Canvas is 这篇关于画布上的Unity粒子效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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;
}
}
ParticleEffect
./p>
ParticleCamera
是新的Camera
.在这里
AudioListener
组件,因为场景中可能只有一个.Solid Color
并设置所需的颜色.粒子不会完全透明,但始终会使相机边缘的背景颜色有些膨胀.确保Alpha设置为0
.Culling Mask
设置为ParticleEffect
,以便此摄像机从场景中不渲染其他任何东西RenderParticleseffect
组件MainCamera
上,从Culling Mask
ParticleEffect
Particles
设置为图层ParticleEffect
,因此现在仅由ParticleCamera
ParticlesCamera
RenderParticlesEffect
组件中的UI引用目标particleImage
Screenspace Overlay
都没有关系.RenderTexture
and show it in a RawImage
in your UI.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;
}
}
ParticleEffect
for the particles.ParticleCamera
is a new Camera
. Here
AudioListener
component since there may only be one in the Scene.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
.Culling Mask
to only ParticleEffect
so this camera renders nothing else from the sceneRenderParticleseffect
componentMainCamera
remove ParticleEffect
from the Culling Mask
Particles
to the layer ParticleEffect
so now it will only be rendered by the ParticleCamera
particleImage
from the UI in the RenderParticlesEffect
component on the ParticlesCamera
Screenspace Overlay
or not.