选择在粒子处于触发器体积之内时要执行的操作。
在粒子处于触发器体积之内时,系统为每一帧执行此操作。 另请参阅:MonoBehaviour.OnParticleTrigger。
using UnityEngine; using UnityEditor; using System.Collections.Generic; using UnityEngine.EventSystems;
[RequireComponent(typeof(ParticleSystem))] public class ExampleClass : MonoBehaviour { private ParticleSystem ps; public bool enter; public bool exit; public bool inside; public bool outside;
void Start() { ps = GetComponent<ParticleSystem>();
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.parent = ps.transform; sphere.transform.localPosition = new Vector3(0.0f, 0.0f, 3.0f); sphere.transform.localScale = new Vector3(3.0f, 3.0f, 3.0f); sphere.GetComponent<MeshRenderer>().material = new Material(Shader.Find("Particles/Standard Unlit"));
var shape = ps.shape; shape.enabled = false;
var trigger = ps.trigger; trigger.enabled = true; trigger.SetCollider(0, sphere.GetComponent<Collider>()); }
void Update() { var trigger = ps.trigger; trigger.enter = enter ? ParticleSystemOverlapAction.Callback : ParticleSystemOverlapAction.Ignore; trigger.exit = exit ? ParticleSystemOverlapAction.Callback : ParticleSystemOverlapAction.Ignore; trigger.inside = inside ? ParticleSystemOverlapAction.Callback : ParticleSystemOverlapAction.Ignore; trigger.outside = outside ? ParticleSystemOverlapAction.Callback : ParticleSystemOverlapAction.Ignore; }
void OnGUI() { enter = GUI.Toggle(new Rect(25, 40, 200, 30), enter, "Enter Callback"); exit = GUI.Toggle(new Rect(25, 80, 200, 30), exit, "Exit Callback"); inside = GUI.Toggle(new Rect(25, 120, 200, 30), inside, "Inside Callback"); outside = GUI.Toggle(new Rect(25, 160, 200, 30), outside, "Outside Callback"); }
void OnParticleTrigger() { if (enter) { List<ParticleSystem.Particle> enterList = new List<ParticleSystem.Particle>(); int numEnter = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enterList);
for (int i = 0; i < numEnter; i++) { ParticleSystem.Particle p = enterList[i]; p.startColor = new Color32(255, 0, 0, 255); enterList[i] = p; }
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enterList); }
if (exit) { List<ParticleSystem.Particle> exitList = new List<ParticleSystem.Particle>(); int numExit = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, exitList);
for (int i = 0; i < numExit; i++) { ParticleSystem.Particle p = exitList[i]; p.startColor = new Color32(0, 255, 0, 255); exitList[i] = p; }
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, exitList); }
if (inside) { List<ParticleSystem.Particle> insideList = new List<ParticleSystem.Particle>(); int numInside = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Inside, insideList);
for (int i = 0; i < numInside; i++) { ParticleSystem.Particle p = insideList[i]; p.startColor = new Color32(0, 0, 255, 255); insideList[i] = p; }
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Inside, insideList); }
if (outside) { List<ParticleSystem.Particle> outsideList = new List<ParticleSystem.Particle>(); int numOutside = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Outside, outsideList);
for (int i = 0; i < numOutside; i++) { ParticleSystem.Particle p = outsideList[i]; p.startColor = new Color32(0, 255, 255, 255); outsideList[i] = p; }
ps.SetTriggerParticles(ParticleSystemTriggerEventType.Outside, outsideList); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.