Huge CPU spike when erasing a tile at runtime

Sclouto
New Member
Registered: 2017-04-10
Posts: 1

Topic

Hello Rotorz people,

I'm trying to destroy tiles at runtime, and it works, however the cpu is getting hit much harder than I would expect it to.

I'm using unity 5.5.0f3 and Rotorz 2.4.4
This is my code:

    
    private void Update()
    {
        Vector3 mousePos = Vector3.zero;
        mousePos.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
        mousePos.y = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
        mousePos.z = transform.position.z;

        TileIndex tileIndex = tileSystem.ClosestTileIndexFromWorld(mousePos);

        if(tileSystem.EraseTile(tileIndex))
        {
            tileSystem.RefreshSurroundingTiles(tileIndex);
        }        
    }

This is what I get in the profiler:
Profiler

And here are my tile system settings:
Profiler

14ms to erase a tile and 29ms to refresh the tiles around it seems a bit much, so I guess I'm doing something wrong. The brush used is a procedural tileset brush with a simple prefab that contains only a boxColider2D and a kinematic rigidbody attached to it.

Is there any way I can improve this or is this the expected behavior?

Thanks :)!

Last edited by Sclouto (2017-04-10 14:17:38)

Lea Hayes
Rotorz Limited
From: United Kingdom
Registered: 2014-03-04
Posts: 629

Response 1

Try using this logic instead and let me know whether this performs better for your situation:

if (tileSystem.GetTile(tileIndex) != null)
{
    tileSystem.BeginBulkEdit();
    tileSystem.EraseTile(tileIndex);
    tileSystem.RefreshSurroundingTiles(tileIndex);
    tileSystem.EndBulkEdit();
}

Procedural tile map updates can be reduced to a single update by applying all changes using the bulk editing mode. This means that the tile mesh only has to be updated one time rather than once per tile change. Bulk editing mode can be nested such that the more expensive changes only occur once the root-most bulk editing has ended.

Tiles are much more static in nature than dynamic sprites are. You may want to consider making things like doors, or destructible walls using sprite prefabs rather than using tiles.

Unity has a certain amount of internal overhead when it comes to instantiating prefabs and destroying game objects, especially when they contain complicated components such as rigidbody's and colliders. If the same tile prefabs are being created and destroyed frequently, then one of the things that you can do to alleviate these pressures is to pool them: http://rotorz.com/blog/2013/02/pool-you … oolmanager

I hope that this helps with your enquiry :)