Introduction
This article covers the following topics:
- Introduction to Unity 3D
- Install Unity from Visual Studio 2015
- Create your first Unity 3D Application
- Introduction to Unity 3D Framework
- How to add C# Script to your Unity 3D Application
- How to add the C# Script to selected object to produce the result
- How to Build and Run the Unity 3D Application to web
Introduction to Unity 3D
Unity is used to develop a game application. Using Unity, we can create 2D and 3D games. It also supports multi platform which means we can build and deploy our Unity program to WEB, Windows and MAC OS, iOS, Android, BlackBerry, etc. It’s interesting right, yes this is the main advantage of Unity as we write once and can deploy to multiple platforms. Another advantage of Unity is that we can write our C# script to perform some actions on Game Objects, for example, we can write a C# script to rotate 3D text, Cube, etc. We can see more details of how to install Unity and how to work and run our first Unity Game application.
You can refer the Unity3d web site for more details here.
For game lovers who don’t know how to write your game application using C#, it’s a very good place to start with Unity. Unity is a very good platform to start and write your own first game application and write scripts using C#.
For Unity C# script tutorials, kindly refer to this link. You can find both C# Script API and JavaScript API .
Install Unity from Visual Studio 2015
Here, I have used Visual Studio 2015. You can download the Visual Studio 2015 Community Edition from this link.
After installing Visual Studio 2015, click Start -> Programs -> Click Visual Studio 2015.
Click New -> Project - > Select Game. If you didn’t install Unity in your computer, you can install from here. Select Install Unity and click OK.
Click Install to continue installation of Unity and Visual Studio Tools for Unity.
Select both Unity and Visual Studio Tools for Unity and click Next which will install Unity in your computer. Wait for a few minutes to complete the installation process.
Once the Unity installation is complete, click Start Menu -> Click Unity Folder and click Unity.
When you click Unity from Start menu, for the first time, it will ask you to sign in using your Unity account. If you don’t have the Unity Account, you can create a new Unity account and use the account login to open your Unity framework.
If you have Processional Edition License, you can give the license code, otherwise select Unity Personal Edition and click Next.
Create Your First Unity 3D Application
Click Create new project to create your first Unity Game application.
Select your Project Folder and give your Unity application name and select 2D or 3D. In this example, I have used Unity 3D so I have selected 3D and click Create Project.
Now you are ready to work with your first Unity Game application development.
Using the Code
Introduction to Unity 3D Framework
After creating a new project, we can see Unity Framework as below. In Unity framework, we can see six main parts as below:
- Hierarchy Window – All the added game objects, Camera and Light for our Scene will be listed in this window. We can add 2D, 3D Object, etc. to our Scene by right clicking the Hierarchy Window and select as per our need. (Here, the game objects like
Cube
, Sphere3D
text, etc).You can also add the objects from GameObject menu.
- Project Window – We can see our entire project file in this window. We can add C# Script file from this window.
- Scene Window – We can see all our Objects which has added to the scene here for example if we add Cube and 3D Text, we can see in this Scene window as well.
- Game Window – Here, we can see the final output of our scene. We can click the Run button to see the game output.
- Console Window – In this window, we can see error message if any we have in our script file.
- Property Window – We can change the selected object Font color, Alignment, etc.
When we care our Unity program in Hierarchy window, we can see by default Main Camera and Directional Light will be added.
Main Camera
This will be used to display our objects in any angle as we placed.
Here, we can see that I have added 3D Capsule object and 3D Text object to the scene. Now, we can see how to use property window for each selected object.
Object Toolbar
We can see the below toolbar at the top of Unity Framework.
- The first tool will be used to Drag and adjust the Scene of our Game.
- The second tool will be used to move the selected object and place it in our desired location. When we click on Move tool and select some object from the scene and drag the object, we can see the X and Y position of object will be changing from the property window.
- The third tool will be used to rotate the selected object. When we click on rotate tool and select some object from scene, by clicking on the object, we can rotate the object as we needed, we can also see the selected object rotation X and Y value will be changing. To see the actual output, click on the Game window.
- The fourth tool is to Scale the selected Object. We can resize the selected object by clicking the Scale tool.
Property Window
Now we start with adding color and Directional Light.
The Directional Light object will be used to add light effect to our objects, for example, here we can see I have selected the Directional light and drag it and place it near the Capsule Object.
Now in property window, we can see that I have selected the green color for the Directional Light Object and I have increased the Identity of the Light Object from the property window.
We can see the final light effect on the Capsule object which was initially white in color due to light placed for the Capsule Object, now it has green color light effect on it.
3D Text Property
Here, we can see similar to our Visual Studio Property window, the 3D Text object has properties like Text (we can add any text to the selected 3D Text object, for example, I have given my name as “Shanu
”). We can add color to the Text
property, etc. We can give name for object from the top of the Property window or from the Hierarchy window, we can rename our object names.
How to Add C# Script to Your Unity 3D Application
So far, we have added our basic design to our Game application, now let’s see how to add some basic actions like rotating our 3D Capsule and 3D Text Object by writing a simple C# Script.
In our .NET Web or Winform application, we can see Code-Behind (.CS) file to add C# code part, but in Unity, we can add C# code by adding C# Script from the Project Window.
To add the C# Script file, right click on the Project window as below and select Create and click on C# Script.
After we add the C# Script file, we can see now C# File will be added .We can rename the C# Script file as per our need for example, here I have named the C# Script file as “shanuBehaviour”.
To write our C# code on the Script file, double click on the C# Script file which we have added. We can see the C# Script file will be open in Mono Editor as below.
MonoDevelop is a IDE which is default editor for writing the scripts like C#, JavaScript, etc. For more details on MonoDevelop, refer to the link here.
C# Script file Code Part
By default, when we open the C# code file, it will look like the below code.
We can see that two default namespaces will be added in our C# Script file.
Our entire C # script class will inherit MonoBehaviour
class by default. MonoBehaviour
is the base class of all the script derives from. For more details of MonoBehaviour
, kindly refer to this link.
When we create our C# Script file, we can see two default methods as Start()
and Update()
.
Start()
method: The Start()
method is similar to our Webform Page Load or Winform Form Load Method. The Start
method will be run only once at the start of the program. Update()
method: The Update()
method is the important method as this method will be called every time to perform our action. For example, if we need to rotate an Object
, we write code in the update
method and assign the C# Script file to the Object. When we run the Unity program, every time this update method will be called and we can see continuous rotation of the object from our game output.
using UnityEngine;
using System.Collections;
public class shanuBehaviour : MonoBehaviour {
void Start () {
}
void Update () {
}
}
Simple C# Script File to Rotate Our Objects
Here, we can see that I have written a simple code on Update()
method to rotate the objects in set of interval times.
transofrm.RotateAround()
method will be used to rotate the objects. We pass the parameters as rotate directions and Time interval to the RoteAround()
method.
using UnityEngine;
using System.Collections;
public class shanuBehaviour : MonoBehaviour {
void Start () {
}
void Update () {
transform.RotateAround (Vector3.right,Vector3.up , 40 * Time.deltaTime);
}
}
After writing the C# code, save it in MonoDevelop
and we can see the changes in our Unity framework at the right side of the property window.
Console Window: We can see the error messages of our C# Script from Console Window. For example, I have misspelled the “this
” keyword with “thisd
”, we can see the error message at the bottom of the Console window and also when we click on Game window, we can see the error message as below:
How to Add the C# Script to Selected Object to Produce the Result
To add our C# Script file to our selected object, click on the C# Script file and drag it to our selected objects in Hierarchy window. See the below Gif image to see how to add the C# Script file to our Object.
Now that we have created our simple Game Application using Unity, run the program and check the output from the Game Window.
How to Build and Run the Unity 3d Application to Web
To build and run the output in Browser, click on File -> Build & Run.
Select your Scene as we have created only one Scene, I will click on the Add Current button which will add our Current Scene to be build and run. In platform, we can see Web Player, PC, iOS, Android, etc. We can select any one to see our output in our selected platform. Here, I have selected the Web Player.
If we click on Build, we can see the result output files will be added to our selected folder. Here, when we click on BuildOutput.html file, we can see our Unity game will be displayed in browser.
We can install the Unity web player to see the output.
History
- 5th August, 2015: Initial version