Class Spline
The Spline class is a collection of BezierKnot, the closed/open state, and editing representation.
Inherited Members
Namespace: UnityEngine.Splines
Syntax
[Serializable]
public class Spline : ISpline, IReadOnlyList<BezierKnot>, IReadOnlyCollection<BezierKnot>, IList<BezierKnot>, ICollection<BezierKnot>, IEnumerable<BezierKnot>, IEnumerable
Constructors
Spline()
Default constructor creates a spline with no knots, not closed.
Declaration
public Spline()
Spline(IEnumerable<BezierKnot>, Boolean)
Create a spline from a collection of BezierKnot.
Declaration
public Spline(IEnumerable<BezierKnot> knots, bool closed = false)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<BezierKnot> | knots | A collection of BezierKnot. |
Boolean | closed | Whether the spline is open (has a start and end point) or closed (forms an unbroken loop). |
Spline(Int32, Boolean)
Create a spline with a pre-allocated knot capacity.
Declaration
public Spline(int knotCapacity, bool closed = false)
Parameters
Type | Name | Description |
---|---|---|
Int32 | knotCapacity | The capacity of the knot collection. |
Boolean | closed | Whether the spline is open (has a start and end point) or closed (forms an unbroken loop). |
Properties
Closed
Whether the spline is open (has a start and end point) or closed (forms an unbroken loop).
Declaration
public bool Closed { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Implements
Count
Return the number of knots.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
Int32 |
Implements
IsReadOnly
Returns true if this Spline is read-only, false if it is mutable.
Declaration
public bool IsReadOnly { get; }
Property Value
Type | Description |
---|---|
Boolean |
Implements
Item[Int32]
Get or set the knot at index
.
Declaration
public BezierKnot this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The zero-based index of the element to get or set. |
Property Value
Type | Description |
---|---|
BezierKnot |
Implements
Knots
A collection of BezierKnot.
Declaration
public IEnumerable<BezierKnot> Knots { get; set; }
Property Value
Type | Description |
---|---|
IEnumerable<BezierKnot> |
Methods
Add(BezierKnot)
Adds a knot to the spline.
Declaration
public void Add(BezierKnot item)
Parameters
Type | Name | Description |
---|---|---|
BezierKnot | item | The BezierKnot to add. |
Implements
Add(BezierKnot, TangentMode)
Adds a knot to the spline.
Declaration
public void Add(BezierKnot item, TangentMode mode)
Parameters
Type | Name | Description |
---|---|---|
BezierKnot | item | The BezierKnot to add. |
TangentMode | mode | The tangent mode for this knot. |
Add(BezierKnot, TangentMode, Single)
Adds a knot to the spline.
Declaration
public void Add(BezierKnot item, TangentMode mode, float tension)
Parameters
Type | Name | Description |
---|---|---|
BezierKnot | item | The BezierKnot to add. |
TangentMode | mode | The tangent mode for this knot. |
Single | tension | The modifier value that is used to calculate the magnitude of tangents when the TangentMode is AutoSmooth. Valid values are between 0 and 1. A lower value results in sharper curves, whereas higher values appear more rounded. |
Clear()
Remove all knots from the spline.
Declaration
public void Clear()
Implements
Contains(BezierKnot)
Return true if a knot is present in the spline.
Declaration
public bool Contains(BezierKnot item)
Parameters
Type | Name | Description |
---|---|---|
BezierKnot | item | The BezierKnot to locate. |
Returns
Type | Description |
---|---|
Boolean | Returns true if the knot is found, false if it is not present. |
Implements
Copy(Spline)
Copy the values from copyFrom
to this spline.
Declaration
public void Copy(Spline copyFrom)
Parameters
Type | Name | Description |
---|---|---|
Spline | copyFrom | The spline to copy property data from. |
CopyTo(BezierKnot[], Int32)
Copies the contents of the knot list to an array starting at an index.
Declaration
public void CopyTo(BezierKnot[] array, int arrayIndex)
Parameters
Type | Name | Description |
---|---|---|
BezierKnot[] | array | The destination array to place the copied item in. |
Int32 | arrayIndex | The zero-based index to copy. |
Implements
GetAutoSmoothTension(Int32)
Gets the tension value for the requested index.
Declaration
public float GetAutoSmoothTension(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The knot index to get a tension value for. |
Returns
Type | Description |
---|---|
Single | Returns the tension value for the requested index. |
GetCurve(Int32)
Get a BezierCurve from a knot index.
Declaration
public BezierCurve GetCurve(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The knot index that serves as the first control point for this curve. |
Returns
Type | Description |
---|---|
BezierCurve | A BezierCurve formed by the knot at index and the next knot. |
Implements
GetCurveInterpolation(Int32, Single)
Return the normalized interpolation (t) corresponding to a distance on a BezierCurve.
Declaration
public float GetCurveInterpolation(int curveIndex, float curveDistance)
Parameters
Type | Name | Description |
---|---|---|
Int32 | curveIndex | The zero-based index of the curve. |
Single | curveDistance | The curve-relative distance to convert to an interpolation ratio (also referred to as 't'). |
Returns
Type | Description |
---|---|
Single | The normalized interpolation ratio associated to distance on the designated curve. |
Implements
GetCurveLength(Int32)
Return the length of a curve.
Declaration
public float GetCurveLength(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index |
Returns
Type | Description |
---|---|
Single |
Implements
See Also
GetEnumerator()
Get an enumerator that iterates through the BezierKnot collection.
Declaration
public IEnumerator<BezierKnot> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<BezierKnot> | An IEnumerator that is used to iterate the BezierKnot collection. |
Implements
GetLength()
Return the sum of all curve lengths, accounting for Closed state. Note that this value is not accounting for transform hierarchy. If you require length in world space use
Declaration
public float GetLength()
Returns
Type | Description |
---|---|
Single | Returns the sum length of all curves composing this spline, accounting for closed state. |
Implements
Remarks
This value is cached. It is recommended to call this once in a non-performance critical path to ensure that the cache is valid.
See Also
GetTangentMode(Int32)
Gets the TangentMode for a knot index.
Declaration
public TangentMode GetTangentMode(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index to retrieve TangentMode data for. |
Returns
Type | Description |
---|---|
TangentMode | A TangentMode for the knot at index. |
IndexOf(BezierKnot)
Return the first index of an element matching item.
Declaration
public int IndexOf(BezierKnot item)
Parameters
Type | Name | Description |
---|---|---|
BezierKnot | item | The knot to locate. |
Returns
Type | Description |
---|---|
Int32 | The zero-based index of the knot, or -1 if not found. |
Implements
Insert(Int32, BezierKnot)
Insert a BezierKnot at the specified index
.
Declaration
public void Insert(int index, BezierKnot knot)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The zero-based index to insert the new element. |
BezierKnot | knot | The BezierKnot to insert. |
Implements
Insert(Int32, BezierKnot, TangentMode)
Inserts a BezierKnot at the specified index
.
Declaration
public void Insert(int index, BezierKnot knot, TangentMode mode)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The zero-based index to insert the new element. |
BezierKnot | knot | The BezierKnot to insert. |
TangentMode | mode | The TangentMode to apply to this knot. Tangent modes are enforced when a knot value is set. |
Insert(Int32, BezierKnot, TangentMode, Single)
Adds a BezierKnot at the specified index
.
Declaration
public void Insert(int index, BezierKnot knot, TangentMode mode, float tension)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The zero-based index to insert the new element. |
BezierKnot | knot | The BezierKnot to insert. |
TangentMode | mode | The TangentMode to apply to this knot. Tangent modes are enforced when a knot value is set. |
Single | tension | The modifier value that is used to calculate the magnitude of tangents when the TangentMode is AutoSmooth. Valid values are between 0 and 1. A lower value results in sharper curves, whereas higher values appear more rounded. |
OnSplineChanged()
Invoked any time a spline property is modified.
Declaration
protected virtual void OnSplineChanged()
Remarks
In the editor this can be invoked many times per-frame. Prefer to use UnityEditor.Splines.EditorSplineUtility.afterSplineWasModified when working with splines in the editor.
Remove(BezierKnot)
Removes the first matching knot.
Declaration
public bool Remove(BezierKnot item)
Parameters
Type | Name | Description |
---|---|---|
BezierKnot | item | The BezierKnot to locate and remove. |
Returns
Type | Description |
---|---|
Boolean | Returns true if a matching item was found and removed, false if no match was discovered. |
Implements
RemoveAt(Int32)
Removes the knot at the specified index.
Declaration
public void RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The zero-based index of the element to remove. |
Implements
Resize(Int32)
Change the size of the BezierKnot list.
Declaration
public void Resize(int newSize)
Parameters
Type | Name | Description |
---|---|---|
Int32 | newSize | The new size of the knots collection. |
SetAutoSmoothTension(Int32, Single)
Sets the tension that is used to calculate the magnitude of tangents when the TangentMode is AutoSmooth. Valid values are between 0 and 1. A lower value results in sharper curves, whereas higher values appear more rounded.
Declaration
public void SetAutoSmoothTension(int index, float tension)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The knot index to set a tension value for. |
Single | tension | Set the length of the tangent vectors. |
SetAutoSmoothTension(SplineRange, Single)
Sets the tension that is used to calculate the magnitude of tangents when the TangentMode is AutoSmooth. Valid values are between 0 and 1. A lower value results in sharper curves, whereas higher values appear more rounded.
Declaration
public void SetAutoSmoothTension(SplineRange range, float tension)
Parameters
Type | Name | Description |
---|---|---|
SplineRange | range | The range of knot indices to set a tension value for. |
Single | tension | Set the length of the tangent vectors. |
SetAutoSmoothTensionNoNotify(Int32, Single)
Sets the tension that is used to calculate the magnitude of tangents when the TangentMode is AutoSmooth. Valid values are between 0 and 1. A lower value results in sharper curves, whereas higher values appear more rounded. No changed callbacks will be invoked.
Declaration
public void SetAutoSmoothTensionNoNotify(int index, float tension)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The knot index to set a tension value for. |
Single | tension | Set the length of the tangent vectors for a knot set to AutoSmooth. |
SetAutoSmoothTensionNoNotify(SplineRange, Single)
Set the tension that is used to calculate the magnitude of tangents when the TangentMode is AutoSmooth. Valid values are between 0 and 1. A lower value results in sharper curves, whereas higher values appear more rounded. No changed callbacks will be invoked.
Declaration
public void SetAutoSmoothTensionNoNotify(SplineRange range, float tension)
Parameters
Type | Name | Description |
---|---|---|
SplineRange | range | The range of knot indices to set a tension value for. |
Single | tension | Set the length of the tangent vectors for a knot set to AutoSmooth. |
SetKnot(Int32, BezierKnot, BezierTangent)
Sets the value of a knot at index.
Declaration
public void SetKnot(int index, BezierKnot value, BezierTangent main = BezierTangent.Out)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index of the BezierKnot to set. |
BezierKnot | value | The BezierKnot to set. |
BezierTangent | main | The tangent to prioritize if the tangents are modified to conform with the TangentMode set for this knot. |
SetKnotNoNotify(Int32, BezierKnot, BezierTangent)
Sets the value of a knot index without invoking any change callbacks.
Declaration
public void SetKnotNoNotify(int index, BezierKnot value, BezierTangent main = BezierTangent.Out)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index of the BezierKnot to set. |
BezierKnot | value | The BezierKnot to set. |
BezierTangent | main | The tangent to prioritize if the tangents are modified to conform with the TangentMode set for this knot. |
SetTangentMode(Int32, TangentMode, BezierTangent)
Sets the TangentMode for a knot, and ensures that the rotation and tangent values match the behavior of the tangent mode. This function can modify the contents of the BezierKnot at the specified index.
Declaration
public void SetTangentMode(int index, TangentMode mode, BezierTangent main = BezierTangent.Out)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index of the knot to set. |
TangentMode | mode | The mode to set. |
BezierTangent | main | The tangent direction to align both the In and Out tangent when assigning Continuous or Mirrored tangent mode. |
SetTangentMode(SplineRange, TangentMode, BezierTangent)
Sets the TangentMode for a series of knots, and ensures that the rotation and tangent values match the behavior of the tangent mode. This function can modify the contents of the BezierKnot at the specified indices.
Declaration
public void SetTangentMode(SplineRange range, TangentMode mode, BezierTangent main = BezierTangent.Out)
Parameters
Type | Name | Description |
---|---|---|
SplineRange | range | The range of knot indices to set. |
TangentMode | mode | The mode to set. |
BezierTangent | main | The tangent direction to align both the In and Out tangent with when Continuous or Mirrored tangent mode is assigned . |
SetTangentMode(TangentMode)
Sets the TangentMode for all knots on this spline.
Declaration
public void SetTangentMode(TangentMode mode)
Parameters
Type | Name | Description |
---|---|---|
TangentMode | mode | The TangentMode to apply to each knot. |
SetTangentModeNoNotify(Int32, TangentMode, BezierTangent)
Sets the TangentMode for a knot, and ensures that the rotation and tangent values match the behavior of the tangent mode. No changed callbacks will be invoked. This function can modify the contents of the BezierKnot at the specified index.
Declaration
public void SetTangentModeNoNotify(int index, TangentMode mode, BezierTangent main = BezierTangent.Out)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index of the knot to set. |
TangentMode | mode | The mode to set. |
BezierTangent | main | The tangent direction to align both the In and Out tangent when assigning Continuous or Mirrored tangent mode. |
ToArray()
Create an array of spline knots.
Declaration
public BezierKnot[] ToArray()
Returns
Type | Description |
---|---|
BezierKnot[] | Return a new array copy of the knots collection. |
Warmup()
Ensure that all caches contain valid data. Call this to avoid unexpected performance costs when accessing spline data. Caches remain valid until any part of the spline state is modified.
Declaration
public void Warmup()
Events
Changed
Invoked any time a spline is modified.
Declaration
public static event Action<Spline, int, SplineModification> Changed
Event Type
Type | Description |
---|---|
Action<Spline, Int32, SplineModification> |
Remarks
First parameter is the target Spline that the event is raised for, second parameter is the knot index and the third parameter represents the type of change that occured. If the event does not target a specific knot, the second parameter will have the value of -1.
In the editor this callback can be invoked many times per-frame. Prefer to use AfterSplineWasModified when working with splines in the editor.
See Also
Explicit Interface Implementations
IEnumerable.GetEnumerator()
Gets an enumerator that iterates through the BezierKnot collection.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator | An IEnumerator that is used to iterate the BezierKnot collection. |