Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I use StaticOptimizeEntity in Dots1.0?

Discussion in 'Entity Component System' started by MatanYamin, Apr 24, 2023.

  1. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109
    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?
     
    Last edited: Apr 24, 2023
    charleshendry likes this.
  2. charleshendry

    charleshendry

    Joined:
    Jan 7, 2018
    Posts:
    95
    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.
     
    MatanYamin likes this.
  3. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    The baking system currently determines if the object is static like this:

    Code (CSharp):
    1. /// <summary>
    2. /// Checks if the GameObject is static for a given GameObject.
    3. /// </summary>
    4. /// <param name="gameObject">The GameObject to check</param>
    5. /// <returns>Returns true if the object or one of its ancestors is static, otherwise returns false.</returns>
    6. /// <remarks>This takes a dependency on the static state</remarks>
    7. public bool IsStatic(GameObject gameObject)
    8. {
    9.     // Intentionally using gameObject.GetComponentInParent instead of the baker version.
    10.     // We want this baker to trigger when the overall static value changes, not StaticOptimizeEntity is added/removed
    11.     var staticOptimizeEntity = gameObject.GetComponentInParent<StaticOptimizeEntity>(gameObject) != null;
    12.     bool isStatic = (staticOptimizeEntity || InternalIsStaticRecursive(gameObject));
    13.  
    14.     _State.Dependencies->DependOnStatic(gameObject.GetInstanceID(), _State.AuthoringId, isStatic);
    15.  
    16.     return isStatic;
    17. }
    18.  
    19. /// <summary>
    20. /// Returns if the GameObject or one of its parents is static
    21. /// </summary>
    22. /// <param name="gameObject">The GameObject to check.</param>
    23. /// <returns>Returns true if the object or one of its ancestors is static, otherwise returns false.</returns>
    24. private static bool InternalIsStaticRecursive(GameObject gameObject)
    25. {
    26.     ...
    27. }
    28.  
    It looks like you can just set any parent GameObject as static and it should already behave as if the StaticOptimizeEntity component is added.
     
    MatanYamin likes this.
  4. MatanYamin

    MatanYamin

    Joined:
    Feb 2, 2022
    Posts:
    109