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 Find an Entity's SceneGUID at runtime

Discussion in 'Entity Component System' started by PublicEnumE, May 30, 2021.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    I'm trying to access the SceneGUID associated with an Entity's subscene at runtime. I need to gather some information about the scene, on the first time that it loads. But scenes can be loaded multiple times, so I need to use the SceneGUID as key to make sure I'm not logging information multiple times for the same subscene.

    I need to access this information from inside of a job.

    So far, this info seems to be very elusive. Each Entity that was converted as part of a subscene has both a "SceneTag" and a "SceneSection" component, but both of these components are ISharedComponentData. So there's no way to access them from inside of a job.

    It seems like Unity has arranged the data this way in order to group Entities from the same scene in their own chunks. That would make it easy to perform operations on all entities from a specific scene (like to delete them all). But it doesn't allow for data access in the other direction: Finding out which scene an individual Entity belongs to.

    I've also tried giving up on accessing the SceneGUID through pre-existing Components , and instead storing it myself in a custom component during the Conversion process. I've looked for any way to locate the SceneGUID for an Entity's scene during Conversion. But similarly, it seems incredibly difficult - if not impossible - to access any Scene information from inside a GameObjectConversionSystem. I've tried everything I can think of so far. I'm flummoxed.

    If anyone has any ideas, please let me know. I've been trying to find a solution to this problem for a few days now, with absolutely no luck. I never expected this to be such a challenge.
     
    Last edited: May 30, 2021
  2. mattdymott

    mattdymott

    Joined:
    Dec 27, 2016
    Posts:
    29
    The TinyRacing sample has some SubScene loading code:
    ProjectTinySamples/UpdateMainMenu.cs at master · Unity-Technologies/ProjectTinySamples (github.com)

    I think what they do is create an IComponentData tag along with an Authoring class and add it to the SubScene that you want to access. Then you add a ConvertToEntity component but select the method as Injection so the SubScene is not destroyed. Then you can access it as a SingletonEntity using the tag you've created and then get the SceneReference which has the SceneGUID.