How do I get EntityGuid during Baking?

I’m trying to get the EntityGuid for the internal element of the baked authoring, so that inside the system that creates entity instances from baked prefabs, I can add a specific IComponentData to certain components.

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
using Random = Unity.Mathematics.Random;

[BurstCompile]
public partial struct CubeRotationSystem : ISystem
{
	[BurstCompile]
	public void OnCreate(ref SystemState state)
	{
		state.RequireForUpdate<CapsuleAndCubeReference>();

		// Создаем запрос один раз в OnCreate
		query = state.GetEntityQuery(ComponentType.ReadOnly<Child>(), ComponentType.ReadOnly<Marked>());

		delta = 0;

		rnd = new Random { state = state.GlobalSystemVersion };
		rnd.InitState();
	}

	private Random rnd;
	private float delta;
	private EntityQuery query; // Храним запрос как поле системы

	public struct Marked : IComponentData { }

	[BurstCompile]
	public void OnUpdate(ref SystemState state)
	{
		delta += SystemAPI.Time.DeltaTime;
		if (delta < 0.3) return;
		delta = 0;

		var data = SystemAPI.GetSingleton<CapsuleAndCubeReference>();

		var ecb = new EntityCommandBuffer(Allocator.Temp);

		var root = ecb.Instantiate(data.EntityRoot);
		ecb.SetComponent(root, LocalTransform.FromPosition(new float3(rnd.NextFloat(-10, 10), 0, rnd.NextFloat(-10, 10))));
		ecb.AddComponent(root, new Marked { });

		ecb.Playback(state.EntityManager);
		ecb.Dispose();

		// Используем заранее созданный запрос
		var entities = query.ToEntityArray(Allocator.Temp);
		foreach (var entity in entities)
		{
			state.EntityManager.RemoveComponent<Marked>(entity);
			string msg = "";
			var children = state.EntityManager.GetBuffer<Child>(entity);
			foreach (var child in children)
			{
				Entity childEntity = child.Value;

				var guid = state.EntityManager.GetComponentData<EntityGuid>(childEntity);

				msg += $"\t{childEntity} Guid:{guid.OriginatingId}\n";
			}

			Debug.Log($"{entity}:\n{msg}");
		}
		entities.Dispose(); // Освобождаем память от временного массива
	}
}

This Guid is unique as long as the object inside the prefab (capsule or cube) is inside the authoring prefab. But I don’t understand how to associate the EntityGuid with a specific element inside the prefab, despite the fact that these Guids don’t change (well, im can’t write these Guid-values as a constant)

using UnityEngine;
using Unity.Entities;

public struct ComponentCube : IComponentData { }
public struct ComponentCapsule : IComponentData
{
	public float CurrentFrame;
}
public struct InstanceTime : IComponentData
{
	public float lifeTime;
}

public struct CapsuleAndCubeReference : IComponentData
{
	public Entity EntityRoot;

	public Entity EntityCube;
	public EntityGuid EntityGuidCube;

	public Entity EntityCapsule;
	public EntityGuid EntityGuidCapsule;
}

public class PrefabRotationCubeAuthoring : MonoBehaviour
{
	[Tooltip("Префаб поворотного куба")]
	public GameObject PrefabRotationCube;
}

public class PrefabSimpleDetailedBaker : Baker<PrefabRotationCubeAuthoring>
{
	public override void Bake(PrefabRotationCubeAuthoring authoring)
	{
		var rootEntity = GetEntity(TransformUsageFlags.Dynamic);

		

		if (authoring.PrefabRotationCube != null)
		{
			var cubeTransform = authoring.PrefabRotationCube.transform.Find("Cube");
			var capsuleTransform = authoring.PrefabRotationCube.transform.Find("Capsule");

			if (cubeTransform == null || capsuleTransform == null)
			{
				Debug.LogError("Не удалось найти один из объектов 'Cube' или 'Capsule' в префабе.");
				return;
			}
			AddComponent(rootEntity, new CapsuleAndCubeReference
			{
				EntityRoot = GetEntity(authoring.PrefabRotationCube, TransformUsageFlags.Dynamic),

				EntityCube = GetEntity(cubeTransform.gameObject, TransformUsageFlags.Dynamic),

				EntityCapsule = GetEntity(capsuleTransform.gameObject, TransformUsageFlags.Dynamic),
			});
		}
		else
		{
			Debug.LogError("Префаб не задан для PrefabRotationCubeAuthoring.");
		}
	}
}
1 Like