您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

55 行
1.5 KiB

using UnityEngine;
public class ParticleCulling : MonoBehaviour
{
public float cullingRadius = 1;
public ParticleSystem target;
private ParticleSystemRenderer render;
private CullingGroup m_CullingGroup;
void Start()
{
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;
render = GetComponent<ParticleSystemRenderer>();
}
private void Update()
{
m_CullingGroup.targetCamera = Camera.main;
}
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);
render.enabled = true;
//Debug.Log("ParticlesEnabled");
}
else
{
target.Pause();
render.enabled = false;
//Debug.Log("ParticlesDisabled");
}
}
void OnDestroy()
{
if (m_CullingGroup != null)
m_CullingGroup.Dispose();
}
void OnDrawGizmos()
{
// Draw gizmos to show the culling sphere.
Gizmos.color = Color.cyan;
Gizmos.DrawWireSphere(transform.position, cullingRadius);
}
}