ParticleSystemForceField

class in UnityEngine

/

Inherits from:Component

Switch to Manual

Description

Script interface for Particle System Force Fields.

Particle System Force Fields can be used to influence groups of particles that enter each field's zone of influence.

The shape of the Force Field can be set to a variety of shapes, and how the particles are affected is controlled by various properties in the Force Field.

As part of choosing the shape, you may define a start and end range. The end range describes the maximum extent of the shape, and the start range can be used to create a hollow shape.

A number of forces can be applied to particles that are within this volume: directional, gravitational, rotational, drag, and a vector field.

The settings for each type of force make use of the MinMaxCurve type, which is also used in the Particle System. This type allows you to set simple uniform values, or more complicated values that vary per-particle, and vary over the lifetime of each particle.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

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; } }

Variables

directionXApply a linear force along the local X axis to particles within the volume of the Force Field.
directionYApply a linear force along the local Y axis to particles within the volume of the Force Field.
directionZApply a linear force along the local Z axis to particles within the volume of the Force Field.
dragApply drag to particles within the volume of the Force Field.
endRangeDetermines the size of the shape used for influencing particles.
gravityApply gravity to particles within the volume of the Force Field.
gravityFocusWhen using the gravity force, set this value between 0 and 1 to control the focal point of the gravity effect.
lengthDescribes the length of the Cylinder when using the Cylinder Force Field shape to influence particles.
multiplyDragByParticleSizeWhen using Drag, the drag strength will be multiplied by the size of the particles if this toggle is enabled.
multiplyDragByParticleVelocityWhen using Drag, the drag strength will be multiplied by the speed of the particles if this toggle is enabled.
rotationAttractionControls how strongly particles are dragged into the vortex motion.
rotationRandomnessApply randomness to the Force Field axis that particles will travel around.
rotationSpeedThe speed at which particles are propelled around a vortex.
shapeSelects the type of shape used for influencing particles.
startRangeSetting a value greater than 0 creates a hollow Force Field shape. This will cause particles to not be affected by the Force Field when closer to the center of the volume than the startRange property.
vectorFieldApply forces to particles within the volume of the Force Field, by using a 3D texture containing vector field data.
vectorFieldAttractionControls how strongly particles are dragged into the vector field motion.
vectorFieldSpeedThe speed at which particles are propelled through the vector field.

Inherited members

Variables

gameObject此组件附加到的游戏对象。始终将组件附加到游戏对象。
tag此游戏对象的标签。
transform附加到此 GameObject 的 Transform。
hideFlagsShould the object be hidden, saved with the Scene or modifiable by the user?
name对象的名称。

Public Functions

BroadcastMessage调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。
CompareTag此游戏对象是否使用 tag 进行了标记?
GetComponent如果游戏对象附加了类型为 type 的组件,则将其返回,否则返回 null。
GetComponentInChildren使用深度首次搜索返回 GameObject 或其任何子项中类型为 type 的组件。
GetComponentInParent返回 GameObject 或其任何父项中类型为 type 的组件。
GetComponents返回 GameObject 中类型为 type 的所有组件。
GetComponentsInChildren返回 GameObject 或其任何子项中类型为 type 的所有组件。
GetComponentsInParent返回 GameObject 或其任何父项中类型为 type 的所有组件。
SendMessage调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。
SendMessageUpwards调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。
GetInstanceID返回对象的实例 ID。
ToString返回 GameObject 的名称。

Static Functions

Destroy删除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 /obj/。强烈建议您改用 Destroy。
DontDestroyOnLoadDo not destroy the target Object when loading a new Scene.
FindObjectOfType返回第一个类型为 type 的已加载的激活对象。
FindObjectsOfType返回所有类型为 type 的已加载的激活对象的列表。
Instantiate克隆 original 对象并返回克隆对象。

Operators

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