Category Archives: Game engine

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

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

Today I will show you how to install Unity 5 for free.
First will need to download it from official website – free download.
You need to follow this steps: Select Unity 5 Personal Edition.
Fill with your email adress and password from Unity website account.
Select your answers …
Press the blue button Start Unity and will be ready to start your project.

Posted in 3D, All, Game engine, Unity 3D, Windows 8. Tagged with , , , , , , , .

Unity 3D – C# script – countdown timer tutorial.

This tutorial will show us how to make one simple countdown timer.
First go to Asserts tab and click with right mouse button.
Create new C# script and open it with double clicks – left mouse button.
Drag and drop the script into your main camera.
Fill into Mono Developer with this script:

Press Play Button and you will see this.
The GameOver is empty and you can put there your own game over script.

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

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

This tutorial will cover the first steps programming with C#.
The C# is an more complicated language like another programming language.
Now, C# it is an object-oriented programming language like C++ or Java.
If you know this programming language then will be easy to you to use C#.
First in C# the entry point of program is a static function called Main inside a class, not more than one.
If you want to compile one C# program then you can use the compiler:
cc yourprogram.cs
… or just use the Mono IDE.
C# has data types, operators, variables, constants, typecasts, branching like any programming language.
Also C# allow you to create your own data types. This mean the C# is an extensible language.
Let’s see more about this:

  • data types: bool,byte, sbyte, char, short, ushort, int, uint, float, long, ulong, double, decimal
  • operators: +,-,*,/,%,++,–,*=,/=,-=,%=,>>=,<<=, bitwise, logical, shifting operators …
  • variables: int my_var = 1;
  • constants: const float my_const = 5;
  • typecasts: C# need to know if is an explicit and implicit conversions.
  • branching: if, switch, while, for, do-while, …

After you open the Unity 3D IDE the next step is to create the C# script.
Right click with the mouse into Project tab, like in the next image and simply create the script.
The default script is named NewBehaviourScript and come with this source code:

You can see the class NewBehaviourScript calls to the Unity Engine ( see: using UnityEngine).
Also two functions:
Start() – this is called when the scene first launches and Update() – This is called in every frame that the game runs.
Unity 3D will rewrite the class name in your C# script when you will rename your script in the Project panel.
You can create new scripts and also can be used to call different features of the game engine.
Foe example you can create one new script to move, rotate or scale one object from Unity 3D Engine.

Posted in All, C#, Game engine, Unity 3D, Windows 7, Windows 8. Tagged with , , , , , , .