Adding Transform.LookAt2D like it belongs to the Unity API

March 31 | 15

Unfortunately there is no Transform.LookAt2D in Unity as for now in Unity5. BUT with an extension method this can be added into the API quick and easy!.

Its pretty simple. We just need to have a namespace included wherever we want to use Transform.LookAt2D and thats pretty much!, here’s how:

Lets create the extension method for LookAt2D:

And then we just need to call it from any of our scripts as part of the Transform methods! here’s a quick example:

Quick, easy and clean!

 

 

 

 

Getting a random enum value and also be able to exclude some values

March 5 | 15

As the post says, in the game I am working on I needed to get a random value of an enumeration but on some cases I needed to exclude some values of the enumeration, so I came up with this small snippet of code that given an enum type and an array of enum excluded values it will return you a random value of the enumeration.

It works with any type of enum you create :D

If you have a better way to do it _please_ let me know :).

Quick example on how to use it:

 

Mixing GUILayout and GUI and making them coexist together (Useful when editor scripting)

January 20 | 15

Warning:

Even tho Unity 4.6 has come out with the new Unity GUI, you still need sometimes to use the old legacy GUI and GUILayout API specially if you are working with editor scripting.

Being said that, DONT use GUI/GUILayout functions for GUI on your projects (except editor scripting) if you are using Unity 4.6+; using GUI/GUILayout  in your actual game is slow and consumes lots of draw calls!.

 

So now that everything is clear here’s a little bit o what happened:

I am programming a new tool for the asset store and I was needing to display some textures in the inspector view (after all the GUI was written using GUILayout…), but Unfortunately “EditorGUI.DrawPreviewTexture()” doesnt have a layouted version :/ and I didnt wanted to do the whole same thing again using plain GUI. so I came up with an interesting idea that I wanted to share on making GUI and GUILayout coexist in the same GUI :).

So I’m going to expose having GUILayout code and inserting GUI code. and making them both coexist in the same Repaint().

 

Mixing  GUILayout with GUI

this happens when we have lots of GUILayout calls same as when we are building our window with pure GUILayout calls and there are some functions that are available only for GUI.

In my case I was wanting to create a tool for displaying the materials’ textures of any given object and some small info:

So the tool is organized and also looks like this:

ProMaterialCombiner

 

 

 

 

 

 

 

 

 

 

 

and it even maintains its proportions! :D

Resizeable

 

 

 

 

 

 

 

 

 

 

 

So how do I do it?, its actually pretty simple, basically what I create is an area (with GUILayout) and inside the area I create a ScrollView (also with GUILayout) and then in the inside we put all our GUI (relative to the Area we created and not to the window) and we fill everything with GUILayout.Spaces(px)!

 

Sounds complex? its not at all!, trust me :),  let me show you a really simple code:

Basically what we need to do is to create an empty area from the GUILayout space and fill it with “spaces” that are consistent with what we draw on the GUI space. Let me draw it for you:

CombinedGUILayoutAndGUI

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Mixing GUI with GUILayout.

Mixing GUI with GUILayout is basically the same process you just have to take into account the space consumed by the GUILayout and then just offset the other GUI objects so they can coexist.

I’ll leave this part to the reader tho.

 

Like this post?, feel free to subscribe to my posts, I’ll keep you posted on anything that I write :).

Any comments/suggestions/feedback let me know :).

 

 

Reducing Draw Calls (also named SetPass calls) on Unity5.

January 10 | 15

So I have been working on the standard shader support for Unity 5 and doing some tests regarding how feasible is to reduce draw calls with the new standard shader and actually the results where pretty positive!.

Lets talk about the standard shader a little bit. The standard shader is a physically based shader that is consistent across diverse lighting conditions, its multi-platform and texture slots enable different shader features.

Here’s a small introductory video on what can be achieved with this one new shader.

 

 

This standard shader “mutates” and becomes more faster/slower depending on the textures you are using on the texture slots. being said that these two shaders are different (even if they are using the standard shader):

StandardShaders

So these two shaders (even if they are “Standard”); they are totally different shaders when rendering a scene, hence they have to be handled totally different if we want to reduce some draw calls (Now called SetPass calls).

Before Talking about how to reduce draw calls in Unity 5 I will talk about how draw calls are reduced regardless the shader is being used and then I will talk about how to reduce draw calls with the Standard shader in Unity5.

 

Reducing Draw Calls in Unity The Manual Way.

So, if we want to reduce draw calls in Unity or in any other engine the way to do it is using as less materials as possible.

Basically it all gets reduced to these single steps:

 

  1. Sort all the materials and gather them by the type of shader they are using.
  2. With the materials that share the same shader gather their textures in a bigger texture (atlas texture).
  3. Create one material that will contain the shader and the atlas texture(s) created on step 2.
  4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
  5. Assign to your remapped meshes the material we created on step 3.

 

 1. Sort all the materials and gather them by the type of shader.

I’m going to use for easiness of explanation purposes one simple scene that will have all the meshes use the same type of materials but different textures.

In our case we are going to use this scene (which contains 4 materials each of them with a Bumped Diffuse shader):

0SampleScene4Materials

 

 

 

 

 

 

 

 

 

 

 

Notice the SetPass calls: 4

So we have  for this scene these materials to process:

SceneMaterials

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2. With the materials that share the same shader gather their textures in a bigger texture (Atlas texture).

In our case we have 2 textures per shader as the Bumped Diffuse shader uses a normal map and a base texture, so what we need to do is to have 2 textures like this:

AtlassedTextures

 

 

 

 

 

 

 

 

Save these textures in the project view, we are going to need them later on:

ProjectView

 

 

 

 

 

 

3. Create one material that will contain the shader and the atlas texture(s).

Create a material and assign (on this case) the two textures to the material with the shader used (in our case “Bumped Diffuse”):

3Material

 

 

 

 

 

 

 

 

4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.

This modification can be done on any 3D editor (Maya/ 3D studio / Max / Blender / etc) of your choice.

This is the most boring/demanding step as it involves you modifying your UV coords of your meshes and make them match the  texture’s atlas pixels (the texture we created on step 2).

So what we do is that to each of our UV coords located all around [0,0 – 1,1] we Re-position them on a smaller sub-set of [0,0-1,1] that matches the pixels on the atlas texture we created on step 2.

4UVREMAP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And… we do this for each of our meshes that share the same shader.

 

 

 5. Assign to the remapped meshes the material created on step 3.

We are almost there! still we have a couple of more steps to do:

 

  1. Select all your UV-remapped-meshes and place them where the old meshes where located and deactivate the old meshes.
  2. Assign the created material on step 3 to all your UV-remapped-meshes.

5ObjectsSameMAt

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

And by here, we are finished!, now lets hit play:

5OptimizedScene

 

 

 

 

 

 

 

 

 

 

 

 

Before Optimizing if you go back to Step 1, we had 4 SetPass calls and after all the optimization has been done we end up with 1 SetPass calls!. which is amazing! its 75% draw call reduce on this case.

 

Reducing Draw Calls in Unity 5 with the Standard shader.

By here you should know more or less how the optimization process is done with the standard shader. as I wrote on the beginning,  the standard shader internally “mutates” and becomes more faster/slower depending on the textures you are using on the shader’s texture slots (and also renders differently depending on the textures that are used). being said that we have to gather our object’s materials if they are using the Standard shader by the textures used. i.e: Gather all the meshes that only have colors; Gather all the meshes that only have an Albedo texture.. and so on.

After gathering our objects by the textures used we just need to follow the same steps we use for any other shader:

 

  1. Sort all the materials and gather them by the type of shader they are using (In the standard shader case gather them by the textures used).
  2. With the materials that share the same “shader” gather their textures in a bigger texture (atlas texture).
  3. Create one material that will contain the shader and the atlas texture(s) created on step 2.
  4. Remap all the UVs of your meshes that use the shader to fit the coordinates of the atlas texture.
  5. Assign to your remapped meshes the material we created on step 3.

Here’s a small scene I created for testing purposes:

StandardShaderScene

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

If you compare the original draw calls (91) and the optimized scene’s draw calls (22) you can see that there’s ~75% draw call decrease!. Which is amazing!

Also, if you can try to combine meshes as much as possible, this also helps a lot!.

Reducing Draw Calls on Unity Automatically

Knowing that this is a really cumbersome process I decided to create a tool that automatically gathers your objects and sort them by the type of shaders they use and automagically “Bakes” all your unoptimized scenes!, you can find the package here, and if your scene only uses Diffuse and Bumped Diffuse shaders please be my guest and use it for free!

 

 

 

Like this post?, feel free to subscribe to my posts, I’ll keep you posted on anything that I write :).

Any comments/suggestions/feedback let me know :).

 

Light, easy and fast Field of View 2D

September 11 | 14

So I was working on a personal project that requires some AI to have a field of view to check if the player has been seen or not by the AI.

I searched on the asset store and to my surprise there is nothing like this implemented nor in the internet, so I decided to quickly implement a fast, light and small version of a small field of view for 2D.

My implementation doesn’t use physics at all, without further ado, here’s a quick explanation of the component; to use it just drop it on a GameObject and it will run automagically.

Inspector vars
Radius: Radius of the field of view, how far it can “see”.
FOV (Field of view): Angle in degrees how wide the field of view will be, by default 90 degrees
Direction: Defines the direction where the field of view will be pointing at, this vector internally gets normalized.
Test Point: This is a transform reference to test the field of view when hitting play.

And this is how the field of view will be seen in the scene view; the green line shows the direction and the yellow lines determine the FOV edges.

FOV Scene view

 

Finally the source code can be found here, feel free to use it in your commercial projects and also include it in your Asset store packages if needed, still you don’t have permission to sell this script alone in the Asset store.

;).