You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity ECS实例化实体无法在场景/游戏视图显示问题求助

Unity ECS 六边形网格实体生成后无法显示问题

我正在跟随Unity ECS教程开发新游戏,在基础步骤遇到问题:Subscene内的HexGrid对象挂载了HexGridAuthoring组件,关联预制体后通过Baker<>将预制体引用烘焙为Entity,再用System生成实体。实体在层级面板可见且组件正常,但无法在场景或游戏视图中显示。

编辑器版本:2022.2.3f1,Entities版本:1.0.0-pre.15,Entities-Graphics版本:1.0.0-pre.15


相关代码

HexGridProperties(HexGrid实体的数据组件)

using Unity.Entities;
using UnityEngine;

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

HexGridAuthoring(HexGrid对象的MonoBehaviour及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(负责生成网格单元格的System)

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);
        }
    }
}

问题排查与解决方法

1. 预制体未正确转换为可渲染ECS实体

  • 给HexCell预制体添加ConvertToEntity组件,转换模式设为Convert and Destroy(Subscene内推荐此模式)。
  • 打开Entity Debugger,检查生成的实体是否包含RenderMesh和LocalToWorld组件,这是ECS实体可渲染的核心条件。

2. 实体位置重叠或超出视口

当前代码所有实例化的实体都在原点(0,0,0),重叠后无法看到。修改生成逻辑,为每个实体设置不同位置:

for (int i = 0; i < 100; i++)
{
    var newEntity = ecb.Instantiate(hexGrid.HexCellPrefab);
    // 按六边形网格规则计算位置,或简单偏移
    ecb.SetComponent(newEntity, new LocalTransform
    {
        Position = new float3(i * 2f, 0, 0),
        Rotation = quaternion.identity,
        Scale = 1f
    });
}

注意:ECS中用LocalTransform组件控制实体位置,旧版的Translation/Rotation/Scale组件已被替代。

3. Subscene加载与烘焙时机问题

  • 确认Subscene设置为Always Loaded或已通过代码正确加载。
  • 在Entity Debugger中找到HexGrid实体,检查HexGridProperties组件内的HexCellPrefab引用是否有效。

4. Entities-Graphics全局配置缺失

  • 通过菜单GameObject > Entities > Rendering Settings创建全局渲染配置实体,确保ECS渲染系统正常工作。
  • 检查HexCell预制体的Layer,确认该Layer在渲染配置的可见Layer列表中。

内容的提问来源于stack exchange,提问作者Pascal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 16:20:22