Version: 2022.2

MonoBehaviour.OnParticleTrigger()

切换到手册

描述

粒子系统中的任何粒子满足触发模块中的条件时,将调用 OnParticleTrigger。

可用于湮灭或修改碰撞体积内部或外部的粒子。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TriggerScript : MonoBehaviour { void OnParticleTrigger() { ParticleSystem ps = GetComponent<ParticleSystem>();

// particles List<ParticleSystem.Particle> enter = new List<ParticleSystem.Particle>(); List<ParticleSystem.Particle> exit = new List<ParticleSystem.Particle>();

// get int numEnter = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter); int numExit = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);

// iterate for (int i = 0; i < numEnter; i++) { ParticleSystem.Particle p = enter[i]; p.startColor = new Color32(255, 0, 0, 255); enter[i] = p; } for (int i = 0; i < numExit; i++) { ParticleSystem.Particle p = exit[i]; p.startColor = new Color32(0, 255, 0, 255); exit[i] = p; }

// set ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter); ps.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit); } }

要检索 ParticleSystem 造成的所有碰撞的详细信息,必须使用 ParticlePhysicsExtensions.GetTriggerParticles 检索 Particle 的数组。