粒子系统力场的脚本接口。
可以使用粒子系统力场来影响进入每个力场影响区的粒子群。
力场的形状可以设置为各种形状,粒子受影响的方式由力场中的各种属性控制。
在您选择形状的过程中,可以定义开始范围和结束范围。结束范围描述了形状的最大范围,开始范围可用于创建中空形状。
可以将许多力应用于此体积内的粒子:方向、重力、旋转、拖动和矢量场。
每种类型的力的设置都使用 MinMaxCurve 类型,该类型也用于粒子系统中。此类型允许您设置简单的均匀值,或者设置更复杂的值,即每个粒子各不相同并且每个粒子的生命周期也各不相同。
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine;
[RequireComponent(typeof(ParticleSystem))] public class ExampleClass : MonoBehaviour { public ParticleSystemForceFieldShape m_Shape = ParticleSystemForceFieldShape.Sphere; public float m_StartRange = 0.0f; public float m_EndRange = 3.0f; public Vector3 m_Direction = Vector3.zero; public float m_Gravity = 0.0f; public float m_GravityFocus = 0.0f; public float m_RotationSpeed = 0.0f; public float m_RotationAttraction = 0.0f; public Vector2 m_RotationRandomness = Vector2.zero; public float m_Drag = 0.0f; public bool m_MultiplyDragByParticleSize = false; public bool m_MultiplyDragByParticleVelocity = false;
private ParticleSystemForceField m_ForceField;
void Start() { // Create a Force Field var go = new GameObject("ForceField", typeof(ParticleSystemForceField)); go.transform.position = new Vector3(0, 2, 0); go.transform.rotation = Quaternion.Euler(new Vector3(90.0f, 0.0f, 0.0f));
m_ForceField = go.GetComponent<ParticleSystemForceField>();
// Configure Particle System transform.position = new Vector3(0, -4, 0); transform.rotation = Quaternion.identity; var ps = GetComponent<ParticleSystem>();
var main = ps.main; main.startSize = new ParticleSystem.MinMaxCurve(0.05f, 0.2f); main.startSpeed = new ParticleSystem.MinMaxCurve(1.5f, 2.5f); main.maxParticles = 100000;
var emission = ps.emission; emission.rateOverTime = 0.0f; emission.burstCount = 1; emission.SetBurst(0, new ParticleSystem.Burst(0.0f, 200, 200, -1, 0.1f));
var shape = ps.shape; shape.shapeType = ParticleSystemShapeType.SingleSidedEdge; shape.radius = 5.0f; shape.radiusMode = ParticleSystemShapeMultiModeValue.BurstSpread; shape.randomPositionAmount = 0.1f; shape.randomDirectionAmount = 0.05f;
var forces = ps.externalForces; forces.enabled = true; }
void Update() { m_ForceField.shape = m_Shape; m_ForceField.startRange = m_StartRange; m_ForceField.endRange = m_EndRange; m_ForceField.directionX = m_Direction.x; m_ForceField.directionY = m_Direction.y; m_ForceField.directionZ = m_Direction.z; m_ForceField.gravity = m_Gravity; m_ForceField.gravityFocus = m_GravityFocus; m_ForceField.rotationSpeed = m_RotationSpeed; m_ForceField.rotationAttraction = m_RotationAttraction; m_ForceField.rotationRandomness = m_RotationRandomness; m_ForceField.drag = m_Drag; m_ForceField.multiplyDragByParticleSize = m_MultiplyDragByParticleSize; m_ForceField.multiplyDragByParticleVelocity = m_MultiplyDragByParticleVelocity; }
void OnGUI() { GUIContent[] shapeLabels = Enum.GetNames(typeof(ParticleSystemForceFieldShape)).Select(n => new GUIContent(n)).ToArray(); m_Shape = (ParticleSystemForceFieldShape)GUI.SelectionGrid(new Rect(25, 25, 400, 25), (int)m_Shape, shapeLabels, 4);
float y = 80.0f; float spacing = 40.0f;
GUI.Label(new Rect(25, y, 140, 30), "Start Range"); m_StartRange = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), m_StartRange, 0.0f, 2.0f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "End Range"); m_EndRange = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), m_EndRange, 2.0f, 3.0f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "Direction"); m_Direction.x = GUI.HorizontalSlider(new Rect(165, y + 5, 40, 30), m_Direction.x, -1.0f, 1.0f); m_Direction.y = GUI.HorizontalSlider(new Rect(210, y + 5, 40, 30), m_Direction.y, -1.0f, 1.0f); m_Direction.z = GUI.HorizontalSlider(new Rect(255, y + 5, 40, 30), m_Direction.z, -1.0f, 1.0f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "Gravity"); m_Gravity = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), m_Gravity, -0.05f, 0.05f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "Gravity Focus"); m_GravityFocus = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), m_GravityFocus, 0.0f, 1.0f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "Rotation Speed"); m_RotationSpeed = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), m_RotationSpeed, -10.0f, 10.0f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "Rotation Attraction"); m_RotationAttraction = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), m_RotationAttraction, 0.0f, 0.01f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "Rotation Randomness"); m_RotationRandomness.x = GUI.HorizontalSlider(new Rect(165, y + 5, 60, 30), m_RotationRandomness.x, 0.0f, 1.0f); m_RotationRandomness.y = GUI.HorizontalSlider(new Rect(230, y + 5, 60, 30), m_RotationRandomness.y, 0.0f, 1.0f); y += spacing;
GUI.Label(new Rect(25, y, 140, 30), "Drag"); m_Drag = GUI.HorizontalSlider(new Rect(165, y + 5, 100, 30), m_Drag, 0.0f, 20.0f); y += spacing;
m_MultiplyDragByParticleSize = GUI.Toggle(new Rect(25, y, 220, 30), m_MultiplyDragByParticleSize, "Multiply Drag by Particle Size"); y += spacing;
m_MultiplyDragByParticleVelocity = GUI.Toggle(new Rect(25, y, 220, 30), m_MultiplyDragByParticleVelocity, "Multiply Drag by Particle Velocity"); y += spacing; } }
directionX | 沿本地 X 轴对力场体积内的粒子施加线性力。 |
directionY | 沿本地 Y 轴对力场体积内的粒子施加线性力。 |
directionZ | 沿本地 Z 轴对力场体积内的粒子施加线性力。 |
drag | 对力场体积内的粒子施加阻力。 |
endRange | 确定用于影响粒子的形状的大小。 |
gravity | 对力场体积内的粒子施加重力。 |
gravityFocus | 使用重力时,请将此值设置在 0 到 1 之间,以控制重力效果的聚焦点。 |
length | 描述使用圆柱体力场形状影响粒子时圆柱体的长度。 |
multiplyDragByParticleSize | 使用阻力时,如果启用此开关,则阻力的强度将乘以粒子的大小。 |
multiplyDragByParticleVelocity | 使用阻力时,如果启用此开关,则阻力的强度将乘以粒子的速度。 |
rotationAttraction | 控制粒子被拖入涡旋运动的强度。 |
rotationRandomness | 针对粒子运动所围绕的力场轴应用随机性。 |
rotationSpeed | 粒子围绕涡旋推进的速度。 |
shape | 选择用于影响粒子的形状类型。 |
startRange | 设置大于 0 的值会创建一个空心力场形状。这将导致粒子在靠近体积中心时不会受到力场影响,而是更大程度上受到 startRange 属性的影响。 |
vectorField | 通过使用包含矢量场数据的 3D 纹理,将力施加到力场体积内的粒子。 |
vectorFieldAttraction | 控制粒子被拖入矢量场运动的强度。 |
vectorFieldSpeed | 粒子在矢量场中推进的速度。 |
enabled | 启用的 Behaviour 可更新,禁用的 Behaviour 不可更新。 |
isActiveAndEnabled | Reports whether a GameObject and its associated Behaviour is active and enabled. |
gameObject | 此组件附加到的游戏对象。始终将组件附加到游戏对象。 |
tag | 此游戏对象的标签。 |
transform | 附加到此 GameObject 的 Transform。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
BroadcastMessage | 调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。 |
CompareTag | Checks the GameObject's tag against the defined tag. |
GetComponent | Gets a reference to a component of type T on the same GameObject as the component specified. |
GetComponentInChildren | Gets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject. |
GetComponentInParent | Gets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject. |
GetComponents | Gets references to all components of type T on the same GameObject as the component specified. |
GetComponentsInChildren | Gets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject. |
GetComponentsInParent | Gets references to all components of type T on the same GameObject as the component specified, and any parent of the GameObject. |
SendMessage | 调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。 |
SendMessageUpwards | 调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。 |
TryGetComponent | 获取指定类型的组件(如果存在)。 |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindAnyObjectByType | Retrieves any active loaded object of Type type. |
FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |