在C# Unity中的可空泛型类型函数

huangapple go评论102阅读模式
英文:

nullable generics type function in C# Unity

问题

以下是您提供的代码的翻译部分:

  1. 我正在为Unity DOTS中的空间分割系统工作
  2. 示例函数如下:
  3. partial struct CharCopyJob : IJobEntity
  4. {
  5. public NativeList<Entity> Entities;
  6. public NativeList<CharacterControllerComponent> Bodies;
  7. public NativeList<LocalTransform> Transforms;
  8. public NativeList<AICharacterStateData> CharacterStates;
  9. public NativeList<AIInteractionSignalData> Signals;
  10. void Execute(Entity entity, in CharacterControllerComponent body, in LocalTransform transform, in AICharacterStateData state, in AIInteractionSignalData signal)
  11. {
  12. Entities.Add(entity);
  13. Bodies.Add(body);
  14. Transforms.Add(transform);
  15. CharacterStates.Add(state);
  16. Signals.Add(signal);
  17. log(Entities.Length);
  18. }
  19. }

但是正如您可以想象的那样,对于每种类型的实体,我都必须重新编写此函数(例如,角色、武器等)。

我正在尝试编写一个更通用的作业,用于此目的:

  1. partial struct CopyJob<A, B, C, D, E, F, G, H, I> : IJobEntity
  2. where A : unmanaged, IComponentData
  3. where B : unmanaged, IComponentData
  4. where C : unmanaged, IComponentData
  5. where D : unmanaged, IComponentData
  6. where E : unmanaged, IComponentData
  7. where F : unmanaged, IComponentData
  8. where G : unmanaged, IComponentData
  9. where H : unmanaged, IComponentData
  10. where I : unmanaged, IComponentData
  11. {
  12. public NativeList<A> al;
  13. public NativeList<B> bl;
  14. public NativeList<C> cl;
  15. public NativeList<D> dl;
  16. public NativeList<E> el;
  17. public NativeList<F> fl;
  18. public NativeList<G> gl;
  19. public NativeList<H> hl;
  20. public NativeList<I> il;
  21. void Execute(Entity entity, A a, B b, C c, D d, E e, F f, G g, H h, I i)
  22. {
  23. if (typeof(A) == typeof(Null))
  24. al.Add(a);
  25. if (typeof(B) == typeof(Null))
  26. bl.Add(b);
  27. if (typeof(C) == typeof(Null))
  28. cl.Add(c);
  29. if (typeof(D) == typeof(Null))
  30. dl.Add(d);
  31. if (typeof(E) == typeof(Null))
  32. el.Add(e);
  33. if (typeof(F) == typeof(Null))
  34. fl.Add(f);
  35. if (typeof(G) == typeof(Null))
  36. gl.Add(g);
  37. if (typeof(H) == typeof(Null))
  38. hl.Add(h);
  39. if (typeof(I) == typeof(Null))
  40. il.Add(i);
  41. }
  42. }

这是我想出来的,但由于NULL不能是unmanaged类型,因此无法正常工作。

如果您有任何建议,将不胜感激。

英文:

I am working on a system for spatial partitioning in unity dots and currently.

the workin example function looks something like this

  1. partial struct CharCopyJob : IJobEntity
  2. {
  3. public NativeList&lt;Entity&gt; Entities;
  4. public NativeList&lt;CharacterControllerComponent&gt; Bodies;
  5. public NativeList&lt;LocalTransform&gt; Transforms;
  6. public NativeList&lt;AICharacterStateData&gt; CharacterStates;
  7. public NativeList&lt;AIInteractionSignalData&gt; Signals;
  8. void Execute(Entity entity, in CharacterControllerComponent body, in LocalTransform transform, in AICharacterStateData state, in AIInteractionSignalData signal)
  9. {
  10. Entities.Add(entity);
  11. Bodies.Add(body);
  12. Transforms.Add(transform);
  13. CharacterStates.Add(state);
  14. Signals.Add(signal);
  15. log(Entities.Length);
  16. }
  17. }

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

  1. partial struct CopyJob&lt;A,B,C,D,E,F,G,H,I&gt; : IJobEntity
  2. where A : unmanaged, IComponentData
  3. where B : unmanaged, IComponentData
  4. where C : unmanaged, IComponentData
  5. where D : unmanaged, IComponentData
  6. where E : unmanaged, IComponentData
  7. where F : unmanaged, IComponentData
  8. where G : unmanaged, IComponentData
  9. where H : unmanaged, IComponentData
  10. where I : unmanaged, IComponentData
  11. {
  12. public NativeList&lt;A&gt; al;
  13. public NativeList&lt;B&gt; bl;
  14. public NativeList&lt;C&gt; cl;
  15. public NativeList&lt;D&gt; dl;
  16. public NativeList&lt;E&gt; el;
  17. public NativeList&lt;F&gt; fl;
  18. public NativeList&lt;G&gt; gl;
  19. public NativeList&lt;H&gt; hl;
  20. public NativeList&lt;I&gt; il;
  21. void Execute(Entity entity, A a, B b, C c, D d, E e, F f, G g, H h, I i)
  22. {
  23. if(typeof(A) == typeof(Null))
  24. al.Add(a);
  25. if(typeof(B) == typeof(Null))
  26. bl.Add(b);
  27. if(typeof(C) == typeof(Null))
  28. cl.Add(c);
  29. if(typeof(D) == typeof(Null))
  30. dl.Add(d);
  31. if(typeof(E) == typeof(Null))
  32. el.Add(e);
  33. if(typeof(F) == typeof(Null))
  34. fl.Add(f);
  35. if(typeof(G) == typeof(Null))
  36. gl.Add(g);
  37. if(typeof(H) == typeof(Null))
  38. hl.Add(h);
  39. if(typeof(I) == typeof(Null))
  40. il.Add(i);
  41. }
  42. }

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

  1. Allocator allocator = Allocator.Temp;
  2. var query = EntityManager.CreateEntityQuery(
  3. ComponentType.ReadOnly<CharacterControllerComponent>()
  4. , ComponentType.ReadOnly<LocalTransform>()
  5. , ComponentType.ReadOnly<AICharacterStateData>()
  6. , ComponentType.ReadOnly<AIInteractionSignalData>()
  7. );
  8. var entities = query.ToEntityListAsync(allocator, out var h0);
  9. var bodies = query.ToComponentDataListAsync<CharacterControllerComponent>(allocator, out var h1);
  10. var transforms = query.ToComponentDataListAsync<LocalTransform>(allocator, out var h2);
  11. var characterStates = query.ToComponentDataListAsync<AICharacterStateData>(allocator, out var h3);
  12. var signals = query.ToComponentDataListAsync<AIInteractionSignalData>(allocator, out var h4);
  13. JobHandle copyJobHandle = JobHandle.CombineDependencies(
  14. new NativeList<JobHandle>(Allocator.Temp) { h0, h1, h2, h3, h4 }
  15. );
英文:

You do not need to implement what is there already.

  1. Allocator allocator = Allocator.Temp;
  2. var query = EntityManager.CreateEntityQuery(
  3. ComponentType.ReadOnly&lt;CharacterControllerComponent&gt;()
  4. , ComponentType.ReadOnly&lt;LocalTransform&gt;()
  5. , ComponentType.ReadOnly&lt;AICharacterStateData&gt;()
  6. , ComponentType.ReadOnly&lt;AIInteractionSignalData&gt;()
  7. );
  8. var entities = query.ToEntityListAsync( allocator , out var h0 );
  9. var bodies = query.ToComponentDataListAsync&lt;CharacterControllerComponent&gt;( allocator , out var h1 );
  10. var transforms = query.ToComponentDataListAsync&lt;LocalTransform&gt;( allocator , out var h2 );
  11. var characterStates = query.ToComponentDataListAsync&lt;AICharacterStateData&gt;( allocator , out var h3 );
  12. var signals = query.ToComponentDataListAsync&lt;AIInteractionSignalData&gt;( allocator , out var h4 );
  13. JobHandle copyJobHandle = JobHandle.CombineDependencies(
  14. new NativeList&lt;JobHandle&gt;(Allocator.Temp){ h0 , h1 , h2 , h3 , h4 }
  15. );

答案2

得分: 0

我最终创建了多个系统以满足各种用途。
它还有助于简化不同的系统。

英文:

I ended up creating multiple systems for every use.
It also helps in simplifying different systems.

huangapple
  • 本文由 发表于 2023年6月12日 14:07:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定