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

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

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&lt;Entity&gt; Entities;
        public NativeList&lt;CharacterControllerComponent&gt; Bodies;
        public NativeList&lt;LocalTransform&gt; Transforms;
        public NativeList&lt;AICharacterStateData&gt; CharacterStates;
        public NativeList&lt;AIInteractionSignalData&gt; 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&lt;A,B,C,D,E,F,G,H,I&gt; : 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&lt;A&gt; al;
        public NativeList&lt;B&gt; bl;
        public NativeList&lt;C&gt; cl;
        public NativeList&lt;D&gt; dl;
        public NativeList&lt;E&gt; el;
        public NativeList&lt;F&gt; fl;
        public NativeList&lt;G&gt; gl;
        public NativeList&lt;H&gt; hl;
        public NativeList&lt;I&gt; 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&lt;CharacterControllerComponent&gt;()
    ,	ComponentType.ReadOnly&lt;LocalTransform&gt;()
    ,	ComponentType.ReadOnly&lt;AICharacterStateData&gt;()
    ,	ComponentType.ReadOnly&lt;AIInteractionSignalData&gt;()
);    
var entities = query.ToEntityListAsync( allocator , out var h0 );
var bodies = query.ToComponentDataListAsync&lt;CharacterControllerComponent&gt;( allocator , out var h1 );
var transforms = query.ToComponentDataListAsync&lt;LocalTransform&gt;( allocator , out var h2 );
var characterStates = query.ToComponentDataListAsync&lt;AICharacterStateData&gt;( allocator , out var h3 );
var signals = query.ToComponentDataListAsync&lt;AIInteractionSignalData&gt;( allocator , out var h4 );
    	
JobHandle copyJobHandle = JobHandle.CombineDependencies(
	new NativeList&lt;JobHandle&gt;(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.

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:

确定