How is RegisterGenericComponentType attribute used?

While upgrading, I got this error while playing the project:

ArgumentException: Unknown Type:CommonEcs.CollectSharedComponentsSystem1+Collected[CommonEcs.SpriteLayer]` All ComponentType must be known at compile time. For generic components, each concrete type must be registered with [RegisterGenericComponentType].

I don’t know how to use it. It says it can only be used in Assembly declarations. I tried setting as specified here but it doesn’t work. Can somebody show code on how use this?

I got it to working in compilation but it still showed the same error on play. I tried this:

[assembly: RegisterGenericComponentType(typeof(SpriteLayerInstancesSystem))]

namespace CommonEcs {
    [UpdateInGroup(typeof(PresentationSystemGroup))]
    public class SpriteLayerInstancesSystem : CollectSharedComponentsSystem<SpriteLayer> {
    }
}

and this:

[assembly: RegisterGenericComponentType(typeof(CollectSharedComponentsSystem<SpriteLayer>))]

namespace CommonEcs {
    [UpdateInGroup(typeof(PresentationSystemGroup))]
    public class SpriteLayerInstancesSystem : CollectSharedComponentsSystem<SpriteLayer> {
    }
}

I also tried setting it to the class mentioned in the error message. It still doesn’t work:

[assembly: RegisterGenericComponentType(typeof(SpriteLayerInstancesSystem))]

namespace CommonEcs {
    public class AddGameObjectSpriteToLayerSystem : ComponentSystem {
        ...
    }

Have a look at this post

2 Likes

Thanks

Still didn’t work. Where are you supposed to place AssemblyInfo.cs? Is it the same folder as the component with generic parameters?

Also tried deleting PackageCache and ScriptAssemblies. Still didn’t work. :frowning: I hope a Unity dev can comment.

You can put it anywhere in your project.
I’m using that in my project and I have a few generics. They all work fine for me.

Oh I see your generics are on the system and not on the component. That shouldn’t even require you to declare the generics.
What is the definition for CollectSharedComponentsSystem<> and SpriteLayer?

Here

public abstract class CollectSharedComponentsSystem<T> : ComponentSystem where T : struct, ISharedComponentData {
    ...
}

public struct SpriteLayer : ISharedComponentData, IEquatable<SpriteLayer> {
    ...
}

That doesn’t require you to register generics. It must be something else that you are not showing.
By looking at the error more carefully it points otherwise. It says that there is a component Collected and that is what must be registered.

I think it’s this one:

public abstract class CollectSharedComponentsSystem<T> : ComponentSystem where T : struct, ISharedComponentData {
        protected struct Collected : ISystemStateComponentData {
        }

    ...
}

Should I register it as:

[assembly: RegisterGenericComponentType(typeof(CollectSharedComponentsSystem<SpriteLayer>.Collected))]

Edit:
It worked. Thanks for the help!