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

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

问题描述

可以在您的UI元素上使用粒子效果系统.例如在画布上?我想为我的UI元素制作一些动画,而不做任何事情,粒子系统会很好,但是它似乎不支持该动画.我认为正确吗?还有其他解决方案吗?

解决方案

好吧,您可以做的是让Camera在不同的图层上将粒子效果渲染到这个答案:默认情况下,RenderTexture的色深只有24位,但是我们需要32位的Alpha,最简单的方法就是通过代码生成一个:

 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;
    }
}
 


我的示例Hierachy看起来像这样:

  • 为粒子添加新的 Layer ParticleEffect./p>

  • ParticleCamera是新的Camera.在这里

    • 删除AudioListener组件,因为场景中可能只有一个.

    • 将ClearFlag设置为Solid Color并设置所需的颜色.粒子不会完全透明,但始终会使相机边缘的背景颜色有些膨胀.确保Alpha设置为0.

    • 仅将Culling Mask设置为ParticleEffect,以便此摄像机从场景中不渲染其他任何东西

    • RenderParticleseffect组件

  • 在普通的MainCamera上,从Culling Mask

    中删除ParticleEffect

  • Particles设置为图层ParticleEffect,因此现在仅由ParticleCamera

    渲染

  • 最后从ParticlesCamera

    RenderParticlesEffect组件中的UI引用目标particleImage

结果:

画布是否为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天全站免登陆