using System;
namespace UnityEngine.XR.ARSubsystems
{
///
/// A Guid that can be serialized by Unity. The 128-bit Guid
/// is stored as two 64-bit ulongs. Refer to the creation utility at
/// for more information.
///
[Serializable]
public struct SerializableGuid : IEquatable
{
[SerializeField]
ulong m_GuidLow;
internal ulong guidLow => m_GuidLow;
[SerializeField]
ulong m_GuidHigh;
internal ulong guidHigh => m_GuidHigh;
///
/// Constructs a from two 64-bit ulongs.
///
/// The low 8 bytes of the Guid.
/// The high 8 bytes of the Guid.
public SerializableGuid(ulong guidLow, ulong guidHigh)
{
m_GuidLow = guidLow;
m_GuidHigh = guidHigh;
}
///
/// Constructs a from a .
///
/// The Guid used to create the SerializableGuid
public SerializableGuid(Guid guid)
{
var guidParts = GuidUtil.Decompose(guid);
m_GuidLow = guidParts.low;
m_GuidHigh = guidParts.high;
}
static readonly SerializableGuid k_Empty = new SerializableGuid(0, 0);
///
/// Used to represent System.Guid.Empty (that is, a GUID whose values are all zeros).
///
public static SerializableGuid empty => k_Empty;
///
/// Reconstructs the Guid from the serialized data.
///
public Guid guid => GuidUtil.Compose(m_GuidLow, m_GuidHigh);
///
/// Convert from to `SerializableGuid` using the constructor.
///
/// The TrackableId to convert.
/// The SerializableGuid.
public static implicit operator SerializableGuid(TrackableId trackableId)
{
return new SerializableGuid(trackableId.subId1, trackableId.subId2);
}
///
/// Generates a hash suitable for use with containers like `HashSet` and `Dictionary`.
///
/// A hash code generated from this object's fields.
public override int GetHashCode() => HashCodeUtil.Combine(m_GuidLow.GetHashCode(), m_GuidHigh.GetHashCode());
///
/// Tests for equality.
///
/// The `object` to compare against.
/// `True` if is of type and
/// also returns `true`; otherwise `false`.
public override bool Equals(object obj) => (obj is SerializableGuid) && Equals((SerializableGuid)obj);
///
/// Generates a string representation of the Guid. Same as .ToString().
/// See Microsoft's documentation
/// for more details.
///
/// A string representation of the Guid.
public override string ToString()
{
return $"{m_GuidLow:X16}-{m_GuidHigh:X16}";
}
///
/// Generates a string representation of the Guid. Same as .ToString(format).
///
/// A single format specifier that indicates how to format the value of the Guid.
/// See Microsoft's documentation
/// for more details.
/// A string representation of the Guid.
public string ToString(string format) => guid.ToString(format);
///
/// Generates a string representation of the Guid. Same as .ToString(format, provider).
///
/// A single format specifier that indicates how to format the value of the Guid.
/// See Microsoft's documentation
/// for more details.
/// An object that supplies culture-specific formatting information.
/// A string representation of the Guid.
public string ToString(string format, IFormatProvider provider) => guid.ToString(format, provider);
///
/// Tests for equality.
///
/// The other to compare against.
/// `True` if every field in is equal to this , otherwise false.
public bool Equals(SerializableGuid other)
{
return
(m_GuidLow == other.m_GuidLow) &&
(m_GuidHigh == other.m_GuidHigh);
}
///
/// Tests for equality. Same as .
///
/// The left-hand side of the comparison.
/// The right-hand side of the comparison.
/// `True` if is equal to , otherwise `false`.
public static bool operator ==(SerializableGuid lhs, SerializableGuid rhs) => lhs.Equals(rhs);
///
/// Tests for inequality. Same as `!`.
///
/// The left-hand side of the comparison.
/// The right-hand side of the comparison.
/// `True` if is not equal to , otherwise `false`.
public static bool operator !=(SerializableGuid lhs, SerializableGuid rhs) => !lhs.Equals(rhs);
}
}