NavMeshPlus icon indicating copy to clipboard operation
NavMeshPlus copied to clipboard

The "Bake Runtime" system is tiring for mobile devices...?

Open GugaGuga9 opened this issue 2 years ago • 3 comments

I did exactly what you wrote in the "How To" section for the 2D game and it worked. I used BuildNavMesh in the lateupdate function of the "NavMesh" object to update the walkable area after the destroyed objects (Tile) were destroyed during the game and it worked again. Paths are now updated while the game is running. Thank you so much.

What I'm wondering is how tiring this system can be for mobile devices. Issues such as heating the device immediately... What do you think?

GugaGuga9 avatar Jan 09 '22 23:01 GugaGuga9

Hello @GugaGuga9

I don't have ability to test such case. But in general:

  1. Call Surface2D.BuildNavMeshAsync() only once
  2. Don't call NavMeshBuilder.UpdateNavMeshDataAsync every frame, only when needed
  3. Use cache to provide new sources into NavMeshBuilder.UpdateNavMeshDataAsync(data, GetBuildSettings(), **sources**, sourcesBounds)

h8man avatar Jan 10 '22 07:01 h8man

Thanks. I will try. I will post possible problems and consequences here.

GugaGuga9 avatar Jan 10 '22 15:01 GugaGuga9

When the relevant area of the tilemap explodes, the tile and of course the collider there are also destroyed, and only then the "navmesh" path is updated. By doing this, instead of running it in every frame, I run it only when needed. This is how I did it:

(I just used this "Surface2D.BuildNavMesh();") I haven't had a performance test for mobile devices yet. But it seems to work better than the previous method.

 using UnityEngine;
 using UnityEngine.Tilemaps;
 using UnityEngine.AI;

 public class DestroyScript : MonoBehaviour
 {
public Tilemap tilemap;

public Tile wallTile;
public Tile destructibleTile;

public NavMeshSurface2d Surface2D;

public void Explode(Vector2 worldPos)
{
    Vector3Int originCell = tilemap.WorldToCell(worldPos);

    ExplodeCell(originCell + new Vector3Int(1, 0, 0));
    ExplodeCell(originCell + new Vector3Int(0, 1, 0));
    ExplodeCell(originCell + new Vector3Int(-1, 0, 0));
    ExplodeCell(originCell + new Vector3Int(0, -1, 0));

    ExplodeCell(originCell + new Vector3Int(1, 1, 0));
    ExplodeCell(originCell + new Vector3Int(1, -1, 0));
    ExplodeCell(originCell + new Vector3Int(-1, 1, 0));
    ExplodeCell(originCell + new Vector3Int(-1, -1, 0));

    Surface2D.BuildNavMesh();
}

void ExplodeCell(Vector3Int cell)
{
    Tile tile = tilemap.GetTile<Tile>(cell);

    if (tile == wallTile)
    {
        return;
    }

    if (tile == destructibleTile)
    {
        tilemap.SetTile(cell, null);
    }

}

}

GugaGuga9 avatar Jan 11 '22 16:01 GugaGuga9