Refactoring and improving performance. Update #7
This week no big visible changes from last update. After testing my last build more I realized I got huge fps drops all the time. So I decided to work on improving performance. After some quick searches of how to improve performance and where to look this is what helped me:
One of the biggest changes was implementing object pooling.
Without going into too much detail I figured that allocating memory for every projectile being shot just to remove it from memory second later might be the cause. Instead they should be just disabled when they hit and later re-enabled as new projectiles from a different unit (or the same one). Luckily this could be done with minimum changes.
First I created method GetPoolItem() which both returns pooled item or creates new one if there is no free to use object of that prefab type. Then I had to add Some sort of OnResuse() function to make sure object is set properly as now I can't depend on Unity's Start() method.
Other huge benefit came from trying out Unity Profiler Tool. With this I could better analyze what else causes frame drop and work from there. It was a little confusing at first, a specially when at first it seemed that performance issued were outside of my code.
But I dug deeper and realized I had a problem with unit attack script. To detect if unit is within attack range I used collider set a trigger. When something entered the trigger the script would look if the object had a GameEntity script attached to it. I didn't know GetComponent was considered resource cost intensive method. But worst of all, trigger collider component was left inside an object that was in default layer so it got triggered by literally everything - not just units, but also projectiles, trees and such. Just creating a new layer for range triggers that collide exclusively with GameEntityies was enough to get additional boost in performance.
hope this helps someone else as it helped me.Files
Get rts prototype
rts prototype
More posts
- New tank models. Update #12Feb 18, 2024
- New Unit. Update #11Jan 14, 2024
- New models. Update#10Nov 04, 2023
- Ice and Fire. Update #9Oct 21, 2023
- Improvments and fixes. Update #8Oct 15, 2023
- Update #6Oct 01, 2023
- Weekly Update #5Sep 24, 2023
- Update #4Sep 16, 2023
- Some improvments. Update #3Sep 08, 2023
Leave a comment
Log in with itch.io to leave a comment.