using System; using UnityEngine; namespace Unity.XR.CoreUtils { /// /// A Guid that can be serialized by Unity. /// /// /// The 128-bit Guid /// is stored as two 64-bit ulongs. See the creation utility, /// , for additional information. /// [Serializable] public struct SerializableGuid : IEquatable { static readonly SerializableGuid k_Empty = new SerializableGuid(0, 0); [SerializeField] [HideInInspector] ulong m_GuidLow; [SerializeField] [HideInInspector] ulong m_GuidHigh; /// /// Represents System.Guid.Empty, a GUID whose value is all zeros. /// public static SerializableGuid Empty => k_Empty; /// /// Reconstructs the Guid from the serialized data. /// public Guid Guid => GuidUtil.Compose(m_GuidLow, m_GuidHigh); /// /// Constructs a from two 64-bit ulong values. /// /// 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; } /// /// Gets the hash code for this SerializableGuid. /// /// The hash code. public override int GetHashCode() { unchecked { var hash = m_GuidLow.GetHashCode(); return hash * 486187739 + m_GuidHigh.GetHashCode(); } } /// /// Checks if this SerializableGuid is equal to an object. /// /// The object to check. /// True if is a SerializableGuid with the same field values. public override bool Equals(object obj) { if (!(obj is SerializableGuid serializableGuid)) return false; return Equals(serializableGuid); } /// /// Generates a string representation of the Guid. Same as . /// See Microsoft's documentation /// for more details. /// /// A string representation of the Guid. public override string ToString() => Guid.ToString(); /// /// Generates a string representation of the Guid. Same as . /// /// 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 . /// /// 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); /// /// Check if this SerializableGuid is equal to another SerializableGuid. /// /// The other SerializableGuid /// True if this SerializableGuid has the same field values as the other one. public bool Equals(SerializableGuid other) { return m_GuidLow == other.m_GuidLow && m_GuidHigh == other.m_GuidHigh; } /// /// Perform an equality operation on two SerializableGuids. /// /// The left-hand SerializableGuid. /// The right-hand SerializableGuid. /// True if the SerializableGuids are equal to each other. public static bool operator ==(SerializableGuid lhs, SerializableGuid rhs) => lhs.Equals(rhs); /// /// Perform an inequality operation on two SerializableGuids. /// /// The left-hand SerializableGuid. /// The right-hand SerializableGuid. /// True if the SerializableGuids are not equal to each other. public static bool operator !=(SerializableGuid lhs, SerializableGuid rhs) => !lhs.Equals(rhs); } }