Legacy Documentation: Version 2017.2 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

MonoBehaviour.OnParticleTrigger()

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

OnParticleTrigger is called when any particles in a particle system meet the conditions in the trigger module.

This can be used to kill or modify particles that are inside or outside a collision volume.

#pragma strict
public class TriggerScript extends MonoBehaviour {
	function OnParticleTrigger() {
		var ps: ParticleSystem = GetComponent.<ParticleSystem>();
		// particles
		var enter: List.<ParticleSystem.Particle> = new List.<ParticleSystem.Particle>();
		var exit: List.<ParticleSystem.Particle> = new List.<ParticleSystem.Particle>();
		// get
		var numEnter: int = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
		var numExit: int = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
		// iterate
		for (var i: int = 0; i < numEnter; i++) {
			var p: ParticleSystem.Particle = enter[i];
			p.startColor = new Color32(255, 0, 0, 255);
			enter[i] = p;
		}
		for (var i: int = 0; i < numExit; i++) {
			var p: ParticleSystem.Particle = exit[i];
			p.startColor = new Color32(0, 255, 0, 255);
			exit[i] = p;
		}
		// set
		ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
		ps.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
	}
}
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); } }

In order to retrieve detailed information about all the collisions caused by the ParticleSystem, ParticlePhysicsExtensions.GetTriggerParticles must be used to retrieve the array of Particle.

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答