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

//Given an enum type returns a random value of the enum type.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

//this is done in case someone introduces all the enum as excluded values in the parameters

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:

and it even maintains its proportions!

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:

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!.

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):

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:

Sort all the materials and gather them by the type of shader they are using.

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

Create one material that will contain the shader and the atlas texture(s) created on step 2.

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

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):

Notice the SetPass calls: 4

So we have for this scene these materials to process:

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:

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

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”):

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

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.

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:

Select all your UV-remapped-meshes and place them where the old meshes where located and deactivate the old meshes.

Assign the created material on step 3 to all your UV-remapped-meshes.

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

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:

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).

With the materials that share the same “shader” gather their textures in a bigger texture (atlas texture).

Create one material that will contain the shader and the atlas texture(s) created on step 2.

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

Assign to your remapped meshes the material we created on step 3.

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

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 :).

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.

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 degreesDirection: 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.

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.