Unity ECS | Entities don't render/show up

1.6k Views Asked by At

I'm trying to create a new game with Unity ECS following these tutorials: https://www.youtube.com/watch?v=H7zAORa3Ux0 https://www.youtube.com/watch?v=IO6_6Y_YUdE

However I'm seem to be stuck already at the very basics. I have a HexGrid object inside a Subscene with an mono HexGridAuthoringComponent. This Components holds a reference to a prefab. I bake the prefab reference with a Baker<> to an Entity. I try to spawn the Entity with a System.

That works. The Entity shows up in the Hierachy and seems to have the proper components in the inspector. However I can not see any of the Entities in the game or scene view.

Code:

HexGridProperties (The Data component for the HexGrid Entity)

using Unity.Entities;
using UnityEngine;

namespace HexGrid
{
    public struct HexGridProperties : IComponentData
    {
        public Entity HexCellPrefab;
    }
}

HexGridAuthoring (The MonoBehaviour on the HexGrid object + The Baker)

using Unity.Entities;
using UnityEngine;

namespace HexGrid
{
    public class HexGridAuthoring : MonoBehaviour
    {
        public GameObject HexGrid;
    }

    public class HexGridBaker : Baker<HexGridAuthoring>
    {
        public override void Bake(HexGridAuthoring authoring)
        {
            AddComponent(new HexGridProperties
            {
                HexCellPrefab = GetEntity(authoring.HexGrid)
            });
        }
    }
}

HexGridAspect;

using Unity.Entities;

namespace HexGrid
{
    public readonly partial struct HexGridAspect : IAspect
    {
        public readonly Entity Entity;

        private readonly RefRO<HexGridProperties> _hexGridProperties;

        public Entity HexCellPrefab => _hexGridProperties.ValueRO.HexCellPrefab;
    }
}

HexCellSpawnSystem (The System responsible for spawning the cells of the grid):

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;

namespace HexGrid
{
    [BurstCompile]
    [UpdateInGroup(typeof(InitializationSystemGroup))]
    public partial struct HexCellSpawnSystem : ISystem
    {
        [BurstCompile]
        public void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<HexGridProperties>();
        }

        [BurstCompile]
        public void OnDestroy(ref SystemState state)
        { 
        }

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            state.Enabled = false;
            var hexGridEntity = SystemAPI.GetSingletonEntity<HexGridProperties>();
            var hexGrid = SystemAPI.GetAspectRO<HexGridAspect>(hexGridEntity);

            var ecb = new EntityCommandBuffer(Allocator.Temp);

            for (int i = 0; i < 100; i++)
            {
                ecb.Instantiate(hexGrid.HexCellPrefab);
            }
            
            ecb.Playback(state.EntityManager);
        }
    }
}

Full Project: https://github.com/PMSSMP/HexWorld

Editor: 2022.2.3f1 Entities: 1.0.0-pre.15 Entities-Graphics: 1.0.0-pre.15

2

There are 2 best solutions below

0
On

try reimporting the graphics package, then restart the project and visual studio. I faced the same problem and this fixed mine. Further try recreating the subscene and gameobject as well.

0
On

When baking, if you want the entity to have transform you should explicitly tell it by executing

Entity entity = GetEntity(TransformUsageFlags.Dynamic);

If you do TransformUsageFlags.None you will see it in the hierarchy but it won't be visible on the scene because it has no transform component.

public class Baker : Baker<HexGridAuthoring>
{
    public override void Bake(HexGridAuthoring authoring)
    {
        Entity entity = GetEntity(TransformUsageFlags.None);
        AddComponent(entity, new HexGridProperties
        {
            HexCellPrefab = GetEntity(authoring.HexGrid, TransformUsageFlags.Dynamic)
        });
    }
}

If neither of these working for you make sure you are using either URP or HDRP because entities won't render on built-in render pipeline