Optimizing Game in Unity 3D

by


There are so many ways to go to Rome, or another day dream city of your mind. But right now we are not talking about that travelling dream because it's still evening. So you are a game developer, or just person who will make a game and want to start developing with your dream team. Above is my Orbiz cinematic explosion view look and below is some tips about how we can optimize game on Unity 3D as our lovely editor. Here's the do and don't.

1. The Forbidden Lines
This is the don't. It's happen when the programmer use the try and catch formula for exception handler like paramore song and that will be consuming a lot memory. For example, you write like this on the Update method.

 void Update (){  
   try{  
      //problem  
   } catch {  
     //solution  
   }  
 }  
Or this is mine when working AI about two years ago.


Foto kiriman Hendrianto Kusuma (@hendrikk1) pada


Usually, they use it for solving a null object. But you can handle it by using if and else like this. 

 void Update (){  
   if(thatObject == null){  
   }  
   else {  
     //do the action when the object not null  
   }  
 }  
So let it be empty after if. You can see the memory consumption if we use try and catch in the profiler. And the second forbidden lines are using FindGameObject, GetComponent thing on Update method like below.

 void Update (){  
   enemy = FindObjectOfType<EnemyScript>();
 }  
But you may add it on your Awake or Start method because is like starting a dream is very hard and heavy, but after that you can enjoy the process.

2. Go Check The Profiler
This is the do, and the third and the fourth will be do also. Profiler is very useful for indiedev like us because Unity make it available unless we use Unity Free License (It's a Pro Version feature before). To look how beautiful is, or just activate it just click Window and select Profiler.




Above is an empty Profiler, you must hit the Unity Play button for testing your game like usual and Profiler will work like his promises before.




And yes, image above is when you test your game and look the Profiler, I always check the Time ms table, It's like the memory process, the higher, the more heavier. Just read the wise word "Select Line for per-object breakdown". You can expand for the detail like the top big Time ms on that table above is Physics.ProcessReports. Then, you can check if you bring the Try and catch exception to your dream game and also FindGameObject or GetComponent thing, let's see if we put that lines on the script on the Update method.

3. The Magic of Occlusion Culling
Once again, Unity has doing a great thing for indiedev like a big charity. Yes, they bring the occlusion culling on the free version. He can hide the unwatched object from the Main Camera (the camera object default name) and showing the objects in front of camera.


That's a picture about "I don't activate the occlusion culling yet". And the below is when I activate it. The circle red is a camera object. You'll see the different, the yellow wire box and the mystery of hidden objects. Hilarious.


So you are very interested, I guess. To activate it, just group all of the objects which only used for your environment aesthetic look or the objects which shouldn't be appeared while the camera isn't facing them, whatever.

  
The red circle above is my parent object for grouping the childs. The childs are all of the object except players and enemies, we do love the childs. And the next is check the static, you can see it on the inspector coloumn. I use Orbiz Apocalypse object as parent and I should check the static from Orbiz Apocalypse inspector coloumn.




After all that journey and this is gonna be the last advice for occlusion culling. Go check the occlusion coloumn or you can find it from Window menu bar and select Occlusion Culling. Let say you still select the Orbiz Apocalypse object, the parent of all childs and see Occlusion Culling coloumn. Jump to the Object then hit Bake. If there is a pop up window asking about the static object thing, just hit yes and hell yeah, just wait the bake process. 


You can now own the future. You can test it by select your camera and look at the Scene view. 

4. Crunched It All
This is a new fearture of the new version on Unity 5. Used for reducing memory size. If you want pixel art texture for your 3D model like Orbiz did, use it to reduce the texture memory size. Orbiz is using pixel art theme, 3D but pixel art. We did the pixel art look for the texture and it's ok to reduce the size of the texture lower. Just select the texture on your own texture's folder from Project coloumn, look at the inspector and change the texture format from Compresed to Crunched and Apply. Just simple like you eating sandwich.

This is the end of the paragraph. You may say it is very easy to make a game, there are a tons of tutorial and engine and be aware of indiepocalypse.