【问题标题】:How could i get struct as generic from type?我怎样才能从类型中获得通用的结构?
【发布时间】:2019-12-12 13:37:39
【问题描述】:

您好,我为对象中的搜索类型创建了类型数组,如果对象具有该结构(或组件),则从对象中删除结构(或组件)。

这是我的代码的概念,但我得到错误“'结构'是一个变量但是 从行中像类型一样使用

if (entityManager.HasComponent<structure>(entity))

下面是我的脚本。

public struct ECS : IComponentData{}
...


static Type[] componentArray = new Type[] { typeof(ECS), typeof(JECS), typeof(JECS2), typeof(JECS3), typeof(JECS4) };

static void ResetComponent(Entity entity, EntityManager entityManager) {

    foreach (Type structure in componentArray)
    {
        if (entityManager.HasComponent<structure>(entity))
        {
            entityManager.RemoveComponent(entity, structure);
        }
    }

}

在我看来,我需要将类型转换为泛型,但我不知道如何。

请找出我的问题。

【问题讨论】:

  • 你不能像这样使用泛型,它们是预编译的。什么是entityManager,什么是HasComponent
  • entityManger 是类,hascomponent 是检查实体(对象)是否具有结构组件并返回 bool 值的方法。所有这些都来自 Unity DOTS。

标签: c# arrays unity3d struct


【解决方案1】:

(假设EntityManagerUnity.Entities.EntityManager

在这种情况下,您不应该调用HasComponent&lt;T&gt;(Entity) 方法,因为您将组件类型作为Type 对象,而不是在编译时知道类型。

虽然有一个second overloadHasComponent,您可以调用它。

 if (entityManager.HasComponent(entity, new ComponentType(structure)))

【讨论】:

  • 我明白为什么不能使用。感谢您向我解释并找出我的问题,它对我有用。 :)
猜你喜欢
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 2011-06-04
  • 2017-05-25
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多