Version: 2023.2

ParticleSystemForceField

class in UnityEngine

/

继承自:Behaviour

切换到手册

描述

粒子系统力场的脚本接口。

可以使用粒子系统力场来影响进入每个力场影响区的粒子群。

力场的形状可以设置为各种形状,粒子受影响的方式由力场中的各种属性控制。

在您选择形状的过程中,可以定义开始范围和结束范围。结束范围描述了形状的最大范围,开始范围可用于创建中空形状。

可以将许多力应用于此体积内的粒子:方向、重力、旋转、拖动和矢量场。

每种类型的力的设置都使用 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 不可更新。
isActiveAndEnabledReports whether a GameObject and its associated Behaviour is active and enabled.
gameObject此组件附加到的游戏对象。始终将组件附加到游戏对象。
tag此游戏对象的标签。
transform附加到此 GameObject 的 Transform。
hideFlags该对象应该隐藏、随场景一起保存还是由用户修改?
name对象的名称。

公共函数

BroadcastMessage调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。
CompareTagChecks the GameObject's tag against the defined tag.
GetComponentGets a reference to a component of type T on the same GameObject as the component specified.
GetComponentInChildrenGets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject.
GetComponentInParentGets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject.
GetComponentsGets references to all components of type T on the same GameObject as the component specified.
GetComponentsInChildrenGets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject.
GetComponentsInParentGets 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获取指定类型的组件(如果存在)。
GetInstanceIDGets the instance ID of the object.
ToString返回对象的名称。

静态函数

Destroy移除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 /obj/。强烈建议您改用 Destroy。
DontDestroyOnLoad在加载新的 Scene 时,请勿销毁 Object。
FindAnyObjectByTypeRetrieves any active loaded object of Type type.
FindFirstObjectByTypeRetrieves the first active loaded object of Type type.
FindObjectsByTypeRetrieves a list of all loaded objects of Type type.
Instantiate克隆 original 对象并返回克隆对象。

运算符

bool该对象是否存在?
operator !=比较两个对象是否引用不同的对象。
operator ==比较两个对象引用,判断它们是否引用同一个对象。