Tag Archives: Unity 3D

Unity 3D – Shaders scripts – part 001.

This is the first tutorial in a series of shaders tutorials in Unity 3D.
Open your Unity 3D project and using right click mouse add a Unlit shader to your project, see the image:

Rename your shader file and open this shader with right click mouse.
You will see the content of the default shader into your Visual Studio IDE.
Let’s see my shader example named NewUnlitShader :

When you write your first shader then you need to know some rules:

  • take a look at this link to see the most common variables and HLSL type and rules;
  • example: vec2 types with float2;
  • example: mat2 with float2x2;
  • example: vec3(1)  is float3(1,1,1);
  • example: mainImage(out vec4 fragColor, in vec2 fragCoord) is float4 mainImage(float2 fragCoord : SV_POSITION) : SV_Target;
  • example:
  • Shader “Unlit/NewUnlitShader” – the shader will add into your material from : Unlit – NewUnlitShader;
  • you can change the name NewUnlitShader , but you need to change the name of the file shader;
  • the body of shader start with word CGPROGRAM and end with word ENDCG;
  • Shader Level of Detail (LOD) is a number (see doc);
  • the Properties word is used for: inputs like a texture (see and _MainTex (“Texture”, 2D) = “white” {} );
  • the SubShader word is used to start define your settings for shader and the content of shader;
  • the words like: #pragma, #include , struct  are reserved words and is used to set and make the body of shader;
  • any shader come with : vertex shader body and fragment shader body;

One simple example of settings for vertex and fragment body shader but also you can have pixel shader body:

The next step is to add this to your shader:

Now, about vertex shader this can manipulate the attributes of vertices.
The fragment shader is the same as pixel shader.
The fragment/pixel shader is part of the rasterization steps.
You can see in my first example I used fragment shader not pixel shader:  #pragma fragment frag
The result is this function :

This is first part of this tutorials about shaders and you will need to learn all of this and make connections with my examples.

Posted in All, Programming, Unity 3D. Tagged with , , , , .

Unity 3D – C# script – save and load file.

This is a simple C# script example for a C# class Player.
The class Player come with this content into Player.cs C# script:

To make this C# script for saving the Player into one file I used this C# script named SaveLoadManager.cs:

The script come with two parts for save and load files.
I used Serializable property to save binary file named player.sav.
This script used also the Application.persistentDataPath to take the path of application.
I used a Debug log output but if you want to used into real application then is no need to use.
You can deal with any player settings and make new like: player.magic or player.status.
Take a look at the comments in the script and that script highlights design issues.

Posted in 3D, All, C#, Game engine, Programming, Unity 3D. Tagged with , , , , , , .

Programming with C# and Unity 3D game engine – part 003 .

In this tutorials I will show how to deal with lists and enumerations.
First you need to make one C# script under Unity 3D. If don’t know how to do it then read this tutorial.
The list is an object which holds variables in a specific order.
The enumeration is an object with one distinct type that consists of a set of named constants called the enumerator list.
Let’s start with lists.
You need to put this on your script:

Now you can create list under your class , see the example with one class named YourClass:

We can see the List’s syntax is at first confusing. If you take a look to types – int, sting and GameObject and variables – ages, names and objects then the base remain this: List<...> … = new List<...>(); where types are into <> and variables can be created with = new … ().
Now will add one value to names with:

To reverse one list in-place then use this:

If you want then can remove one:

We can use an array to initialize a List named animals:

Loop into the list animals and print the list:

To get the number of elements from animals list, use this:

To clean the list just use this:

The next step is enumerations and is define by the enum type.
An enum type internally contains an enumerator list like this example:

In this enum named animals, cat will be zero (unless given another value explicitly), dog will be one, and fox will be two …
To print out the enum values, you can call ToString on the enum variable in a program.
The Console.WriteLine can automatically call the ToString method.
Using GetValues method with foreach to returns an array that contains a value for each member of the enumType enumeration.

Sometimes we have a string value that we want to convert to an equivalent enum and we can use Parse().
Also you can used many enum like: Enum.Parse, Enum.TryParse, Enum.Format, Enum.IsDefined…
You can get values from enum , see the example:

Convert enums to list:

Posted in C#, Game engine, Unity 3D, Visual Studio, Windows 10. Tagged with , , , , , , .

Unity 3D with Visual Studio 2015 IDE.

Most of the time I used Mono IDE for Unity 3D.
Today I show you how to use Unity 3D with Visual Studio 2015.
You need to know this solution it’s a little slow but can solve another issues for development.
Some settings we make from start to the end will come with restart of both IDE.
Let’s start with the first step:
First install Visual Studio 2015 from here.
You need to know about the the Visual Studio : it’s on Windows OS only.
Now download Visual Studio Tools for Unity from Unitytools Visual Studio and install this download.
If your instalation will come with Repair just repair it.
This repair option it’s show you when the Visual Studio it’s install with Unity 3D package.
Open your Unity software and make this settings from Unity Preferences ( first menu of Unity 3D software).
Duble left click to run the Visual Studio Tools for Unity from folder where you put the download.
Select all and then accept this settings.
You have one new folder add Assets into your project. You need to make more settings because will not working yet.
Make one new C# script and named it. I used the name : test. Start to develop your script by open this script.
When you start your development of this script the Visual Studio 2015 start also.
Now use right click mouse button to add Existing Item… or use the short key.
Add the DLL files from your Unity 3D project ( Assets -> UnityVS folders).
Try to build your solution from Visual Studio. Also check the settings of thise two DLL from Unity 3D by left click and check your options. If you got errors then restart both IDE: Visual Studio 2015 and Unity 3D.
This will fix all your imports and settings files.

Posted in 3D, All, Unity 3D, Visual Studio, Windows 10. Tagged with , , , , , .

News: The new Unity 5.1 is ready for you.

The new Unity 5.1 is ready for you to download.
Some new features can be found here.
The Unity 5.1 release come with industry-leading multiplatform support, and unmatched efficiency boosted in the cloud.
It brings you a new unified development experience for VR platforms like Oculus Rift and our easy to use Unity Multiplayer networking solution.
Changes to your project are detected as you work and builds are delivered automatically to your device from the cloud, or distributed to
your team so you can play them on the Web with Unity Cloud Build.
The Unity Cloud Build come with this options : Free, Pro – 25$ / month, Studio – 100$ / month and Enterprise.
You can read more about Unity 5.1 here.

Posted in news. Tagged with , , , .