一、粒子系统基础属性
Unity 的粒子系统(Particle System)包含许多属性和功能,下面是一些常见的属性及其用途:
发射模块(Emission Module):控制粒子的发射速率、发射角度、初始速度等,可以用来控制粒子的产生方式和速度。
形状模块(Shape Module):定义了粒子发射的区域形状,可以是球体、盒子、圆锥等,也可以通过贴图来定义不规则的发射形状。
大小模块(Size over Lifetime):控制粒子大小随时间变化的曲线,可以实现粒子从产生到消失过程中大小渐变的效果。
颜色模块(Color over Lifetime):控制粒子颜色随时间变化的曲线,可以实现粒子从产生到消失过程中颜色渐变的效果。
旋转模块(Rotation over Lifetime):控制粒子旋转角度随时间变化的曲线,可以实现粒子自旋转的效果。
纹理动画(Texture Sheet Animation):通过设置纹理帧数和播放模式,实现粒子的纹理帧动画效果。
碰撞模块(Collision Module):定义了粒子与碰撞器之间的交互效果,如碰撞后的反弹、消失等行为。
外部力场(External Forces):允许外部力场对粒子系统施加影响,如重力、风等。
Start Lifetime(开始寿命):定义了粒子的初始生命周期,即粒子被发射后存在的时间。
Start Speed(初始速度):确定了粒子被发射时的初速度,影响了粒子移动的距离和速度。
Start Size(初始大小):设定了粒子被发射时的初始大小,可以影响粒子的显示效果。
Start Color(初始颜色):确定了粒子被发射时的初始颜色。
Start Rotation(初始旋转):定义了粒子被发射时的初始旋转角度。
这些属性共同决定了粒子的初始状态,而在粒子系统中,还可以通过曲线或颜色过渡等方式来调整粒子在生命周期内的变化情况,比如:
Size over Lifetime(大小随生命周期变化):控制了粒子在生命周期内大小的变化,可以实现粒子渐变消失的效果。
Color over Lifetime(颜色随生命周期变化):控制了粒子在生命周期内颜色的变化,可以实现颜色渐变消失的效果。
二、自定义脚本操作粒子系统
1.自定义脚本获取组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| using UnityEngine;
public class ParticleController : MonoBehaviour { private ParticleSystem particleSystem;
private void Start() { particleSystem = GetComponent<ParticleSystem>(); }
private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { PlayParticleEffect(); } }
private void PlayParticleEffect() { particleSystem.Play(); } }
|
创建了一个名为 ParticleController 的脚本类,用于控制粒子系统。在 Start 方法中,通过 GetComponent 方法获取当前对象上的 ParticleSystem 组件,以便在后续代码中对其进行操作。
在 Update 方法中,检测到用户按下空格键时,调用 PlayParticleEffect 方法来播放粒子效果。PlayParticleEffect 方法中,我们调用 ParticleSystem 的 Play 方法来启动粒子效果。
使用以上代码,可以将该脚本组件添加到场景中的一个游戏对象上,并将粒子系统组件拖拽到脚本组件的相应字段中。然后,在运行游戏时按下空格键,就能播放粒子效果了。
2.脚本实现火焰效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Frame : MonoBehaviour { public float maxParticles = 1000f; public float emissionRate = 300f; public float lifetime = 1f; public float startSize = 0.2f; public float endSize = 0.01f; public float startSpeed = 1f; public float endSpeed = 0f; public Color startColor = new Color(1f, 0.5f, 0f); public Color endColor = new Color(1f, 0f, 0f);
private ParticleSystem particleSystem;
private void Start() { particleSystem = GetComponent<ParticleSystem>();
var main = particleSystem.main; main.maxParticles = (int)maxParticles; main.startLifetime = lifetime; main.startSize = startSize; main.startSpeed = startSpeed;
var emission = particleSystem.emission; emission.rateOverTime = emissionRate;
var shape = particleSystem.shape; shape.shapeType = ParticleSystemShapeType.Cone; shape.angle = 30f; shape.radius = 0.1f;
var renderer = particleSystem.GetComponent<ParticleSystemRenderer>(); renderer.material = new Material(Shader.Find("Particles/Additive")); renderer.material.color = startColor; }
private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { PlayParticleEffect(); } }
private void PlayParticleEffect() { particleSystem.Play();
StartCoroutine(StopParticleEffect()); }
private IEnumerator StopParticleEffect() { yield return new WaitForSeconds(lifetime);
particleSystem.Stop(); particleSystem.Clear(); } }
|
上述代码中,我们创建了一个名为 FlameEffect 的脚本类,用于控制火焰粒子效果。我们定义了一些公共的属性变量,可以在 Inspector 界面中进行设置,以便调整粒子系统的各项属性。
在 Start 方法中,我们通过 GetComponent 方法获取当前对象上的 ParticleSystem 组件,并设置其各项属性,包括最大粒子数、粒子生命周期、大小、速度、颜色等。
在 Update 方法中,我们检测到用户按下空格键时,调用 PlayParticleEffect 方法来播放粒子效果。PlayParticleEffect 方法中,我们调用 ParticleSystem 的 Play 方法来启动粒子效果,并使用协程在一定时间后停止粒子效果。
StopParticleEffect 方法中,我们使用 WaitForSeconds 方法等待粒子生命周期的时间后,停止粒子效果并清空粒子。