OndrejNepozitek / Edgar-Unity

Unity Procedural Level Generator
https://ondrejnepozitek.github.io/Edgar-Unity/docs/introduction
MIT License
817 stars 70 forks source link

Disable colliders before copying tiles #50

Open OndrejNepozitek opened 4 years ago

OndrejNepozitek commented 4 years ago

It seems that if colliders are enabled on shared tilemaps when we copy individual room templates, Unity tries to incrementally rebuild them to update their shape. When working with bigger levels, this can cause noticeable lag (up to 10 seconds on levels with hundreds of rooms, 1-2 seconds on medium sized levels).

Based on some quick tests, it seems like it is better to first disable all colliders and only enable them after all tiles are copied to the final level. The difference might not be noticeable on small levels, but it went from 10 seconds to cca 2 seconds iirc. We need to do some additional performance tests.

There are at least 2 possible approaches to handle this:

  1. We can split tilemap layers initialization into 2 stages. In the first stage, we would create tilemaps without colliders and in the second stage, we would only add the colliders. The second stage would be called only after all the tiles were copied. The problem of this approach is that it makes it harder for uses to implement their own tilemap initialization logic because they would need to split the logic.
  2. Before copying tiles, we can go through all the colliders of individual tilemap layers, mark which ones were enabled and then disable them. We would then copy all the tiles and after that we would enable all the collider that were disabled in the previous step. The advantage of this approach is that it is transparent for users. We just have to make sure that there are no problems with disabling and then enabling colliders.