#Baking/Referencing prefab from BGDatabase

1 messages · Page 1 of 1 (latest)

keen wind
#

So, I need a lot of prefab reference from bansheegz BGDatabse as my screenshot
I have tried the prefab baking from authoring itself and works fine, but it doesnt work for database

When I used Buffer for the reference, it works on Editor, but crash after splash unity screen on android Build

// authoring
public GameObject dropItemsPrefab => DB_Prefab_ECS.GetEntity(0).Fieldprefabse;

// IBufferElementData
public struct DropItemsBuffer : IBufferElementData
{
    public Entity prefab;
}

When I used the prefab reference, it getting error, can't convert to Hash128

// result error 
// cannot convert from 'UnityEngine.GameObject' to 'Unity.Entities.Hash128'
EntityPrefabReference prefabReference = new EntityPrefabReference(DB_Prefab_ECS.GetEntity(0).Fieldprefabse);

I assume that the Bake started before the Database load and causing the crash? and its getting null?
Any Ideas to reference/bake the prefab from database?

timid stratus
#

Baking only works on the editor. Baking related code never exists in the build.

#

The document of EntityPrefabReference said:

Weak reference to entity prefab. Entity prefabs are GameObjects that have been fully converted into entity data during the import/build process.

#

That means your baking code must be able to convert these gameobjects into entities first.

#

The constructor EntityPrefabReference(GameObject) only works for baking, means it happens on the editor only. Everything must be converted into ECS-compatible data.

#

At runtime, your subscenes contain only ECS data, nothing else.

#

I experienced crashes before when trying to store managed references in subscenes. I suspect this is the case for you since it also happened for some people. Now I use a custom strategy to connect managed objects with entities at runtime.

keen wind
timid stratus
#

The concept is quite simple: assign each GO a unique ID, store the pair in a kind of database (like yours), then at runtime we can link each GO with its entity via this ID. Favourably the ID should be composable or computable at runtime via a set of logic and data pieces.

#

At runtime, the entities are created first. There is a system to collect data from newly created entities to spawn the associated GOs.

keen wind
#

are there any equivalent of GetEntity(GameObject) in BakingSystem?
Is it possible to convert my gameObject to Entity in BakingSystem?

[UpdateInGroup(typeof(PostBakingSystemGroup), OrderFirst = true)]
[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
public partial struct DatabasePrefabsBakingSystem : ISystem
{
    public void OnUpdate(ref SystemState state)
    {
        var store = state.World.GetExistingSystemManaged<BakingSystem>().BlobAssetStore;
        
        var databasePrefab = DB_Enemy_Data._prefab;
        
        for (int i = 0; i < DB_Enemy_Data.CountEntities; i++)
        {
            // GetEntity in BakingSystem here
            Debug.Log($"Prefab {databasePrefab[i]} found!");
        }
    }
}
rose cobalt
#

the answer is no

#

The responsibility for bakers is to convert gameobjects to entities

#

Baking systems just provide post processing on this data when your entities need to reference another entity