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

Unity Dots实体渲染异常:层级面板可见但场景与游戏窗口不可见

实体在Entities面板可见但Scene/Game窗口不显示的解决方法

核心问题分析

你的代码创建的实体无法渲染,主要有以下几个原因:

  • 缺少RenderBounds组件:ECS渲染系统需要该组件确定实体的渲染范围,没有则不会触发渲染。
  • 未设置LocalTransform的有效位置:大量实体默认堆在原点,可能被相机视角遗漏或互相遮挡。
  • 冗余添加LocalToWorld组件:该组件会由LocalTransformSystem自动生成,手动添加可能导致组件冲突。

修改后的代码实现

private void Start()
{
    EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    // 调整实体原型:添加RenderBounds,移除LocalToWorld
    EntityArchetype entityArchetype = entityManager.CreateArchetype(
        typeof(HealtComponent),
        typeof(LocalTransform), 
        typeof(RenderMesh),
        typeof(RenderBounds)
        );
    NativeArray<Entity> entityArray = new NativeArray<Entity>(200000, Allocator.TempJob);
    entityManager.CreateEntity(entityArchetype, entityArray);

    // 从目标Mesh获取默认渲染边界
    Bounds meshBounds = _Mesh.bounds;

    for (int i = 0; i < entityArray.Length; i++)
    {
        Entity entity = entityArray[i];
        // 设置自定义组件数据
        entityManager.SetComponentData(entity, new HealtComponent { MaxHealth = 3, CurrentHealth = 3 });
        // 设置渲染网格与材质
        entityManager.SetSharedComponentManaged(entity, new RenderMesh
        {
            material = _Material,
            mesh = _Mesh
        });
        // 绑定渲染边界
        entityManager.SetComponentData(entity, new RenderBounds { Value = meshBounds });
        // 为每个实体分配随机位置,避免堆叠在原点
        float3 randomPos = new float3(
            UnityEngine.Random.Range(-100f, 100f),
            UnityEngine.Random.Range(-100f, 100f),
            UnityEngine.Random.Range(-100f, 100f)
        );
        entityManager.SetComponentData(entity, LocalTransform.FromPosition(randomPos));
    }
    
    entityArray.Dispose();
}

额外注意事项

  • 确认已安装Hybrid Renderer包:ECS渲染依赖该包,可在Package Manager中搜索安装。
  • 检查Inspector赋值:确保_Material和_Mesh变量已正确绑定资源,避免为空。
  • 调整相机视角:如果实体集中在特定区域,移动相机确保该区域在视野范围内。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 05:35:15