Created
June 19, 2026 11:25
-
-
Save darbotron/918e836c9e5602e550d1e136a8e67d18 to your computer and use it in GitHub Desktop.
UnityObjectId - wrapper to smooth over migration from int UnityEngine.Object.GetInstanceId() to the EntityId UnityEngine.Object.GetEntityId()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ////////////////////////////////////////////////////////////////////////////// | |
| // This source file is part of the "Darbotron Common" UPM Package | |
| // | |
| // File content © 2026 Darbotron Ltd | |
| // | |
| // Licensed under the MIT license: https://opensource.org/license/mit | |
| ////////////////////////////////////////////////////////////////////////////// | |
| //#define FORCE_USE_ENTITY_ID | |
| using System; | |
| namespace Darbotron.Common | |
| { | |
| #if UNITY_6000_3_OR_NEWER | |
| public struct UnityObjectId : IEquatable< UnityObjectId >, IComparable< UnityObjectId > | |
| { | |
| public static UnityObjectId GetUnityObjectId( UnityEngine.Object unityObj ) => new UnityObjectId{ m_entityId = unityObj.GetEntityId() }; | |
| public static implicit operator UnityEngine.EntityId( UnityObjectId id ) => id.m_entityId; | |
| public UnityEngine.Object GetObject() => UnityEngine.Resources.EntityIdToObject( m_entityId ); | |
| #if UNITY_EDITOR | |
| public UnityEngine.Object EditorGetObject() => UnityEditor.EditorUtility.EntityIdToObject( m_entityId ); | |
| #else | |
| public UnityEngine.Object EditorGetObject() => throw new Exception( $"cannot be used in builds" ); | |
| #endif | |
| public bool Equals( UnityObjectId other ) => ( m_entityId == other.m_entityId ); | |
| public int CompareTo( UnityObjectId other ) => ( m_entityId - other.m_entityId ); | |
| public override bool Equals( object obj ) | |
| { | |
| return obj is UnityObjectId other && Equals( other ); | |
| } | |
| public override int GetHashCode() | |
| { | |
| return m_entityId; | |
| } | |
| public static bool operator==( UnityObjectId left, UnityObjectId right ) => left.Equals( right ); | |
| public static bool operator!=( UnityObjectId left, UnityObjectId right ) => ! left.Equals( right ); | |
| private UnityEngine.EntityId m_entityId; | |
| } | |
| #else // #if UNITY_6000_3_OR_NEWER ... | |
| public struct UnityObjectId : IEquatable< UnityObjectId >, IComparable< UnityObjectId > | |
| { | |
| public static UnityObjectId GetUnityObjectId( UnityEngine.Object unityObj ) => new UnityObjectId{ m_instanceId = unityObj.GetInstanceID() }; | |
| public static implicit operator int( UnityObjectId id ) => id.m_instanceId; | |
| public UnityEngine.Object GetObject() => UnityEngine.Resources.InstanceIDToObject( m_instanceId ); | |
| #if UNITY_EDITOR | |
| public UnityEngine.Object EditorGetObject() => UnityEditor.EditorUtility.InstanceIDToObject( m_instanceId ); | |
| #else | |
| public UnityEngine.Object EditorGetObject() => throw new Exception( $"cannot be used in builds" ); | |
| #endif | |
| public bool Equals( UnityObjectId other ) => ( m_instanceId == other.m_instanceId ); | |
| public int CompareTo( UnityObjectId other ) => ( m_instanceId - other.m_instanceId ); | |
| public override bool Equals( object obj ) | |
| { | |
| return obj is UnityObjectId other && Equals( other ); | |
| } | |
| public override int GetHashCode() | |
| { | |
| return m_instanceId; | |
| } | |
| public static bool operator==( UnityObjectId left, UnityObjectId right ) => left.Equals( right ); | |
| public static bool operator!=( UnityObjectId left, UnityObjectId right ) => ! left.Equals( right ); | |
| private int m_instanceId; | |
| } | |
| #endif // #if UNITY_6000_3_OR_NEWER ... #else ... | |
| public static class UnityObjectIdExtensions | |
| { | |
| public static UnityObjectId GetUnityObjectId( this UnityEngine.Object unityObject ) => UnityObjectId.GetUnityObjectId( unityObject ); | |
| } | |
| } // namespace Darbotron.Common |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment