DEEP DIVE – Combining meshes

In quick tip https://unitytips.wordpress.com/2014/05/28/quick-tip-combining-meshes/ we provided a script for combining meshes and a simple example on how to use it.

To remind us about the script, we have it’s editor menu here:
MeshCombinator_add

We can see that the “Combine When Launched” option is selected. This is the default setting. If we select this option, the script will run everytime at the beginning of our game and automatically combine the meshes. We can however do this manually as well by clicking the “Combine” button. Once we do that, we get a menu with only one button “Uncobime”. This serves us to undo our action if we decide that we want to add more objects to the group later on and want to launch the script again.

Uncombine

Let’s take a look at how the script actually works and what lies beneath the surface.

The script first takes all the MeshFilter components from all the child objects of our group by calling:

this.transform.GetComponentsInChildren<MeshFilter>();

Then it starts to create new groups of objects. These are based on materials, which the original meshes use. Therefore it creates as many groups as there are different materials used within our group of objects. When it’s done sorting the meshes into newly created groups, it starts to combine them. First the script saves all the MeshFilters used for combining within its private field. These will be later used for undoing the combining process. Afterwards, it calls the CombineMeshes() function and passes it, one by one, the various groups of meshes as a parameter. This function creates our new, combined meshes. When our script is done combining, it disables all the original MeshFilters. The reason for doing this is so that only the new meshes get drawn and not the old ones (that would not save any draw calls, so it makes sense). Once the meshes are combined, the script saves the newly combined meshes within another private field, which is again used when undoing its action.

Why did we chose this approach? There are different ways of dealing with combining meshes that can be found online. Some of them do not take multiple textures into account, some of them try to create texture atlases and reduce the amount of draw calls even further. We wanted an easy-to-use tool that works as expected and is easily understandable. Another thing to mention is dealing with the original meshes. We have encountered scripts that destroy the original objects once they are done combining. However, this does not seem like a good idea. The objects can contain other valuable components than only the MeshFilter, such as colliders, audio sources and others. These don’t have to depend on the geometry of the mesh itself, but rather on the position of the player (i. e. a noise plays when a player comes near to an object). We wanted to keep these, so we decided to just disable the MeshFilter and leave the rest of the object as is. Another reason is that we want to be able to undo the combining. Obviously, this wouldn’t be possible if we destroyed the objects.

So here you can see how the script actually works and that it’s quite easy to understand it’s purpose. If you have any other questions about mesh combining, don’t hesitate to ask.

QUICK TIP – Combining meshes

MixedGroup_showOff

Unity’s mesh class contains a function called CombineMeshes(). You can see the full documentation of the class at: https://docs.unity3d.com/Documentation/ScriptReference/Mesh.CombineMeshes.html

The function basically takes an array of meshes passed as parameter and creates a new mesh, with all the triangles from original meshes.

I created a script which uses the CombineMeshes() function and provides an easy-to-use solution for combining meshes.
You can download the package with my script over here: http://1drv.ms/1oDFPDQ

Now let’s see how to use it:

1.) Create an empty game object – let’s call it “Mixed Group”.

2.) Put all the objects that you intend to combine as children of the “Mixed Group”.

MixedGroup
3.) Add the script to the “Mixed Group”.

MeshCombinator_add
4.) Enjoy!

 

The objects within the scene contained 4 different textures. Therefore they got combined into 4 meshes in total. Before calling the script, this view needed 81 draw calls to get done. After combining it took 35 draw calls to draw it.

Let’s take another look at our scene and see what happened:

MixedGroup_combinedType3

MixedGroup_combinedType1

MixedGroup_combinedType2

MixedGroup_combinedType4

As we could see, our script managed to save us a lot of draw calls using just a simple technique of combining meshes.

We will explore more details about mesh combining in our DEEP DIVE into this topic.

QUICK TIP – Debugging with Windows RT

Modern UI, Windows RT, Metro…we can call it whatever we want. This is the new platform by Microsoft available for touch-enabled devices like tablets but also for keyboard & mouse devices.

Image

 

With Unity we are covered even for this new platform. In order to be able to run and debug our application on Windows RT we need to have the following on top of the Unity installation:

  • Windows 8.0 or higher

We also need a developer account to be able to unlock our phone. We can achieve this for free by signing up for an App studio account at http://appstudio.windowsphone.com/.

Image

 

The debugging is very similar to the Windows Phone. We again need to build our game in Unity and open the produced solution in Visual Studio. Then we add the Assembly-Csharp project and we are ready to debug.

Image

We can launch the application on the local machine, in the simulator or on a remote device.

Image

In terms of logging, it is easier to access the log file then the process used on Windows Phone. We can find the log file produced by the game in “C:\Users\<USER>\AppData\Local\Packages\<PKGID>\TempState\UnityPlayer.log”. Unline on Windows Phone, the file is not locked for reading while the game is running so we can open it and see the logging information on the go.

QUICK TIP – Debugging on Windows Phone

The first of the mobile platforms that I want to discuss is Windows Phone. Support for Windows Phone 8 is one of the latest additions to Unity. It isn’t fully supported as we’ll see in some of the tutorials but in terms of building and debugging it is one of the best platforms. In order to be able to run and debug our application on Windows Phone we need to have the following on top of the Unity installation:

  • Windows Phone SDK 8.0 (or higher)
  • Windows 8.0 (or higher)

We also need a developer account to be able to unlock our phone. We can achieve this for free by signing up for an App studio account at http://appstudio.windowsphone.com/. Once we register we are allowed to unlock 1 Windows Phone device and load up to 2 apps onto it.

AppStudio

To unlock a phone we need to connect it with the USB cable, run the “Windows Phone Developer Registration” program (it’s part of the Windows Phone SDK) and register the phone with our developer account.

DeveloperUnlock

As soon as the phone is developer unlocked we can run a game straight from Unity just by changing the build settings to Windows Phone.

BuildUnity

However if we want to to perform remote debugging on a device we need Visual Studio. The free version called “Visual Studio Express for Windows Phone” can be used for this. Once installed we need to first build the app from within Unity using the Windows Phone build settings. This creates a Visual Studio solution with our built app. This can be opened in Visual Studio. Before running the app we need to add the Assembly-CSharp project located in the root folder of our Unity project.

VSAddProject

Then we set the newly added project’s build platform to ARM.

VSSettingsProperties

VSSettingsARM

Now we are all set to run our game on a Windows Phone device and debug it using Visual Studio. We just need to press the Run button and Visual Studio takes care of deploying our game to the phone and attaching the debugger. Then we can use breakpoints, watches, modify the variables on the run and all the neat features of debugging with Visual Studio.

VSRunning

As you can see debugging on Windows Phone needs some setting up but once we are done with the initial setup it is easy to debug games using Visual Studio. The logs on the other hand are quite difficult to use on Windows Phone. It’s impossible to see logs while the game is running. They are accessible only via Windows Phone Power Tools, a program that needs to be installed separately. Once installed it can connect to a device and see all the developer apps and their isolated storages. There we can find our Unity log file and open it using any text editor straight from the phone. But it is locked while the app is running so we can only open the logs and see what was going on retrospectively.

AccessLogFile

GENERAL – Welcome

Welcome fellow Unity developers!

This 1st entry is here to quickly explain my intentions with this blog.

Lets first take a look at who this blog is aimed at. If you are an intermediate Unity developer and you consider developing for mobile platforms you came to the right place.

If you are a novice in Unity you can take a look around, I’m not however going to cover the very basics of Unity development. So if you are struggling with with controlling the Unity environment or choosing the right language for you try to take a look at the official Unity tutorials (http://docs.unity3d.com/Documentation/Manual/UnityBasics.html) and come back afterwards to learn something more.

Moreover if you are an expert in Unity but have been focused on a single platform you might want to check out my tips as well as I’ll try to help you transition to other platforms supported by Unity.

This brings me to the contents of this blog. Unity is nowadays a highly multiplatform engine. However when I wanted to take advantage of this I found out that it’s not all fun & games when you try to look for examples involving development for various platforms simultaneously. That’s why I decided to put my discoveries onto this blog and let you see what challenges you will most likely face when you get into multiplatform development and how to solve them.

The articles will be divided into 3 categories:

  • GENERAL – topics that cover something not directly related to tips (like this article)
  • QUICK TIP – how to get something done quickly
  • DEEP DIVE – explanation of a topic providing more details

If you have any questions involving some of my tutorials or things that you can see in my screenshots and don’t fully understand, don’t hesitate to ask for help. I’ll either answer you directly or if you strike a topic that seems worth it I’ll add another article to provide you all with more details.

I guess this is enough for introduction so let’s get on with some actual tips!