画布上的 Unity 粒子效果 [英] Unity Particle Effects On Canvas

查看:22
本文介绍了画布上的 Unity 粒子效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在您的 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 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.

这篇关于画布上的 Unity 粒子效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆