How do I use StaticOptimizeEntity in Dots1.0?

Hey,

Do I need to add the StaticOptimizeEntity component directly to the SubScene or to the prefab that about to be converted into Entity?

Or to the GameObject that holds the Authoring to the prefab?

Also, is it possible to add this by runtime to a script-created entity?

1 Like

I think if you’re creating Entities in a subscene, you can just tick the Static toggle. When enabled, the LocalTransform component will not be added. Just the LocalToWorld and a Static tag component. So the transform system won’t iterate through them.

1 Like

The baking system currently determines if the object is static like this:

/// <summary>
/// Checks if the GameObject is static for a given GameObject.
/// </summary>
/// <param name="gameObject">The GameObject to check</param>
/// <returns>Returns true if the object or one of its ancestors is static, otherwise returns false.</returns>
/// <remarks>This takes a dependency on the static state</remarks>
public bool IsStatic(GameObject gameObject)
{
    // Intentionally using gameObject.GetComponentInParent instead of the baker version.
    // We want this baker to trigger when the overall static value changes, not StaticOptimizeEntity is added/removed
    var staticOptimizeEntity = gameObject.GetComponentInParent<StaticOptimizeEntity>(gameObject) != null;
    bool isStatic = (staticOptimizeEntity || InternalIsStaticRecursive(gameObject));

    _State.Dependencies->DependOnStatic(gameObject.GetInstanceID(), _State.AuthoringId, isStatic);

    return isStatic;
}

/// <summary>
/// Returns if the GameObject or one of its parents is static
/// </summary>
/// <param name="gameObject">The GameObject to check.</param>
/// <returns>Returns true if the object or one of its ancestors is static, otherwise returns false.</returns>
private static bool InternalIsStaticRecursive(GameObject gameObject)
{
    ...
}

It looks like you can just set any parent GameObject as static and it should already behave as if the StaticOptimizeEntity component is added.

1 Like

Thanks charleshendry and apkdev!