#Baking systems not acting on prefabs loaded via SceneSystem.LoadPrefabAsync

1 messages · Page 1 of 1 (latest)

tropic stream
#

Hello!
I'm struggling with loading entity prefabs at runtime with SceneSystem.LoadPrefabAsync. They get loaded correctly and all component added by Bakers are fine, but I noticed that Baking Systems don't run on those entities. If the same entity is manually added to a subscene the Baking Systems work correctly, the issue it's just when the entity is loaded with SceneSystem.LoadPrefabAsync.

So, is there some configuration in the editor or some steps I'm missing with this approach, or this is not expected behavior?

Unity 2022.3.0
Entities 1.0.10

iron agate
#

everything gets baked beforehand

#

and if you want to debug baking you need to open subscene (sadly that's the only way) and run playmode

#

as it will do live conversion, which does run in runtime (but only in editor)

tropic stream
#

The approach I'm following is loading prefabs individually at runtime. I understand that they are supposed to be already baked at the moment of loading, but they are not, they are baked in play mode as I load them. I'm assuming that's not actually runtime but some "editor runtime" that will not occur in a build and will only occur in the editor if a prefab has not been baked and cached yet.

But the thing is, bakers do run, the entity prefab is loaded correctly, it's just the baking systems that don't receive the loaded entities to process.

#

The baking systems actually run, they just don't detect any of the loaded entities.

iron agate
#

of course they don't detect

#

every baking is done in isolation to subscene

#

when you bake prefab

#

it will get it's own subscene

#

also worth mentioning that editor baking runs on separate process

#

(that's why you can't debug it)

tropic stream
#

But even when editing a prefab and the baking process triggers, baking systems still don't detect the entity.

tropic stream
iron agate
#

what entity it doesn't detect?

tropic stream
#

The one generated by baking the prefab

iron agate
#

can't say much without code then

tropic stream
#

Gimme a sec

#
  public partial class TestBakingSystem : SystemBase
  {
   
      protected override void OnUpdate()
      {
          var query1 = new EntityQueryBuilder(Allocator.Temp).
           WithAll<TempMultirenderElement>().
           Build(this);

          UnityEngine.Debug.Log($"TestBakingSystem About to run! \tRends:              {query1.CalculateEntityCount()} ");
      }
    }```
#

In the case of this system. If I modify the prefab directly, the TestBakingSystem runs, but the entity count of that query is 0.

But If that same prefab gets added to a subscene, it is not 0.

iron agate
#

you mean you modify it in live baking?

#

if so, live baking is incremental

#

no idea how it works precisely

#

and it freqeuently breaks

tropic stream
#

I don't thing modifying the asset triggers Live baking, but it could be. It only triggers the bake process when saving the changes.

iron agate
#

when it comes to prefabs - it does

#

it should work fine when you load it just normally intially though

#

does it?

tropic stream
#

No, none of the loaded prefabs work with SceneSystem.LoadPrefabAsync. Or do you mean adding them manually to a subscene?

iron agate
#

(EntityPrefabReference)

#

to be precise

#

how does Baker look like then?

tropic stream
#

It just adds the TempMultirenderElement buffer, so it can be detected and processed by the baking system.

But the weird thing is the different behaviors between using a subscene and loading the entity via LoadPrefabAsync.

#

Is there a way to force the baking process on a prefab asset?

iron agate
#

wait

#

add to query

#

WithOptions

#

include prefabs

#

I always forget about that one lol

tropic stream
#

Oh haha, I feel so dumb

#

Let me try

#

No, It still does not work. I was hoping that was the issue :/

iron agate
#

you sure that buffer is on Entity?

tropic stream
#

Yes, If I add the entity to a subscene in the current scene, it works fine

iron agate
#

just in case: try to use EntityManager.GetAllEntities()

#

and see if it's there

tropic stream
#

Ok

iron agate
#

you can do foreach on them and log SystemAPI.HasComponent

tropic stream
#

None of them have the component. So maybe bakers are not running at the moment of modifying the prefab, but when they are loaded

#

I'm adding debug.log to baker

iron agate
#

once again - it's incremental, so if none authoring data has changed

#

baker shouldn't run

#

I have no idea how it interacts with systems though

#

but to properly test

#

you need to make some modification to authoring component

#

try adding field to it

tropic stream
#

I'm modifying the authoring component, so it should runs the baker, right?

But it's not running

iron agate
#

when you change it it'll trigger rebaking

tropic stream
#

Yeah, it triggers the rebaking, and the baking system runs, but bakers don't so system doesn't do anything

iron agate
#

if your baker doesn't run - that's the explanation 🤷‍♂️

tropic stream
#

But it seems it's not just my bakers that doesn't work, also the unity's ones. The Companion Objects dont work. The Linked Entity Groups are also wrong

iron agate
#

add a field to your authoring

#

and modify it

#

this will trigger baking on baker that relies on it

tropic stream
#

That's what I'm doing haha, this is the actual authoring component. Baking gets triggered correctly, but the baker is not doing its job. But as I mentioned, I noticed at least to of the unity's bakers are not working either

#

Is there a way to check how many and which components an entity has?

#

From EntityManager?

iron agate
#

live baking is not something easily debugged anyway

tropic stream
#

Yeah, I'm getting real frustrated haha

iron agate
#

why do you even want that

#

just do what you want in baking

#

and only debug if something is wrong

tropic stream
#

I mean, when I modify the asset, baking systems run, but bakers don't

#

And that's the issue as you mentioned. With no bakers, systems won't do anything.

iron agate
#

don't rely on live baking

#

just stop play mode

#

modify prefab

#

and start again

#

live baking only works fine when you modify pure data

#

which also doesn't change during runtime

tropic stream
#

I'm not in play mode, I'm working directly with the asset in the project window

iron agate
#

then it shouldn't bake at all

#

until you call SceneSystem.LoadPrefabAsync

tropic stream
#

Right, but at that moment also doesnt work

iron agate
#

what doesn't work?

tropic stream
#

My baking system. The initial issue I had is that when loading an entity prefab, my Baking system was not doing anything.

iron agate
#

if you mean that testing system which does nothing but log - then write a normal one that actually does what you want

#

and see if this one would work

tropic stream
#

No, i dont mean that. That was just to show you, the actual system uses that buffer to do things, but neither the test one or the actual one detect the entity with the buffer component, they just run.

iron agate
#

🤔

tropic stream
#

But the thing is, why they work on an entity baked on a subcene but not with LoadPrefabAsync

iron agate
#

just did a quick repro - all works fine

#
using Unity.Entities;
using Unity.Entities.Serialization;
using Unity.Scenes;
using UnityEngine;

public class TestAuthoring : MonoBehaviour
{
    public GameObject prefab;

    public class TestAuthoringBaker : Baker<TestAuthoring>
    {
        public override void Bake(TestAuthoring authoring)
        {
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            AddComponent(entity, new TestComponentData { prefab = new(authoring.prefab) });
        }
    }
}

public struct TestComponentData : IComponentData
{
    public EntityPrefabReference prefab;
}


public partial struct TestInstantiateSystem : ISystem
{
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<TestComponentData>();
    }

    public void OnUpdate(ref SystemState state)
    {
        var prefab = SystemAPI.GetSingleton<TestComponentData>().prefab;
        SceneSystem.LoadPrefabAsync(state.WorldUnmanaged, prefab);
        state.Enabled = false;
    }
}
#
using Unity.Entities;
using UnityEngine;

public class TestPrefabAuthoring : MonoBehaviour
{
    public class TestPrefabAuthoringBaker : Baker<TestPrefabAuthoring>
    {
        public override void Bake(TestPrefabAuthoring authoring)
        {
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            AddComponent(entity, new TestPrefabComponentData());
        }
    }
}

public struct TestPrefabComponentData : IComponentData { }

[WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
public partial struct TestBakingSystem : ISystem
{
    public void OnUpdate(ref SystemState state)
    {
        foreach (var test in SystemAPI.Query<TestPrefabComponentData>()
                     .WithOptions(EntityQueryOptions.IncludePrefab))
        {
            Debug.Log("kekw");
        }
    }
}
#

with only object in subscene

#

worth mentioning also: there is only one kekw

#

afterwards it's not rebaked when I run play mode

#

if I trigger recompile though - it will

tropic stream
#

I just did my same test on a clean project, and it works fine too.

#

What could my project be doing differently. Same editor and entities and rest of the packages

iron agate
#

to be certain

#

trigger a recompile

#

before you do your baking

#

changes to assembly must be made in assembly which contains that buffer/component

tropic stream
#

Nope, still not working. Well, at least I know my code is not wrong. But I still don't know how to fix it. I saw an error about corrupted EntitiesClient or something like that, I couldn't read it completely because I had already clicked the playmode button. Maybe it's related