Search code examples
c#unity-game-engineunity-ecs

nullable generics type function in C# Unity


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.


Solution

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