码迷,mamicode.com
首页 > 编程语言 > 详细

Unity Optimisation Basics Part 1

时间:2015-01-13 16:05:17      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

转自:http://stevehalliwell.com/unity-optimisation-basics-part-1/


I recently decided to run the unity profiler over JSD and see what could be improved, this is just a quick review of my findings.

Searching is bad.

查找效率低

I already avoid all Find and GetComponents when ever I can so there weren’t many of them to find. I don’t recall if it was AltDevBlog or the Unity Blog itself or in a Unite or Learn video but it doesn’t matter. GameObject.Find*, Object.Find* and GetComponent are all expensive and wherever it was, explained that its simply because unity does a linear search through the collection to find them. Makes sense in a way, even if they were O(log n) it would still be expensive to do them all the time and would require the internal storage to be not cache sensitive or lazy.

Collision layers, use them.

假如子弹和子弹之间不发生碰撞,将子弹和敌人位于不同的layer,效率会提高

JSD has a lot of trigger rigidbodies, I had set it up so bullets didn’t overlap with withselves and never gave it another thought, until I saw 126 calls to OnTriggerEnter in 1 frame. Lots of bullets overlapping enemies however. It did a few checks and threw it away. Changing bullets to be in either a only collides with player layer and vice versa (an only collides with enemies layer) reduced this down to much more acceptable levels. This also drastically reduced the number of contact pairs the phys engine has to create and maintain which greatly lowered the overall and consistly high amount of cpu time spent in the physics engine.

yield rarely

yield即使没有取得结果,他也会造成10%额外的负担

The bullet spawners had a yield still in them from an earlier debugging attempt to prevent infinite loops during testing, this seemingly harmless line

was causing an additional 10% overhead on that function call. This again is not surprising, if you haven’t have a look at http://www.altdev.co/2011/07/07/unity3d-coroutines-in-detail/ its not an inconsiderable amount of work to make that co-routine happen, even if the line with the yield never gets hit.

Pools are not a silver bullet

我用对象池避免移动设备强制销毁内存。游戏过程中,你应该像避免瘟疫一样避免内存分配。我发现无论什么时候去激活对象ctiveInHierarchy = true都是十分昂贵的。

I use object pools (of my own creation) to avoid trashing memory which is mandatory on mobile if not everywhere. You should avoid allocs during gameplay like the plague. I found however that activating objects (.activeInHierarchy = true) is still quite expensive.

Surprisingly, splines are not using much at all, neither is overall rendering.


Unity Optimisation Basics Part 1

标签:

原文地址:http://blog.csdn.net/claien/article/details/42676571

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!