This is part 1 of several blogs covering Tips and Tricks when using Unity. This (part 1) includes Memory Optimization and working with Textures and was compiled by Steve H (Intel).
Working with Textures
Design Tips
- Normal maps don’t need to be 1:1 scale with diffuse maps.
- Remove redundant keyframes from animations (only refresh to change graphic)
For Mesh Models
- Remove unused channels from meshes (under Player settings, enable Optimized Mesh Data)
- For high detail meshes on low RAM systems, set Quality to use maxLOD
An article with more detail on Graphics Optimization in Unity is available at https://software.intel.com/en-us/articles/how-to-plan-optimizations-with-unity
Audio
- Make use of the 200k buffer for audio playback
- Load short clips uncompressed into this buffer
- Or use AudioClip.GetData to uncompress, and then free the compressed one
Memory Optimization
- Two methods to see what's consuming RAM
- Profiler.GetRuntimeMemorySize() can find high RAM consuming objects (suggest output to debug log)
- Look in Mono's CIL (Common Intermediate Language) for the 3 alloc types - Newarr, Newobj, Box
- Manually expand the heap by pre-allocating memory space at startup (based on a history of assets usages.)
- For on the fly needs, call Resources.UnloadAsset (if asset still needs to be referenced) or use Resources.UnloadUnusedAssets (for non referenced)
- Since Unity's Auto Garbage Collection is usually only called when the heap is full or there is not a large enough freeblock, consider calling (System.GC..Collect) before and after loading a level (or put it on a timer) or otherwise cleanup at transition times.
- Avoid functions with large amounts of hidden allocs
- Avoid memory fragmentation by using structures (stored on stack) instead of classes (stored on heap).
- Remember enumerators allocate RAM. And (Foreach) is reprocessed into a block of code which allocates an enumerator, too.
- For the same reason, avoid Anonymous methods and Lambdas.
- Stream assets to consume less RAM, Collect related assets into Asset Bundles at build and then stream them out at run time. (Be sure to destroy them before the next scene or asset bundle stream).
- Don’t add strings together. Don’t manipulate strings per frame (instead add a trap so you only update if it changes value.)
- Use the StringBuilder class to make strings, Don't use an array into a function that returns a string because it will keep dumping and re-allocating each time in the loop
- Create asset pool classes (bullets/missles) to store objects instead of multiple new-ing - such as
MyClass m – poolOfMyClass //do stuff poolOfMyClass.Store(m);
CREDITS: Parts of the above material were collected from:
Immagine icona:
Area tema:
IDZone
Includere in RSS:
1