Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question New LoadObjectAsync API

Discussion in 'Asset Database' started by TJHeuvel-net, Apr 12, 2023.

  1. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    818
    In 2022.2 a new API has been added to load objects async, i'd love to use it!

    But i have no idea what a local id is, and how to get one. Lets say i did a FindAssets, i now have a list of guids. How do i turn that in a local file id?

    The example code in the documentation assumes the object is already loaded. Thats rather useless for me, i want to load the object for the first time. Just like how i can do with the sync LoadAssetAtPath.
     
    Last edited: Apr 12, 2023
  2. TobiasThomsen

    TobiasThomsen

    Unity Technologies

    Joined:
    Feb 15, 2021
    Posts:
    18
    Hi TJHeuvel-net - The GUID identifies the asset, and can be found inside the corresponding .meta file. An asset can contain multiple objects that references each-other via local id's that can be found inside the asset.

    Below is an example of the content of an empty prefab I have created:

    %YAML 1.1
    %TAG !u! tag:unity3d.com,2011:
    --- !u!1 &6639020273073066883
    GameObject:
    m_ObjectHideFlags: 0
    m_CorrespondingSourceObject: {fileID: 0}
    m_PrefabInstance: {fileID: 0}
    m_PrefabAsset: {fileID: 0}
    serializedVersion: 6
    m_Component:
    - component: {fileID: 3373097609167135116}
    m_Layer: 0
    m_Name: foo
    m_TagString: Untagged
    m_Icon: {fileID: 0}
    m_NavMeshLayer: 0
    m_StaticEditorFlags: 0
    m_IsActive: 1
    --- !u!4 &3373097609167135116
    Transform:
    m_ObjectHideFlags: 0
    m_CorrespondingSourceObject: {fileID: 0}
    m_PrefabInstance: {fileID: 0}
    m_PrefabAsset: {fileID: 0}
    m_GameObject: {fileID: 6639020273073066883}
    m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
    m_LocalPosition: {x: 0, y: 0, z: 0}
    m_LocalScale: {x: 1, y: 1, z: 1}
    m_ConstrainProportionsScale: 0
    m_Children: []
    m_Father: {fileID: 0}
    m_RootOrder: 2
    m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

    In this case you can see two file id's --- !u!1 &6639020273073066883 and --- !u!4 &3373097609167135116 (the actual number highlighted in bold is the actual id).
    The first ID represents the root game object of the prefab, and the second one represents the transform component on the prefab.
    If you then look at the data in the transform (found under Transform inside the content of the prefab) you will see that the transform references the game object via the game objects fileID (m_GameObject: {fileID: 6639020273073066883}).

    In order to get the file id's of an asset, Unity needs to load the given asset to see which file id's are there, this means that you cannot use the LoadAssetAsync API, without having loaded the asset.

    I hope this helped, otherwise feel free to reach out again on this thread! :)
     
  3. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    818
    Thanks for the in depth response!

    I do have to say its quite odd to me as a user that there is an API to load an object, that only works when i have already loaded an object. Would there be any way to have a LoadAssetAtPath that is async, where i only need to pass a path and a type?
     
    omer_unity370 likes this.
  4. TobiasThomsen

    TobiasThomsen

    Unity Technologies

    Joined:
    Feb 15, 2021
    Posts:
    18
    Yes - the LoadObjectAsync is only for very specific use-cases where you have the fileID's (this would probably be a valuable addition to the current documentation).
    It is currently not possible to load assets asynchronously, due to restrictions to the ways the underlying systems work.
     
    TJHeuvel-net likes this.