Blog

Unity V5.6.0f1-f3 Optimization Notes

There is no quick fix but constant experimentation and measurement will guarantee results!

As per Unity : ” It is vital to understand that there is no one size fits all approach to improving rendering performance. Rendering performance is affected by many factors within our game and is also highly dependent on the hardware and operating system that our game runs on. The most important thing to remember is that we solve performance problems by investigating, experimenting and rigorously profiling the results of our experiments.”

I decided to document a few of the techniques that seems to help my Unity project’s performance.

First, I use ubuntu linux  + Unity Game Engine (Personal Edition) V5.6.0f1 for my development machine. (My project version for this article was V1.11.27b)

Note: Beta 10 of Unity has a few quirks of course, one major pain is that the Editor crashes when you use Maximize on  Play, so I had to ‘Play in a Window’ or make a build as a workaround. Always save the Scene and Project before testing and make a full folder backup after each build. I typically do this weekly.

ubuntu.png

Note: I don’t have a special GPU on my HP Probook 4540s, so I have to work ‘extra hard’ to get a decent frame rate. As of this writing I am at least able to achieve a 45 FPS average and with burst of 60 FPS possible on this machine. The machine does have 16 Gb of ram. I am also discovering that frame rate is not everything…meaning if the flow of the game is smooth, even a slightly lower frame rate can be acceptable. I am constantly learning/discovering new tweaks for optimization so any suggestions comments are welcome from your own experiences on real projects. Theory is great and all, but I am more interested in what actually works.

The game I am working on is called ‘Escape from Pirate Island’. The latest version (in pre-Alpha) can be found here. https://mafuta.itch.io/escape-from-pirate-island

When playing in ‘fastest mode’ on this machine with 1280 x 720 resolution for example (from Game Setup Config), one can easily achieve up to 60+ FPS. Close all other applications when playing the game as well and experiment with best modes for your rig.

So, here is a summary of my Game Tweaks in case it might help someone else:

  1. Level Design
    1. Assets – For the most part I used Unity Store Assets and adjusted LOD levels when appropriate/available.
    2. Shadows are nice…but they come at a cost if you are not careful, so make sure you go through the scene level and do a per object evaluation (trial & error) for which objects should cast/receive shadows. In general, small unimportant objects may not have to cast a shadow nor receive one. The best is to test and check in editor how it looks/feels.
    3. Shaders can make/break a game and I am no expert here but I did notice that in general Pixel Shaders ‘cost’ more in performance but provide better looking results usually. Conversely, Vertex Shaders look a little less good but can be faster and perform better. Diffuse might be ok in certain cases but not others. Experiment, profile (in builds) and make a judgment.
    4. Texture Resolution begins in the 3d design software, but if you are like me (not an artist) and have to rely on the work of others for your models, at least you can do a per object evaluation by experimenting with texture compression For PC games I found 2048 are ok for important objects the player will see up close but you can and should be able to drop down to 1024 for smaller (less important) objects like a small stone, mug, bug etc. Saving texture memory is a vital part of optimization but again profile first and if you are not memory constrained and performance is good, don’t sweat it too much. Alternatively there is a kind of ‘bulk’ option under quality settings where one can set the texture quality to say ‘HalfRes’ and so on. This must be used with care because it will literally halve the texture quality of whatever was set on that particular object ie from 1024 –> 512 and 512 –>256 for example. This does save you the trouble of doing it per object but may make ‘important’ objects look ugly if they were already using the optimum setting.
    5. Quality Settings for each mode can also be tweaked for a balance between performance and look. For example if Hard Shadows looks almost as good as Hard & Soft Shadows or Soft Shadows, but gains decent performance, then why not use it?
    6. Test and experiment carefully with other settings under quality like anti aliasing (which is supposed to soften ‘jagged’ edges) and set to 4x or even 2x to see if looks vs performance equation is still works ok for your game. The hardest thing to accept is that there are no instant magic answers and no two games are the same. Don’t blindly ‘optimize’ without having a decent profile ‘before’ and ‘after’. I learnt the hard way that not every trick works for every situation…in fact it could make it worse, so be patient. I am falling in the hole every so often but I am determined to learn what works for each situation.
    7. Occlusion Culling can make a huge difference in performance but be careful as it can also make things worse if not setup correctly for your game world. I found that a completely ’empty’ world does not necessarily benefit from this and conversely a world with objects hiding view of others can in fact work great.
    8. Static and Dynamic Batching should be experimented with as well but don’t instantiate static objects at runtime as it can kill performance.
  2. Sound – Most Sound files are in .WAV format
    1. I have only one main theme song on an endless loop.
    2. The best method is to experiment, profile and repeat until sounds is a non-performance issue.
  3. Colliders -Most Objects use simple colliders instead of mesh colliders
    1. This means box colliders & or sphere colliders for most of them
    2. Make sure you check the right object ie myPlayer is the thing that collides when it should and consider removing colliders completely for objects not requiring them.
  4. Scripts – All scripts are in C# and I try to minimize complexity
    1. This means a lot of simple script logic with specific purpose and only the required logic to accomplish the task at hand ie pickup a food item or something to drink. The downside is code repetition to some extent but the upside is that it seems to run better and is easier to debug a specific task in a short script.
    2. I try to avoid doing ‘Get Component’ logic during the Update() loop and instead setup the reference in Awake() or Start() methods
    3. I try to avoid doing ‘for loops’ as they can be inefficient.
    4. I minimize Find Logic
    5. I rely on ‘invisible’ box type triggers a lot.
  5. Animations are kept as simple as possible during Alpha-phase
    1. This means many animals share the same idle, walk, run animations too
      (Often I use legacy generic animations without an animation controller for better performance, but this needs to be tested and considered carefully for each case)
    2. NPC’s also often have similar AI script logic, again as simple as possible.
    3. I avoid instantiation as much as possible and when used, it happens at scene start, or periods of low activity, for the most part.
  6. Lights
    1. I minimize lights. In fact the whole scene has fewer than 10 lights and only one directional light (Sun). See screen shots at end.
    2. When you do use lights look at the unity notes on what ‘cost’ more and tweak settings carefully and never under/over light the scene at the expense of looking to bad vs running super fast.
    3. I found that often, lights work ok as point lights with no shadows ie just a general area volume light. Try to add a halo for a smoky/foggy effect for example.
  7. Water – I removed Unity Water and replaced with AQUAS and carefully tweaked the settings for best performance. This means I disabled ‘Caustics’ which does not work very well anyway in my game.
  8. Terrain – I used Gaia 1.5.3 for the terrain which is 2048 x 2048 but with windzone and unity water removed. I did keep wind for grass however.  I set the parameters very conservative as per Adam Goodrich’s (developer of Gaia) excellent optimization tips here: http://www.procedural-worlds.com/gaia/tutorials/mobile-unity-scene-optimisation-with-gaia/
  9. General
    I removed all empty Start() and Update() methods from all scripts. When importing a new asset, test it in ‘Lab’ project first, make a custom package with prefabs and components you actually will use in the main project. This helps to keep backups down in file size. Builds don’t matter as Unity will only include assets actually used in project.
  10. BUILDS
    1. I build only for PC/MAC/LINUX platforms.
    2. I usually build with the LINEAR color space (as opposed to Gamma) as the blending of colors is better (scientifically more accurate) for my case but I suggest you test both linear and gamma. I also tweaked the quality settings for each mode to be a little less ‘good’ than standard.
    3. During the build, player settings, I chose ‘Pre-Bake’ Collision meshes and combined that with a Full (Manual) Light Re-baking followed by a Full (new) Occlusion Culling run (I clear the old data before every major Build).
    4. All static objects are marked ‘static’ via the Inspector as appropriate
    5. I implemented a custom Zone Manager Script that essentially cuts the world in 5 chunks, with a large Box Trigger (invisible walls) setup between them. As the player leaves zone1, hits the ‘trigger wall’ I disable all objects (except terrain) behind him, while enabling zone2 objects for example. This requires the use of a 5th zone where certain objects (required to be always active) are organized. I avoid instantiate and destroy as much as possible and use a hide/show strategy controlled by the Zone Manager logic instead.
    6. I hope others will add to this list and provide comments on these tips.

Below are some screen shots from the Unity Editor for my current setup

Escape_Fastest_Config.png
This setup lets me achieve 60-70 FPS at the moment.

QualitySettingsBV1.11.26c.png
I set the ‘Good’ Mode as my default as per above settings but found that FAST or FASTEST mode with 1280 x 720 resolution gets me to 60-70 FPS!

playersettings_V1.11.26.png
Player Settings for Windows Builds. Note that I removed Direct3D9 (DX9) support for older pc’s (built before 2007 or so). Also note I use Linear Color space. In this example, the Prebake Collison Meshes is disabled but experiment with it as the main benefit is that one can often get faster load times, higher fps but at the cost of (sometimes but not always) a slightly larger executable file.

GFX.Waitforpresent issue: Essentially ( on my machine without a GPU) I ran into this issue but after enabling ‘Prebake Collision Meshes’, the CPU was no longer too busy. My understanding is that the CPU was constrained trying to draw/render the world (without the relief of handing off to the GPU -remember I don’t have one!-) but with prebake meshes it seems to be much better. I would like to hear what others think about this?

gfx.waitforpresent.png

I also setup my Tiers as follows: Note the changes to Realtime Global Illumination CPU usage set to LOW for Tier1 and Tier2 and using Forward Rendering Path.

HW_TierSettingsChangedTo.png

Terrain Settings BEFORE tweaking!

terrain_before.png

Drop down the Detail Distance, Detail Density and Tree Density and start Billboard as soon as is acceptable for your situation. Try to find the balance between looks and performance.

When using the profiler, do NOT use it in Editor (not reliable/accurate). Always run profile against a build for better results and disable all other programs.

REFERENCES & related Links by others:

By CRISTIANO F. (Intel), steve-hughes (Intel)

https://software.intel.com/en-us/android/articles/unity-optimization-guide-for-x86-android-part-1
and
https://software.intel.com/en-us/android/articles/unity-optimization-guide-for-x86-android-part-2

By Adam Goodrich from Gaia
http://www.procedural-worlds.com/gaia/tutorials/mobile-unity-scene-optimisation-with-gaia/

Unity Game Engine documentation
https://unity3d.com/unity/beta/unity5.6.0b10

https://docs.unity3d.com/560/Documentation/Manual/Graphics.html

https://unity3d.com/learn/tutorials/temas/performance-optimization/optimizing-graphics-rendering-unity-games

By the Sniper Fan (Great Tips)
https://forum.unity3d.com/threads/general-performance-optimization-tips-for-unity.386338/

Escape from Pirate Island – Devlog V1.11.29

Warning: We are doing testing with new Unity Beta features, so things may or may not look/work as expected until we get further down the road. We are in pre-Alpha phase of development and suggest that players use the GOOD or FAST modes depending on capabilities of hardware during this period.

Brave testers can get your free Game here:

https://mafuta.itch.io/escape-from-pirate-island

or

http://gamejolt.com/games/escape-from-pirate-island-wip/232098

Specifically, we are testing the new lighting options so please be patient until it stabilizes.

In this build we changed the lighting mode from ‘Subtractive’ to ‘Shadow Mask’ which is supposed to give Near objects realtime light and Far objects baked light depending on the shadow distance. This will (ultimately) result in fewer draw calls but we have of course not fully tested/implement this yet.

Also the light map size is now 2048 instead of 1024. Shadow angle is set to 45 deg and under the direct light settings, we have changed the strength for shadows from 0.621 to 1

Underwater Caustics is currently disabled due to incompatibility issues with beta Unity and we may not use it if too heavy on CPU/GPU.

The general game play in this version has not changed, so unless you want to see the light experiments with soft shadows, not much is new in this version.

We plan to upgrade/debug and test the Game Engine as fast as possible during the next month or so. This means the game is not optimized for FPS at the moment.

Version 1.11.21 will also have a TimeofDay cycle that dynamically changes the movement of the Sun (above the fog) to create the illusion of early morning, daytime, late afternoon ambient light. The cycle repeats every 16 minutes of actual time.

Version 1.11.222 Will feature more advanced Player Vitals including Hunger, Thirst, Stamina Bars as well as Body Temperature. All these will affect the player’s overall Health depending on what the player does. We have also added a loading…progress bar to inform player of loading time of Level01.

V1.11.26 is experimental and uses Linear color space (we are testing & comparing against Gamma). The result is a more somber mood in Level01, with better (more accurate) color blends. Note: We had to drop support for old Windows PC’s (mostly prior to 2007) using DX9 API. So you ideally should have a PC that is less than3 years old with a modern GPU (not required but reccommended) capable of DX11 support for best results. Test and let us know any issues you find but please remember this is alpha build so it is suggested to pick the ‘GOOD’ mode or ‘FAST’ mode as the game is not being optimized fully yet. PC should have at least 8gb Ram as well.

V1.11.26d uses Pre-baked collision meshes, which works better for most machines without a GPU and or older hardware when played on the Native Screen in Good/Fast/Fastest Mode.

Example Test Machine: HP Probook 4540s running ubuntu 16.04 LTS with an Intel Core i5-3230M CPU @ 2.60GHz x 4 with Intel Ivybride Mobile Graphics. When playing in fastest mode on this machine with 1280 x 720 resolution (from Game Setup Config), one can easily achieve up to 60 FPS. Close all other applications when playing the game as well and experiment with best modes for your rig.

Escape_Fastest_Config.png

Note: Playing on a laptop screen will usually be faster than playing on an externally attached monitor with HDMI cable but it depends on the configuration, so Start with ‘Good’ Mode on Native Monitor to find what works best for your system.

V1.11.27b

  • Fixed ‘instant die’ bug
  • optimized underwater shadows
  • optimized framerate for amberjack fish schools
  • FPS in Good Mode with 1280 x 720 resolution,  now 45-50 FPS
  • FPS in Fastest Mode with 1280 x 720 resolution, now 50-60 FPS
    • Note: If your machine does not have a GPU and at least 8Gb Ram you should lower the resolution as per above for best results.

Trailer Video (actual pre-alpha game video) here:

https://www.youtube.com/channel/UCbcF2CHeHJXN3P3k3CWTGfA

NOTE: Check back often, as we try to publish at least one update every week.HW Requirements: A 64-bit Windows/Mac/Linux OS with 8GB Ram and optionally a CPU + GPU.

V1.11.29

  • Upgraded game engine to Unity 5.6.0f3
    • In ‘Simple mode’ with 1280 x 800 resolution framerate is 40-60FPS tested on an ubuntu laptop with 16gm Ram on built-in graphics.
  • Added new AI for Deer
    • Certain deer are aware of the player and will be passive/flee/attack depending on the behavior of the player and situation.

Escape from Pirate Island – Devlog V1.11.18

  • Download here: https://mafuta.itch.io/escape-from-pirate-island
  • Experimenting with less natural sun light to make scene less bright
  • Enabled Ambient Occlusion for darker look
  • Moved Amber jack schools closer to shore and wider random size range of fish
  • Water also a bit darker with different reflections (example ship’s black sails)
  • Provide a 32-bit & 64-bit Windows Version , but only 64-bit Mac and Linux versions.

Escape from Pirate Island – Devlog V1.11.17

  • Get your copy here: https://mafuta.itch.io/escape-from-pirate-island
  • Upgraded to new game engine V5.6.0b8 (beta) which still has a few issues one them breaking the underwater ‘Caustics’ effects, so disabled that for now. (This means for now that the camera for underwater effects is set to perspective instead of orthographic. We will reverse this once caustics works again.)
    • also enabled lightmap static for testing and do manual light-map baking
    • set fog density underwater to 0.06 (was 0.04), so a bit denser
  • Changed the directional light from real time to mixed to test if it helps performance
  • Rescaled the water plane and decreased underwater visibility to intentionally confuse player and let him/her become disoriented easier if player did not pay attention to bearing on compass to find reciprocal path out.
  • Drowing is easier
  • Shadows are also (temporarily ) disabled
  • Any 3d artists out there wanting to contribute a few plants/rocks for above/ below water? Better quality cabins/props perhaps? Even a new animal like a seal, beaver, or whatever very welcome but should include basic animations and be .fbx (Unity game engine) compatible with z-axis forward and ideally not use legacy animations.

Escape from Pirate Island – Devlog V1.11.16

  • new above water effects and enhanced underwater view looking up to see trees/objects
    • beautiful dark blue for deeper water
    • refractions and reflection improvements
    • better fps above & below water
    • set caustics max depth to 45 (was 40)
  • Player now runs/walks faster
  • added two groups of AI crows with free roaming behaviour and semi controlled behaviour
  • Rebaked lighting and occlusion culling for some optimization
  • blur effect (of fog lens) now set from 0.5 –>0.3 to make it a bit less blurry
  • player camera far plane is now 1200
  • moved amberjack fish schools closer to shore and slightly more underwter
  • added royal sailing ship with AI
  • rebaked lighting and occlusion culling for new objects
  • adjusted noise & grain underwater (black boost .33, white .33)
  • replaced mesh collider on stool at camp with simple box collider
  • enlarge rear wall collider on shed at docks (box)
  • skeleton in the reeds
  • added more amberjack fish schools
  • bloom special effects to create ‘volumetric’ fog effect with slight blur
  • new underwater effects including invisible game boundaries

Download Escape from Pirate Island

Suggestion: While the game is not yet optimized we suggest players without graphics cards and 12gb Ram use the ‘Good’ graphics mode or lower while test playing.

EscapeGoodMode.png

Escape from Pirate Island – Devlog V1.11.11

  • Fix issue with help text back button (not large enough on certain displays)
  • Fix issue with pear pickups
  • Rescale Devilspeak fires
  • Fix shader issue for swimming walrus moustache
  • Reposition cargo on Jetty to touch surface
  • Add colliders to campsite as needed
  • Resize water bucket collider at pirate bay dock
  • Add some waterplants in various locations
  • Add a Cow with base animation (eat)
  • Add some ducks on island
  • Make it easier to pickup beet
  • Replace Mesh colliders on Shed with 3 Box Colliders

Escape from Pirate Island – Devlog V1.11.10

Changes for this version:

  • Work in progress on camera clipping (huts/towers/tent/barrel)
    • changed mesh for room to convex
    • hut clipping stairs
    • add capsule colliders to pear trees
    • add colliders to tent and other objects as needed
  • changed settings for minimap camera (FOV=60, Near Clip=10)–>redundant/disabled
    • Decided to remove the mimimap camera and replace with compass
    • main camera is now near=0.2 far=900
    • testing and may readjust as needed but should fix most tree clipping
    • some game objects do not yet have optimized colliders – work in progress
  • Added ability to hide/show the mouse pointer cursor (Escape key toggle on/off)
    • This effectively also locks the cursor the center of game window/monitor during play, but on death of player unlocks it to allow player to quit/restart game again.
    • added bad/good mushrooms (more will follow)
  • working on collider on fisher house and replace with box such that player can still get onto porch or make convex
  • working to add capsule colliders to pear trees
  • working on texture turtle eggs (replace temp spheres) , cast shadow
  • 3dArtists – We need:
    • otter, beaver, raccoon, egret, heron, pelican, manta ray, large sea animals, underwater plants, a nicer pear tree, a nicer apple tree.
    • decorative props for huts and world look around and suggest something
    • torches, old lamps, candles, flintstone/lighter,axe pickup->stew
  • enabled blur with downsample  of 1 (but 0 works too)
    • (improves docks/boats/grass/sand/underwater visuals etc.)
  • added a new cat (interactions coming in future)
  • disabled mimimapimage camera (replaced with compass) –>may return
  • add gold coins & gold bars
  • rebaked lightmaps
  • removed the debug FPS counter and timer in build