英文:
nullable generics type function in C# Unity
问题
以下是您提供的代码的翻译部分:
我正在为Unity DOTS中的空间分割系统工作。
示例函数如下:
partial struct CharCopyJob : IJobEntity
{
public NativeList<Entity> Entities;
public NativeList<CharacterControllerComponent> Bodies;
public NativeList<LocalTransform> Transforms;
public NativeList<AICharacterStateData> CharacterStates;
public NativeList<AIInteractionSignalData> Signals;
void Execute(Entity entity, in CharacterControllerComponent body, in LocalTransform transform, in AICharacterStateData state, in AIInteractionSignalData signal)
{
Entities.Add(entity);
Bodies.Add(body);
Transforms.Add(transform);
CharacterStates.Add(state);
Signals.Add(signal);
log(Entities.Length);
}
}
但是正如您可以想象的那样,对于每种类型的实体,我都必须重新编写此函数(例如,角色、武器等)。
我正在尝试编写一个更通用的作业,用于此目的:
partial struct CopyJob<A, B, C, D, E, F, G, H, I> : IJobEntity
where A : unmanaged, IComponentData
where B : unmanaged, IComponentData
where C : unmanaged, IComponentData
where D : unmanaged, IComponentData
where E : unmanaged, IComponentData
where F : unmanaged, IComponentData
where G : unmanaged, IComponentData
where H : unmanaged, IComponentData
where I : unmanaged, IComponentData
{
public NativeList<A> al;
public NativeList<B> bl;
public NativeList<C> cl;
public NativeList<D> dl;
public NativeList<E> el;
public NativeList<F> fl;
public NativeList<G> gl;
public NativeList<H> hl;
public NativeList<I> il;
void Execute(Entity entity, A a, B b, C c, D d, E e, F f, G g, H h, I i)
{
if (typeof(A) == typeof(Null))
al.Add(a);
if (typeof(B) == typeof(Null))
bl.Add(b);
if (typeof(C) == typeof(Null))
cl.Add(c);
if (typeof(D) == typeof(Null))
dl.Add(d);
if (typeof(E) == typeof(Null))
el.Add(e);
if (typeof(F) == typeof(Null))
fl.Add(f);
if (typeof(G) == typeof(Null))
gl.Add(g);
if (typeof(H) == typeof(Null))
hl.Add(h);
if (typeof(I) == typeof(Null))
il.Add(i);
}
}
这是我想出来的,但由于NULL不能是unmanaged类型,因此无法正常工作。
如果您有任何建议,将不胜感激。
英文:
I am working on a system for spatial partitioning in unity dots and currently.
the workin example function looks something like this
partial struct CharCopyJob : IJobEntity
{
public NativeList<Entity> Entities;
public NativeList<CharacterControllerComponent> Bodies;
public NativeList<LocalTransform> Transforms;
public NativeList<AICharacterStateData> CharacterStates;
public NativeList<AIInteractionSignalData> Signals;
void Execute(Entity entity, in CharacterControllerComponent body, in LocalTransform transform, in AICharacterStateData state, in AIInteractionSignalData signal)
{
Entities.Add(entity);
Bodies.Add(body);
Transforms.Add(transform);
CharacterStates.Add(state);
Signals.Add(signal);
log(Entities.Length);
}
}
but as you can imagine, for every kind of entity i have to rewrite this function ( i.e. char, weapons etc).
I am trying to write a more generic job for this purpose
partial struct CopyJob<A,B,C,D,E,F,G,H,I> : IJobEntity
where A : unmanaged, IComponentData
where B : unmanaged, IComponentData
where C : unmanaged, IComponentData
where D : unmanaged, IComponentData
where E : unmanaged, IComponentData
where F : unmanaged, IComponentData
where G : unmanaged, IComponentData
where H : unmanaged, IComponentData
where I : unmanaged, IComponentData
{
public NativeList<A> al;
public NativeList<B> bl;
public NativeList<C> cl;
public NativeList<D> dl;
public NativeList<E> el;
public NativeList<F> fl;
public NativeList<G> gl;
public NativeList<H> hl;
public NativeList<I> il;
void Execute(Entity entity, A a, B b, C c, D d, E e, F f, G g, H h, I i)
{
if(typeof(A) == typeof(Null))
al.Add(a);
if(typeof(B) == typeof(Null))
bl.Add(b);
if(typeof(C) == typeof(Null))
cl.Add(c);
if(typeof(D) == typeof(Null))
dl.Add(d);
if(typeof(E) == typeof(Null))
el.Add(e);
if(typeof(F) == typeof(Null))
fl.Add(f);
if(typeof(G) == typeof(Null))
gl.Add(g);
if(typeof(H) == typeof(Null))
hl.Add(h);
if(typeof(I) == typeof(Null))
il.Add(i);
}
}
this is what i came up with but it is not working as NULL can not be unmanaged.
Any suggestions would be appreciated.
答案1
得分: 0
Allocator allocator = Allocator.Temp;
var query = EntityManager.CreateEntityQuery(
ComponentType.ReadOnly<CharacterControllerComponent>()
, ComponentType.ReadOnly<LocalTransform>()
, ComponentType.ReadOnly<AICharacterStateData>()
, ComponentType.ReadOnly<AIInteractionSignalData>()
);
var entities = query.ToEntityListAsync(allocator, out var h0);
var bodies = query.ToComponentDataListAsync<CharacterControllerComponent>(allocator, out var h1);
var transforms = query.ToComponentDataListAsync<LocalTransform>(allocator, out var h2);
var characterStates = query.ToComponentDataListAsync<AICharacterStateData>(allocator, out var h3);
var signals = query.ToComponentDataListAsync<AIInteractionSignalData>(allocator, out var h4);
JobHandle copyJobHandle = JobHandle.CombineDependencies(
new NativeList<JobHandle>(Allocator.Temp) { h0, h1, h2, h3, h4 }
);
英文:
You do not need to implement what is there already.
Allocator allocator = Allocator.Temp;
var query = EntityManager.CreateEntityQuery(
ComponentType.ReadOnly<CharacterControllerComponent>()
, ComponentType.ReadOnly<LocalTransform>()
, ComponentType.ReadOnly<AICharacterStateData>()
, ComponentType.ReadOnly<AIInteractionSignalData>()
);
var entities = query.ToEntityListAsync( allocator , out var h0 );
var bodies = query.ToComponentDataListAsync<CharacterControllerComponent>( allocator , out var h1 );
var transforms = query.ToComponentDataListAsync<LocalTransform>( allocator , out var h2 );
var characterStates = query.ToComponentDataListAsync<AICharacterStateData>( allocator , out var h3 );
var signals = query.ToComponentDataListAsync<AIInteractionSignalData>( allocator , out var h4 );
JobHandle copyJobHandle = JobHandle.CombineDependencies(
new NativeList<JobHandle>(Allocator.Temp){ h0 , h1 , h2 , h3 , h4 }
);
答案2
得分: 0
我最终创建了多个系统以满足各种用途。
它还有助于简化不同的系统。
英文:
I ended up creating multiple systems for every use.
It also helps in simplifying different systems.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论