private void OnDestroy()
 {
     Manager.Assets.onInitialize.RemoveListener(InitGrid);
     grid = null;
 }
        private void InitGrid()
        {
            // Collect components
            GameObject[]           rootGameObjects  = SceneManager.GetActiveScene().GetRootGameObjects();
            List <Renderer>        tempRenderers    = new List <Renderer>();
            List <Collider>        tempColliders3D  = new List <Collider>();
            List <Collider2D>      tempColliders2D  = new List <Collider2D>();
            List <TerrainCollider> terrainColliders = new List <TerrainCollider>();

            for (int i = 0; i < rootGameObjects.Length; ++i)
            {
                if (generateGridByRenderers)
                {
                    tempRenderers.AddRange(rootGameObjects[i].GetComponentsInChildren <Renderer>(includeInactiveComponents));
                }
                if (generateGridByCollider3D)
                {
                    tempColliders3D.AddRange(rootGameObjects[i].GetComponentsInChildren <Collider>(includeInactiveComponents));
                }
                if (generateGridByCollider2D)
                {
                    tempColliders2D.AddRange(rootGameObjects[i].GetComponentsInChildren <Collider2D>(includeInactiveComponents));
                }
                if (generateGridByTerrain)
                {
                    terrainColliders.AddRange(rootGameObjects[i].GetComponentsInChildren <TerrainCollider>(includeInactiveComponents));
                }
            }
            // Make bounds
            bool   setBoundsOnce = false;
            Bounds bounds        = default;

            if (generateGridByRenderers)
            {
                foreach (Renderer comp in tempRenderers)
                {
                    if (!setBoundsOnce)
                    {
                        bounds = comp.bounds;
                    }
                    else
                    {
                        bounds.Encapsulate(comp.bounds);
                    }
                    setBoundsOnce = true;
                }
            }
            if (generateGridByCollider3D)
            {
                foreach (Collider comp in tempColliders3D)
                {
                    if (!setBoundsOnce)
                    {
                        bounds = comp.bounds;
                    }
                    else
                    {
                        bounds.Encapsulate(comp.bounds);
                    }
                    setBoundsOnce = true;
                }
            }
            if (generateGridByCollider2D)
            {
                foreach (Collider2D comp in tempColliders2D)
                {
                    if (!setBoundsOnce)
                    {
                        bounds = comp.bounds;
                    }
                    else
                    {
                        bounds.Encapsulate(comp.bounds);
                    }
                    setBoundsOnce = true;
                }
            }
            if (generateGridByTerrain)
            {
                foreach (TerrainCollider comp in terrainColliders)
                {
                    if (!setBoundsOnce)
                    {
                        bounds = comp.terrainData.bounds;
                    }
                    else
                    {
                        bounds.Encapsulate(comp.terrainData.bounds);
                    }
                    setBoundsOnce = true;
                }
            }
            // Generate grid
            switch (axisMode)
            {
            case EAxisMode.XZ:
                grid = new StaticGrid2D <uint>(
                    new Vector2(bounds.min.x, bounds.min.z),
                    bounds.size.x, bounds.size.z, cellSize);
                break;

            case EAxisMode.XY:
                grid = new StaticGrid2D <uint>(
                    new Vector2(bounds.min.x, bounds.min.y),
                    bounds.size.x, bounds.size.y, cellSize);
                break;
            }
            AxisMode = axisMode;
        }