DefaultVariants based on component composition

It would be good if there was a system and workflow similar to the DefaultVariantSystemBase where we could add custom logic to which variants should be used per component composition.

My current case; I want to change the LocalTransform to DontSerialize variant when I have authored a custom transform component that contains more specialized data that handle interpolation better for my case. It’s error prone to have to set up InspectionComponents on all these cases (I looked a bit into try to do some OnValidate for the Inspection based on the authored component, but that’s a whole mess in itself as well by how it works with baking it).

GhostPrefabCreation.ConvertToGhostPrefab has an overrides parameter that allows overriding variants per components.

        overrides[new GhostPrefabCreation.Component
        {
            ComponentType = ComponentType.ReadWrite<LocalTransform>(),
            ChildIndex = 0
        }] = new GhostPrefabCreation.ComponentOverride
        {
            OverrideType = GhostPrefabCreation.ComponentOverrideType.Variant,
            Variant = GhostVariantsUtility.UncheckedVariantHashNBC(typeof(PositionOnlyVariant), ComponentType.ReadOnly<LocalTransform>())
        };

But this is for runtime creation of prefabs using GhostPrefabCreation.ConvertToGhostPrefab. Let us know if that’s not enough for your use case.

I don’t know what the cons or limitations for using runtime prefab creation. Ideally I would just use the regular prefab authoring, have some custom systems to run to set the default variants; and then still be able to use the inspection component if you would want to override those rules even more.

Just want to point out I had another case of this today. Where I want e.g. archetypes with dynamics or CC have a specific GhostComponent.PrefabType.

Yeah true, having something that just integrates with existing prefab authoring would be better.
There’s a few methods in GhostAuthoringInspectionComponent that are internal. Things like GetOrAddPrefabOverride.
You can get the variant hash from ComponentTypeSerializationStrategy.
If we made those public and you accessed those during baking, could that solve your use case?

The issue with rule based approaches is we could solve for your use case, but then someone else will have a different way they want to filter/update variants. Exposing a way for you to set those variants yourself through code seems more flexible?

The overrides are stored in GhostAuthoringInspectionComponent right? So it would mean forcing settings in the GhostAuthoringInspectionComponent, not the best user experience I guess, but better than nothing.

@Jawsarn Yeah fair. Would your ideal API be at baking time? E.g. Something like:

    AddComponent<MyCustomTransform>(entity);
    AppendToBuffer(entity, new GhostVariantOverride {
        Replace = ComponentType.ReadWrite<LocalTransform>(),
        WithSpecial = GhostVariantOverride.SpecialCase.DontSerializeVariant,
        Priority = 1,
        // Note: You can just remove the LocalTransform via the GetEntity flag option, but I assume you still want it?
    });
    AddComponent<MyCharacterController>(entity);
    AppendToBuffer(entity, new GhostVariantOverride {
        Replace = ComponentType.ReadWrite<SomeComponent>(),
        PrefabTypeOverride = GhostPrefabType.PredictedClient,
        Priority = 1,
    });

Yeah, that would be very nice and also be possible to use in baking systems at some step. Especially if it’s a baking only type to not take space in runtime.