ParticleSystem.automaticCullingEnabled

切换到手册
public bool automaticCullingEnabled ;

描述

该系统是否支持自动剔除?

在内部,每个粒子系统都有两种工作模式:程序化模式和非程序化模式。

在程序化模式下,可以知道粒子系统在任意时间点(过去和将来)的状态,而非程序化系统是不可预测的。这意味着程序化系统可以快速快进(和倒回)到任意时间点。

离开所有摄像机视野的系统会被剔除。发生这种情况时,程序化系统将停止更新。当该系统再次可见时,程序化系统将高效地快进到新的时间点。非程序化系统无法做到这一点,由于其不可预测的性质,它必须继续更新移出屏幕的系统。

要支持自动剔除,只能使用粒子系统模块和属性的子集。例如,使用生命周期限制速度模块将禁用自动剔除。此外,在系统播放期间从脚本修改任何属性也会禁用自动剔除。

为了帮助您确认您是否使用了会禁用该功能的任何属性,Inspector 右上角会显示一个小对话气泡。该图标的工具提示将详细说明禁用自动剔除的原因。

using UnityEngine;

public class CustomParticleCulling : MonoBehaviour { public float cullingRadius = 10.0f; public ParticleSystem target;

private CullingGroup m_CullingGroup;

void Start () { if (target.automaticCullingEnabled) { enabled = false; return }

m_CullingGroup = new CullingGroup(); m_CullingGroup.targetCamera = Camera.main; m_CullingGroup.SetBoundingSpheres(new BoundingSphere[] { new BoundingSphere(transform.position, cullingRadius) }); m_CullingGroup.SetBoundingSphereCount(1); m_CullingGroup.onStateChanged += OnStateChanged; }

void OnStateChanged(CullingGroupEvent sphere) { if (sphere.isVisible) { // We could simulate forward a little here to hide that the system was not updated off-screen. target.Play(true); } else { target.Pause(); } }

void OnDestroy() { if(m_CullingGroup != null) m_CullingGroup.Dispose(); }

void OnDrawGizmos() { // Draw gizmos to show the culling sphere. Gizmos.color = Color.yellow; Gizmos.DrawWireSphere(transform.position, cullingRadius); } }