unity3d 如何从原型创建可渲染的实体

bd1hkmkf  于 2023-05-01  发布在  其他
关注(0)|答案(3)|浏览(150)

我在玩新的DOTS软件包。
有一件事我还没有设法做的是使用一个原型来创建一个实体,是可见的场景。这是我目前所看到的,我可以看到实体调试器中正在创建的实体,但它们没有呈现。任何帮助都很感激。我想我在原型上漏掉了一些东西。

using Unity.Entities;
    using Unity.Mathematics;
    using Unity.Transforms;
    using UnityEngine;

    public class CreateStuff : MonoBehaviour
    {
        public int CountX = 100;
        public int CountY = 100;

        void Start()
        { 
            var entityManager = World.Active.EntityManager;
            var archetype = entityManager.CreateArchetype(typeof(Translation), typeof(Scale), typeof(Rotation), typeof(MeshRenderer), typeof(MeshFilter));
            for (int x = 0; x < CountX; x++)
            {
                for (int y = 0; y < CountY; y++)
                {
                    var instance = entityManager.CreateEntity(archetype);
                    var position = transform.TransformPoint(new float3(x - CountX / 2, noise.cnoise(new float2(x, y) * 0.21F) * 10, y - CountY / 2));
                    entityManager.SetComponentData(instance, new Translation() { Value = position });
                    entityManager.SetComponentData(instance, new Scale() { Value = 1 });
                }
            }
        }
    }
pgx2nnw8

pgx2nnw81#

我不知道你是否还在寻找答案。这就是我如何设法让它工作。

[SerializeField] private Mesh _mesh;
[SerializeField] private Material _mat;
private void Start()
{
      EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

      EntityArchetype entityArchetype = entityManager.CreateArchetype(
                  typeof(RenderMesh),
                  typeof(LocalToWorld),
                  typeof(RenderBounds),
    );

    // create 100 entities
    NativeArray<Entity> entityArray = new NativeArray<Entity>(100, Allocator.Temp);
    entityManager.CreateEntity(entityArchetype, entityArray);

    for (int i = 0; i < entityArray.Length; i++)
    {
        Entity entity = entityArray[i];

        entityManager.SetSharedComponentData(entity, new RenderMesh
        {
            mesh = _mesh,
            material = _mat,
        }); 
    }
    entityArray.Dispose();
}

我是通过youtube上的this教程做到这一点的。

kb5ga3dv

kb5ga3dv2#

您可以使用GameObjectConversionUtility

public GameObject Prefab;

    var prefab = Unity.Entities.GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab, World.Active);
    var entityManager = World.Active.EntityManager;
    var instance = entityManager.Instantiate(prefab);

如果你的预置有一个渲染器,当它是一个实体时,它会自动有一个
如果你还没有,请查看ECS示例:https://github.com/Unity-Technologies/EntityComponentSystemSamples

3pmvbmvn

3pmvbmvn3#

代码本身没有问题。我有同样的问题,首先你必须安装“混合渲染器”(Windeow=〉包管理器=〉高级=〉显示预览pacages =〉混合渲染器)
我的代码运行良好。可以从实体和预制件示例化对象

public class ECSObjectSpawner : MonoBehaviour
{
    public static ECSObjectSpawner instance;
    private World _defaultWorld;
    private EntityManager _entityManager;

    public List<FromGameObject> fromGameObjects;
    private Dictionary<string, Entity> _convertedFromGo;

    public List<FromEntity> fromEntities;
    private Dictionary<string, FromEntity> _generatedFromEntity;

    private void Awake()
    {
        instance = instance ?? this;
        _convertedFromGo = new Dictionary<string, Entity>();
        _generatedFromEntity = fromEntities.ToDictionary(x => x.prefabName, x=>x);
    }
    void Start()
    {
        // setup references to World and EntityManager
        _defaultWorld = World.DefaultGameObjectInjectionWorld;
        _entityManager = _defaultWorld.EntityManager;
        GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(_defaultWorld, null);
        _convertedFromGo = fromGameObjects.ToDictionary(x => x.prefabName, x=> GameObjectConversionUtility.ConvertGameObjectHierarchy(x.gameObjectPrefab, settings));
    }

    public void Instantiate(string key, float3 position, quaternion rotation)
    {
        if (_generatedFromEntity.TryGetValue(key, out FromEntity fromEntity))
        {
            InstantiateFromEnity(position, rotation, fromEntity);
            return;
        }
        if (_convertedFromGo.TryGetValue(key, out Entity entity))
        {
            InstantiateFromGameObject(position, rotation, entity);
            return;
        }
    }

    private void InstantiateFromEnity(float3 position, quaternion rotation, FromEntity fromEntity)
    {
        EntityArchetype archetype = _entityManager.CreateArchetype(
            typeof(Translation),
             typeof(Rotation),
             typeof(RenderMesh),
             typeof(RenderBounds),
             typeof(LocalToWorld)
         );

        Entity myEntity = _entityManager.CreateEntity(archetype);

        _entityManager.AddComponentData(myEntity, new Translation
        {
            Value = position
        });
        _entityManager.AddComponentData(myEntity, new Rotation
        {
            Value = rotation
        });

        _entityManager.AddSharedComponentData(myEntity, new RenderMesh
        {
            mesh = fromEntity.unitMesh,
            material = fromEntity.unitMaterial
        });
    }

    private void InstantiateFromGameObject(float3 position, quaternion rotation, Entity entity)
    {
        Entity myEntity = _entityManager.Instantiate(entity);
        _entityManager.SetComponentData(myEntity, new Translation
        {
            Value = position
        });
        _entityManager.SetComponentData(myEntity, new Rotation
        {
            Value = rotation
        });
    }

}
[System.Serializable]
public sealed class FromGameObject
{
    public string prefabName;
    public GameObject gameObjectPrefab;
}
[System.Serializable]
public sealed class FromEntity
{
    public string prefabName;
    public Mesh unitMesh;
    public Material unitMaterial;
}

相关问题