首页 > 解决方案 > Unity ECS 不显示自包更新以来创建的实体

问题描述

我尝试生成一些实体并为它们分配网格和材质。不知何故,它们既没有出现在编辑器中,也没有出现在游戏视图中。当我使用“GameObjectToEntity”转换脚本时,实体确实出现了,我尝试了新的 Alpha 版本的编辑器 (2020.1.0a25),但它没有帮助。我也在使用URP。它可能与更新预览包(实体、混合渲染等)有关,因为我在使用旧版本的不同项目中没有问题。不过,我可能在我的代码中遗漏了一些东西。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Rendering;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Collections;

public class EntityCreator : MonoBehaviour
{

    [SerializeField] public Mesh theMesh;
    [SerializeField] public Material theMaterial;

    // Start is called before the first frame update
    void Start()
    {
        EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

        EntityArchetype eArch =  entityManager.CreateArchetype(
                typeof(RenderMesh),
                typeof(Translation),
                typeof(LocalToWorld),
                typeof(MoveSpeedComponent)
            );

        NativeArray<Entity> eArray = new NativeArray<Entity>(10, Allocator.Temp);

        entityManager.CreateEntity(eArch, eArray);

        foreach (Entity ent in eArray)
        {
            entityManager.SetComponentData(ent, new Translation { Value = new Vector3(0f,0f, 0f) });
            entityManager.SetSharedComponentData(ent, new RenderMesh 
            {
                mesh = theMesh, material = theMaterial
            });
        }
        eArray.Dispose();
    }

}

非常感谢您抽出宝贵时间

标签: c#visual-studiounity3drenderingentity-component-system

解决方案


EntityArchetype eArch =  
    entityManager.CreateArchetype(
        typeof(RenderMesh),
        typeof(Translation),
        typeof(LocalToWorld),
        typeof(MoveSpeedComponent),
        typeof(RenderBounds));

推荐阅读