Search Unity

DOTS VERSION > Get the bounds of all Entities in the Scene (convert this code)?

Discussion in 'Entity Component System' started by GDevTeam, May 27, 2020.

  1. GDevTeam

    GDevTeam

    Joined:
    Feb 14, 2014
    Posts:
    90
    UPDATE:
    (trying to get one big bounding value based on all entities in the Scene)

    If I do a Entities.ForEach(in WorldRenderBounds bounds), looping through all Entities in the Scene, I can see bounds for each Entity with Debug.Log(bounds.Value);

    But I need to add these all together to get a bound value for all entities. Or is there a better way of doing?

    __________________________________________________________________________

    Say I used this code before in Unity "Classic" (OOP). How would I convert it or code in DOTS to get the same outcome. I need to have the Bounds for all Entities in the scene (so it grows as each are added to say a collection, etc.).

    Code (CSharp):
    1.     public static Bounds GetBounds(GameObject go) // Parent GameObject with Children
    2.  
    3.     {
    4.         Bounds bounds = new Bounds();
    5.         Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
    6.  
    7.         if (renderers.Length > 0)
    8.  
    9.         {
    10.             //Find first enabled renderer to start encapsulate from it
    11.             foreach (Renderer renderer in renderers)
    12.  
    13.             {
    14.                 if (renderer.enabled)
    15.  
    16.                 {
    17.                     bounds = renderer.bounds;
    18.  
    19.                     break;
    20.                 }
    21.             }
    22.  
    23.             //Encapsulate for all renderers
    24.             foreach (Renderer renderer in renderers)
    25.             {
    26.                 if (renderer.enabled)
    27.                 {
    28.                     bounds.Encapsulate(renderer.bounds);
    29.                 }
    30.             }
    31.         }
     
    Last edited: May 28, 2020