How do I get a reference to the prefab that I am baking?

Whenever I instantiate a prefab that has a component with an Entity field set by GetEntity, the entity field is set to the entity that was instantiated and not the original prefab that it was instantiated from.

One thing to note is that the reference will be to a prefab if the GameObject field in the monobehaviour is set to reference a separate GameObject that will also be baked.

I can only imagine that I am missing something, I couldn’t find anything looking though the baker methods.

An example:

public class TestAuthoring : MonoBehaviour {
	public GameObject referenceToThisPrefab;
}

public struct SelfPrefabReference : IComponentData {
	public Entity prefab;
}

class TestAuthoringBaker : Baker<TestAuthoring> {
    public override void Bake(TestAuthoring authoring) {
		var e = GetEntity(TransformUsageFlags.Dynamic);
		AddComponent(e, new SelfPrefabReference {
				//Upon instantiation all of these become a reference to the instantiated entity
				//prefab = e,
				//prefab = GetEntity(TransformUsageFlags.Dynamic),
				//prefab = GetEntity(authoring.referenceToThisPrefab, TransformUsageFlags.Dynamic),
		});
    }
}

Could be an XY Problem. What do you need this for?

Edit: I did briefly look into a solution but could not find one either, thats why I want to know why you need this.

Interesting. What we do is we have a PrefabCenter that stores all converted prefabs that are mapped with an id. What the components would then have is the prefab ID, not the reference to the converted prefab. We always request the PrefabCenter for the prefab entity.

Just prototyping a component that causes something to replicate itself upon collision. In my case a prefab reference is needed because there are some components that remove themselves after instantiation.

This is probably the answer and something that would have to be built regardless of this specific issue. If within a few days nobody appears with a more direct solution, I’ll go ahead and mark this one as the solution.

For self-replication, the trick is to give the prefab entity a broken reference to itself at runtime. That is, in a runtime system, you should specifically add a component to all the prefab entities that require a self-reference to be preserved after instantiating. It has to be in a runtime system. It cannot be in a baking system or subscene load system.

This component should contain the raw Index and Version values of the entity, but not an Entity struct. This will disable remapping, keeping the original prefab entity Index and Value after instantiation. If you need NetCode or world migration support, you can then copy the Index and Value directly into a proper Entity struct on some other IComponentData, which you then use from then on (and you should probably remove the raw values component for good measure).

1 Like

Just prototyping a component that causes something to replicate itself upon collision.

By coincidence I am working on something similar and I think I will also use a “prefab database” as suggested by @davenirline.

Another option I’m considering is to just spawn a blank entity and then have a series of systems copy over relevant information from the original to the clone. Maybe as part of a system group or something, not sure yet.