您的位置:首页 > 移动开发 > Unity3D

Performance Optimization Tips And Tricks for Unity3D

2014-03-29 19:42 190 查看

来自Unity3D官网上的Tech/Biz Talks-Performance Optimization Tips and Tricks for Unity,在这里下载

Profiler

  CPU usage:Time Cost, GC Allocating

  GPU usage:DrawCalls ...

  Rendering:

  Memory :

Optimizing your Scripts

  *Get details on interesting code by bracing it with: 

Profiler.BeginSample("MyInterestingFunction");
//your code here...
Profiler.EndSample();

 

     *Optimizing your algorithms is programming 101 and not covered here:)

  *If scripts continuously use too much time, do you need to run them all the time?

  *Maybe you can cull script execution for visible objects? Use OnBecameVisible,

OnBecameInvisible callbacks to diable your script.

  *Maybe you don't need to update every frame? Use Coroutines instead:

  

//Do some stuff every frame:
void Update()
{

}
  //Do some stuff every 0.2 seconds:
IEnumerator Start()
{
while(true)
{
yield return new WaitForSeconds(0.2f);
}
}

    *UnityEngine APIs may have unneeded overhead.Consider caching results instead of

calling UnityEngine methods each frame.

  *This also applies to some C# getter properties. For instance.

  *Transform.position will iterate the transform hierarchy to calculate global position.

Optimizing Memory usage

  Mono Memory

  *Script objects

  *Wrappers for

    *Game objects

    *Assets

    *Components

  Unity Internal Memory

  *Asset data

    * Textures, audio data, meshes, animation etc.

  *Game objects

  *Internals

    *Rendering, particles, physics etc.

  Garbage collected

  *Mono allocates heap blocks for allocations.

  *When available heap size runs out, new blocks are added

  *Fragmentation can cause new heap blocks,even though memory is not exhausted

  *Garbage Collector cleans up when

    *Allocation does not fit in available memory

    *System.GC.Collect()

  *Avoid allocations to get rid of Garbage Collection.

  *But it's all managed code! We have no control over allocations!

  *Not quite right:Using C# does allow some control over memory allocations.

  *Use structs instead of classes for local data!  

1   class MyClass
2     {
3         public int a,b,c;
4     }
5     struct MyStruct
6     {
7         public int a,b,c;
8     }
9     void Update()
10     {
11         //Bad:allocated on the heap, will be garbage collected later!
12         MyClass c=new MyClass();
13
14         //Good:allocated on the stack, no GC going to happen!
15         MyStruct s=new MyStruct();
16     }

  *You can use lists of preallocated, reusable class instances to implement your own

memory management scheme.

  *Examples of object pooling can be found in Astro Dude on the Asset Store

  *Instantiation can be slow

  *Prefetch objects you are going to use.

  *For securring objects(Bullets,Explosions) it may make sense to keep a List of inactive

GameObjects around and reuse them instead of Instantianting and Destroying them!

  *Avoid using Unity's built-in GUI system in game,as it is not very GC friendly.

  *Non-streaming games can usually get by without doing any allocations while playing!

  *use GetRuntimeMemroySize(Object), GetMonoHeapSize and GetMonoUsedSize to keep

track of memory usage.

Optimizing Physics

  *Prefer primitive colliders over mesh colliders.

  *Tweak Time.fixedDeltaTime(in Project settings->Time) to be as high as you can get 

away with. if your game is slow moving, you probably need less fixed updates then

  games with fast action.

  *The most common performance pitfall using physics:

    *Never ever move a static collider(i.e. a collider with out a Rigidbody)!

    *Shows up in Profiler as "StaticCollider.Move" but actual processing is in Physics.Simulate!

Optimizing Graphics

  *How fast you can render is bound by GPU and CPU performance.

    *GPu performance is mostly limited by the amount of pixels rendered(fill rate) and

by memory bandwidth.

    *CPU performance is sometimes limited by the amount of draw calls processed.

  *Use the GPU Profiler, to find out how much time is spent rendering each draw call in your

scene.

  *Any draw call you can get rid off completely saves rendering time on both GPU and CPU,

so first let's see if there are any draw calls we can do without.

  *After that, let's see what we can do to make your objects render faster.

  *Improve performance by rendering fewer objects:

    *By reducing scene complexity.

    *By culling more aggressively(occlusion culling,frustum culling).

    *By using LOD.

  *Improve performance by reducing shader passes:

    *Choose a scene setup and shaders with fewer passes(reflections,shadows,pixel lights in

forward rendering all add passes).

    *Forward rendered pixel lights are expensive. Use light mapping instead of realtime lights

wherever possible.

    *Adjust pixel light count in quality settings.

Optimizing Graphics(CPU)

  *CPU performance is sometimes limited by the amount of draw calls to process.

  *Rule of thumb:don't use more than a few hundred draw calls per frame on mobiles or a few

thousand on desktops for current hardware.

  *You can use fewer draw calls by combining nearby objects into single meshes, so they are

drawn together.

    *Manually in your source assets.

    *Automatically using Unity's draw call batching.

  *Use fewer different materials to allow for better batching of meshes.

    *Use Atlas textures where possible.

  *If you are not bound by draw calls, then batching is actually worse for performance, as it

makes culling less efficient and makes more objects affected by lights!

Optimizing Graphics(GPU)

  *GPU performance is often limited by fillrate or memory bandwidth.

    *Checking if you are fillrate-bound is easy:does the game run faster if you decrease the display

resolution? if yes, you are limited by fillrate.

    *Likewise, if reducing the Texture Quality in Quality Settings makes the game run faster,  you

are probably limited by memory bandwidth.

  *if you are fillrate bound, try reducing shader complexity:

    *Moblie GPUs:avoid alpha-testing shaders,prefer alpha-blended versions.

    *Use simple,optimized shader code(such as the "Mobile" shaders coming with Unity).

      *Optimizing your shader code:

        *Avoid expensive math functions in shader code(pow,exp,log,cos,sin,tan,etc).Consider using

pre-calculated lookup textures instead.

        *Mobile:pick lowest possible number precision format(float,half,fixed in Cg) for best performance.

  *if you are memory bandwidth bound,decrease required texture memory:

    *Use texture compression or 16 bit textures.

    *Reduce texture size(check "Mipmaps" render mode in scene view).

    *Use mipmaps for an texture used in 3D scene.

  *GPU performance can also be bound by Geometric complexity, i.e. the amount of vertices the GPU

needs to process.

    *Don't use more triangles than necessary.

    *Keep number of UV seams and hard edges(vertices with multiple normals) as low as possible.

    *Use simpler vertex shaders.

转载于:https://www.cnblogs.com/suntabu/p/3632740.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: