using System; using Unity.Mathematics; namespace UnityEngine.Splines { /// /// Wrapper for accessing a value stored on through one of the /// embedded key value collections. It is not required to use this class to access embedded /// , however it does provide some convenient functionality for working with this data /// in the Inspector. /// /// /// /// /// /// [Serializable] public class EmbeddedSplineData { [SerializeField] SplineContainer m_Container; [SerializeField] int m_SplineIndex; [SerializeField] EmbeddedSplineDataType m_Type; [SerializeField] string m_Key; /// /// The that holds the . /// public SplineContainer Container { get => m_Container; set => m_Container = value; } /// /// The index of the on the . /// public int SplineIndex { get => m_SplineIndex; set => m_SplineIndex = value; } /// /// The type of data stored by the collection. Embedded /// is restricted to a pre-defined set of primitive types. /// public EmbeddedSplineDataType Type { get => m_Type; set => m_Type = value; } /// /// A unique string value used to identify and access a collection stored in a /// . /// public string Key { get => m_Key; set => m_Key = value; } /// /// Create a new instance with no parameters. /// public EmbeddedSplineData() : this(null, EmbeddedSplineDataType.Float) { } /// /// Create a new with parameters. /// /// A unique string value used to identify and access a collection /// stored in a . /// The type of data stored by the collection. /// The that holds the . /// The index of the on the . public EmbeddedSplineData( string key, EmbeddedSplineDataType type, SplineContainer container = null, int splineIndex = 0) { m_Container = container; m_SplineIndex = splineIndex; m_Key = key; m_Type = type; } /// /// Attempt to get a reference to the described by this object. /// /// A if the and are /// valid, otherwise null. /// Returns true if the and are valid, otherwise /// false. public bool TryGetSpline(out Spline spline) { if(Container == null || SplineIndex < 0 || SplineIndex >= Container.Splines.Count) spline = null; else spline = Container.Splines[SplineIndex]; return spline != null; } /// /// Attempt to get a reference to the described by this object. /// /// A reference if the , /// , , and are valid, otherwise null. /// Returns true if a value exists, otherwise false. /// An exception is thrown if the requested /// does not match the . public bool TryGetFloatData(out SplineData data) { if(Type != EmbeddedSplineDataType.Float) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(float)}"); return Container.Splines[SplineIndex].TryGetFloatData(Key, out data); } /// public bool TryGetFloat4Data(out SplineData data) { if(Type != EmbeddedSplineDataType.Float4) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(float4)}"); return Container.Splines[SplineIndex].TryGetFloat4Data(Key, out data); } /// public bool TryGetIntData(out SplineData data) { if(Type != EmbeddedSplineDataType.Int) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(int)}"); return Container.Splines[SplineIndex].TryGetIntData(Key, out data); } /// public bool TryGetObjectData(out SplineData data) { if(Type != EmbeddedSplineDataType.Object) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(Object)}"); return Container.Splines[SplineIndex].TryGetObjectData(Key, out data); } /// /// Returns a for and . If an instance matching /// the key and type does not exist, a new entry is appended to the internal collection and returned. /// Note that this is a reference to the stored , not a copy. Any modifications to /// this collection will affect the data. /// /// A of the requested type. /// An exception is thrown if the requested /// does not match the . public SplineData GetOrCreateFloatData() { if(Type != EmbeddedSplineDataType.Float) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(float)}"); return Container.Splines[SplineIndex].GetOrCreateFloatData(Key); } /// public SplineData GetOrCreateFloat4Data() { if(Type != EmbeddedSplineDataType.Float4) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(float4)}"); return Container.Splines[SplineIndex].GetOrCreateFloat4Data(Key); } /// public SplineData GetOrCreateIntData() { if(Type != EmbeddedSplineDataType.Int) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(int)}"); return Container.Splines[SplineIndex].GetOrCreateIntData(Key); } /// public SplineData GetOrCreateObjectData() { if(Type != EmbeddedSplineDataType.Object) throw new InvalidCastException($"EmbeddedSplineDataType {Type} does not match requested SplineData collection: {typeof(Object)}"); return Container.Splines[SplineIndex].GetOrCreateObjectData(Key); } } }