#How can I bake Managed component with Disabled state?

1 messages · Page 1 of 1 (latest)

hot sonnet
#

I'm trying to bake a managed component (TestComponent) that starts in a disabled state, but I've run into an API inconsistency.

My Code:

class TestComponent : IComponentData, IEnableableComponent
{
    public WeakObjectReference<GameObject> Test;
}

class TestComponentAuthor : MonoBehaviour
{
    public WeakObjectReference<GameObject> Test;
    class TestComponentBaker : Baker<TestComponentAuthor>
    {
        public override void Bake(TestComponentAuthor authoring)
        {
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            AddComponentObject(entity, new TestComponent
            {
                Test = authoring.Test
            });
            // Set TestComponent to disabled
            SetComponentEnabled<TestComponent>(entity, false);
        }
    }
}

The Baker.SetComponentEnabled<T> method doesn't accept class types (managed components), only structs. However, EntityCommandBuffer.SetComponentEnabled<T> has an overload that supports managed components:

// From EntityCommandBuffer - this works with managed components:
public static void SetComponentEnabled<T>(this EntityCommandBuffer ecb, Entity e, bool value) 
    where T : class, IEnableableComponent, new()
{
    // implementation...
}

Is there a supported way to bake managed components in a disabled state?

I'm considering baking the component as enabled, then disabling it in a system on Start or on first update.

I need to use a managed component because of the GameObject references, but I want them to start disabled by default. Any help is appreciated!

opal forge
obsidian crescent
#

You could use a BakingSystem to disable the component..

hot sonnet
#

Thanks, I missed everything about BakingSystem, it does make sense. However, I would appreciate if this could be solvable inside Baker.Bake. Unity should definitely add it. I'm not sure where I can request feature for this. Any tips?

barren abyss
hot sonnet
# barren abyss Isn't there an overload to set enabled state that accepts ComponentType?

In Baker there are listed only two methods:

        /// <summary>
        /// Sets the enabled value of the component on the Entity.
        /// </summary>
        /// <remarks>
        /// This method can only be invoked if the same baker instance previously added this specific component.
        /// </remarks>
        /// <param name="entity">The Entity to set the component to</param>
        /// <param name="enabled">True if the specified component should be enabled, or false if it should be disabled</param>
        /// <typeparam name="T">The type of component to set</typeparam>
        public void SetComponentEnabled<T>(Entity entity, bool enabled) where T : struct, IEnableableComponent
        {
            if (_State.PrimaryEntity != entity)
                CheckValidAdditionalEntity(entity);
            else
                CheckComponentHasBeenAddedByThisBaker(entity, TypeManager.GetTypeIndex<T>());

            _State.Ecb.SetComponentEnabled<T>(entity, enabled);
        }

        /// <summary>
        /// Sets the enabled value of the component on the primary Entity.
        /// </summary>
        /// <param name="enabled">True if the specified component should be enabled, or false if it should be disabled</param>
        /// <typeparam name="T">The type of component to set</typeparam>
        /// <remarks>Implicitly it will access the primary entity with TransformUsageFlags.Dynamic.</remarks>
        [Obsolete("Use the version of the function with the explicit Entity parameter (RemovedAfter Entities 1.0)")]
        public void SetComponentEnabled<T>(bool enabled) where T : struct, IEnableableComponent
        {
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            SetComponentEnabled<T>(entity, enabled);
        }
barren abyss
#

Well unfortunately you need baking system then

#

As a slightly different solution- you can create separate tag component and use it as enablable instead

#

While managed remains just normal one