using System;
using Unity.Mathematics;
namespace UnityEngine.Splines
{
///
/// SplineInfo is used to wrap a reference to the and index within that container to
/// describe a .
///
[Serializable]
public struct SplineInfo : IEquatable, ISerializationCallbackReceiver
{
[SerializeField]
Object m_Object;
[SerializeReference]
ISplineContainer m_Container;
[SerializeField]
int m_SplineIndex;
///
/// If the referenced is a UnityEngine.Object this field contains that object.
///
public Object Object => m_Object;
///
/// The that contains the spline.
///
public ISplineContainer Container
{
get => m_Container ?? m_Object as ISplineContainer;
set => m_Container = value;
}
///
/// The associated of the target. This may be null if the container is not
/// a MonoBehaviour.
///
public Transform Transform => Object is Component component ? component.transform : null;
///
/// A reference to the . This may be null if the container or index are
/// invalid.
///
public Spline Spline => Container != null && Index > -1 && Index < Container.Splines.Count
? Container.Splines[Index]
: null;
///
/// The index of the spline in the enumerable returned by the .
///
public int Index
{
get => m_SplineIndex;
set => m_SplineIndex = value;
}
///
/// Matrix that transforms the from local space into world space.
///
public float4x4 LocalToWorld => Transform != null ? (float4x4)Transform.localToWorldMatrix : float4x4.identity;
///
/// Create a new object.
///
/// The that contains the spline.
/// The index of the spline in the enumerable returned by the .
public SplineInfo(ISplineContainer container, int index)
{
m_Container = container;
m_Object = container as Object;
m_SplineIndex = index;
}
///
/// Compare against another for equality.
///
/// The other instance to compare against.
///
/// Returns true when is a and the values of each instance are
/// identical.
///
public bool Equals(SplineInfo other)
{
return Equals(Container, other.Container) && Index == other.Index;
}
///
/// Compare against an object for equality.
///
/// The object to compare against.
///
/// Returns true when is a and the values of each instance are
/// identical.
///
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is SplineInfo other && Equals(other);
}
///
/// Calculate a hash code for this info.
///
///
/// A hash code for the .
///
public override int GetHashCode()
{
unchecked
{
var hashCode = (Container != null ? Container.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Index;
return hashCode;
}
}
///
/// Called before this object is serialized by the Editor.
///
public void OnBeforeSerialize()
{
if (m_Container is Object obj)
{
m_Object = obj;
m_Container = null;
}
}
///
/// Called after this object is deserialized by the Editor.
///
public void OnAfterDeserialize()
{
m_Container ??= m_Object as ISplineContainer;
}
}
}