Search Unity

Prefab Entity Not Updating

Discussion in 'Entity Component System' started by Hobnob93, May 10, 2020.

  1. Hobnob93

    Hobnob93

    Joined:
    May 19, 2017
    Posts:
    5
    Hello, hope you're all well!

    I've encountered an issue where a prefab that is converted to an Entity no longer changes when I add/remove new authoring components.

    I'll elaborate;
    I have a main scene and a sub-scene within it.
    Within the sub-scene is a GameObject with a SpawnerAuthoring component attached.

    The SpawnerAuthoring is basically the following (with some variables removed for brevity):
    Code (CSharp):
    1. public class SpawnerAuthoring : MonoBehaviour, IConvertGameObjectToEntity, IDeclareReferencedPrefabs
    2. {
    3.     [SerializeField]
    4.     private GameObject personPrefab;
    5.  
    6.     public void DeclareReferencedPrefabs(List<GameObject> referencedPrefabs)
    7.     {
    8.         referencedPrefabs.Add(personPrefab);
    9.     }
    10.  
    11.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    12.     {
    13.         var prefabAsEntity = conversionSystem.GetPrimaryEntity(personPrefab);
    14.  
    15.         dstManager.AddComponentData(entity, new Spawner
    16.         {
    17.             Prefab = prefabAsEntity
    18.         });
    19.     }
    20. }
    I then drag the prefab (which has a ConvertToEntity component attached) into the Authoring component instance. Up until now, any changes made to this prefab (like adding new components) was seen in both the Prefab Entity as well as those entities spawned based on that prefab.

    Now though, if I add anything to this prefab, as well as remove anything, the Entity Prefab does not reflect this in the Entity Debugger. Any systems counting on a given component being on the spawned entities won't pick the entities up as they do not have the component. I'm 100% certain it's the correct prefab being referenced; clicking it in the Spawner GameObject's inspector takes me straight to it.

    I've tried closing and reopening Unity. The components are structs, and they do implement
    IComponentData
    and just contain a float. Even tried with empty components (tags). Whether it has a manual authoring script or using the generate attribute, it has the same issue. I've tried re-assigning the prefab as well. Only thing I haven't tried is deleting the prefab and creating it again.

    Weirdly, this issue occurs when the sub-scene is closed for editing. If I open the sub-scene for editing, suddenly the prefab entity is correct and is as it should be when I click Play. I shouldn't have to have the sub-scene open for it to work though, right?

    Just wondering if anyone has experienced this before...I'm sure I'm just doing something wrong.

    Any help greatly appreciated - thank you for reading this far!
     
  2. LazyGameDevZA

    LazyGameDevZA

    Joined:
    Nov 10, 2016
    Posts:
    143
    What's happening here is what I'd presume to be an issue with subscenes not being fully aware of prefab lifecycles. The subscene generates a runtime-representation of the prefab and stores it in a cache file. This is loaded directly into memory when the subscene is loaded for entering play-mode. With the subscene closed in the scene hierarchy there it won't pick up that changes are made to the prefab so that it can update it's representation of the prefab in it's cache file.

    I suspect Unity will provide a fix for this at some stage, but it's ultimately these optimisations that make loading subscenes so damn quick. The data for it is already correctly laid out on disk as it would be in memory, so the load operation doesn't have to go through the conversion process again and can just dump it and start running correctly.
     
    Hobnob93 likes this.
  3. Hobnob93

    Hobnob93

    Joined:
    May 19, 2017
    Posts:
    5
    Thank you for taking the time to explain that; I figured it would be something to do with caching of sorts. As an optimisation for quick-loading of these subscenes, it does indeed make sense.

    Even if it's not considered an actual bug, it's a nuance to be aware of for the future.