Unity3D优化总结

  1. 不要使用OnGUI(),它很慢,非常慢。
  2. 当需要频繁修改物体的localScale时,移除其collider组件,否则会非常影响帧率。
  3. 使用OnBecameVisible()/OnBecameInvisible()配对使用修改enabled属性,使得只有在对象可见时脚本才会执行。
    (物体不可见的时候drawcall是降低了,但是若自身带有脚本,还是执行的~~~)
  4. 使用InvokeRepeating()代替Update()处理不需要实时的数据更新。
  5. 使用对象池GameObjectPool缓存频繁使用的对象。

IOS平台优化建议
• Keep vertex count below:
• 40K per frame when targeting iPhone 3GS and newer devices (with SGX GPU)
• 10K per frame when targeting older devices (with MBX GPU)
• Keep the number of different materials per scene low - share as many materials between different objects as possible.
• Set Static property on a non-moving objects to allow internal optimizations.
• Use PVRTC formats for textures when possible, otherwise choose 16bit textures over 32bit.
• Use combiners or pixel shaders to mix several textures per fragment instead of multi-pass approach.
• If writing custom shaders, always use smallest possible floating-point types:
• fixed / lowp – perfect for color, lighting information and normals,
• half / mediump – for texture UV coordinates,
• float / highp – avoid in pixel shaders, fine to use in vertex shader for vertex position calculations.
• Minimize use of complex mathematical operations such as pow, sin, cos etc in pixel shaders.
• Do not use Pixel Lights when it is not necessary – choose to have only a single (preferably directional) pixel light affecting your geometry.
• Do not use dynamic lights when it is not necessary – choose baking lighting instead.
• Choose to use less textures per fragment.
• Avoid alpha-testing, choose alpha-blending instead.
• Do not use fog when it is not necessary.
• Learn benefits of Occlusion culling and use it to reduce amount of visible geometry and draw-calls in case of complex static scenes with lots of occlusion. Plan your levels to benefit from Occlusion culling.
• Use skyboxes to “fake” distant geometry.
更多优化相关的知识:官网
http://docs.unity3d.com/Documentation/Manual/OptimizingGraphicsPerformance.html

Modeling Characters for Optimal Performance

Below are some tips for designing character models to give optimal rendering speed.

Use a Single Skinned Mesh Renderer

You should use only a single skinned mesh renderer for each character. Unity optimizes animation using visibility culling and bounding volume updates and these optimizations are only activated if you use one animation component and one skinned mesh renderer in conjunction. The rendering time for a model could roughly double as a result of using two skinned meshes in place of a single mesh and there is seldom any practical advantage in using multiple meshes.

Use as Few Materials as Possible

You should also keep the number of materials on each mesh as low as possible. The only reason why you might want to have more than one material on a character is that you need to use different shaders for different parts (eg, a special shader for the eyes). However, two or three materials per character should be sufficient in almost all cases.

Use as Few Bones as Possible

A bone hierarchy in a typical desktop game uses somewhere between fifteen and sixty bones. The fewer bones you use, the better the performance will be. You can achieve very good quality on desktop platforms and fairly good quality on mobile platforms with about thirty bones. Ideally, keep the number below thirty for mobile devices and don’t go too far above thirty for desktop games.

Polygon Count

The number of polygons you should use depends on the quality you require and the platform you are targeting. For mobile devices, somewhere between 300 and 1500 polygons per mesh will give good results, whereas for desktop platforms the ideal range is about 1500 to 4000. You may need to reduce the polygon count per mesh if the game can have lots of characters onscreen at any given time. As an example, Half Life 2 used 2500-5000 triangles per character. Current AAA games running on the PS3 or Xbox 360 usually have characters with 5000-7000 triangles.

Keep Forward and Inverse Kinematics Separate

When animations are imported, a model’s inverse kinematic (IK) nodes are baked into forward kinematics (FK) and as a result, Unity doesn’t need the IK nodes at all. However, if they are left in the model then they will have a CPU overhead even though they don’t affect the animation. You can delete the redundant IK nodes in Unity or in the modeling tool, according to your preference. Ideally, you should keep separate IK and FK hierarchies during modeling to make it easier to remove the IK nodes when necessary.

Simple Checklist to make Your Game Faster

• Keep vertex count below 200K..3M per frame when targetting PCs, depending on the target GPU
• If you’re using built-in shaders, pick ones from Mobile or Unlit category. They work on non-mobile platforms as well; but are simplified and approximated versions of the more complex shaders.
• Keep the number of different materials per scene low - share as many materials between different objects as possible.
• Set Static property on a non-moving objects to allow internal optimizations like static batching.
• Do not use Pixel Lights when it is not necessary - choose to have only a single (preferably directional) pixel light affecting your geometry.
• Do not use dynamic lights when it is not necessary - choose to bake lighting instead.
• Use compressed texture formats when possible, otherwise prefer 16bit textures over 32bit.
• Do not use fog when it is not necessary.
• Learn benefits of Occlusion Culling and use it to reduce amount of visible geometry and draw-calls in case of complex static scenes with lots of occlusion. Plan your levels to benefit from ccclusion culling.
• Use skyboxes to “fake” distant geometry.
• Use pixel shaders or texture combiners to mix several textures instead of a multi-pass approach.
• If writing custom shaders, always use smallest possible floating point format:
o fixed / lowp - for colors, lighting information and normals,
o half / mediump - for texture UV coordinates,
o float / highp - avoid in pixel shaders, fine to use in vertex shader for position calculations.
• Minimize use of complex mathematical operations such as pow, sin, cos etc. in pixel shaders.
• Choose to use less textures per fragment.