Search Unity

Question Are asset InstanceIDs persistent?

Discussion in 'Editor & General Support' started by MechaWolf99, May 17, 2021.

  1. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    I was wondering if InstanceIDs for assets are persistent between editor sessions?
    I thought they were not, however in my testing they seem to be.
    And the LazyLoadReference<T> class seems to only serialize and load objects using their InstanceID.

    But if this is the case then I don't understand what is the point of asset GUIDs as well.

    If they are not, then can someone please explain how LazyLoadReference works then?

    Here is a link to the source code for LazyLoadReference, and here is a link to the docs. That way if anyone wants they can take a look for themselves.
     
    Last edited: May 17, 2021
  2. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I have the same question. Have you found why not GUID but just an instanceID with 32-bit integer is enough?
     
  3. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    I have! I made a forum post about it here.
    In short, InstanceID is based on the load order of the object. So, use GUID. The way that LazyLoadReference works from what I understand is that there is additional data on the C++ side which is used to persist a reference between the instanceID and the object.
     
    watsonsong likes this.
  4. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    It is funny that the LazyLoadReference store in the file like this:
    Code (CSharp):
    1. - {fileID: 102900000, guid: 3f7c629c15a652343b1ef34c4c6fcbc6, type: 3}
    It seems that the `[SerializeField] m_InstanceID` is cheating me. The c++ part do something magic. I don't known why it design like this. It could be easy to record the fileId and GUID directly in .cs part.