【问题标题】:Unity Particle Effects On Canvas画布上的 Unity 粒子效果
【发布时间】:2019-11-13 04:56:22
【问题描述】:

可以在您的 UI 元素上使用粒子效果系统。例如在画布上?我想为我的 UI 元素制作一些动画和诸如此类的东西,粒子系统会很好,但它似乎不支持这一点。我的假设是否正确?还有其他解决方案吗?

【问题讨论】:

    标签: user-interface unity3d canvas


    【解决方案1】:

    您可以做的是让相机将不同层上的粒子效果渲染到RenderTexture,并在您的 UI 中以RawImage 显示。

    结合this answer 的提示:默认情况下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;
        }
    }
    

    我的层次结构示例如下所示:

    • 为粒子添加一个新的LayerParticleEffect

    • 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 无关紧要。

    【讨论】:

    • 这对我在 Unity 2020 中没有使用 URP 有帮助
    【解决方案2】:

    默认情况下,渲染模式设置选择为屏幕空间-叠加。 此渲染模式将 UI 元素放置在渲染在场景顶部的屏幕上,因此粒子效果不可见。

    您必须将检查器中 Canvas 的渲染模式设置更改为 Screen Space-Camera 并在场景中提供相机的引用以渲染画布的相机属性。 在这种渲染模式下,Canvas 被放置在指定相机前面的给定距离。

    【讨论】:

    • 我意识到了这一点。我尝试过并且成功了,接受我的对象在我的 UI 中飞来飞去?。我想我必须让它以某种方式工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-04-07
    相关资源
    最近更新 更多