您好,我为对象中的搜索类型创建了类型数组,如果对象具有该struct(或组件),则从对象中删除struct(或组件)。
这是我的代码的概念,但是我得到了一个错误,即“'结构'是一个变量,但是 像类型一样使用”
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);
}
}
}
我认为我需要将类型转换为泛型,但我不知道如何。
请找出我的问题。
答案 0 :(得分:1)
(假设EntityManager
是Unity.Entities.EntityManager
)
在这种情况下,您不应调用HasComponent<T>(Entity)
方法,因为您将组件类型作为Type
对象,而不是在编译时就知道类型。
尽管有HasComponent
中的second overload,您可以 呼叫。
if (entityManager.HasComponent(entity, new ComponentType(structure)))