Click here to Skip to main content
13,896,966 members
Click here to Skip to main content
Add your own
alternative version

Stats

45.3K views
1.1K downloads
37 bookmarked
Posted 5 Aug 2015
Licenced CPOL

Unity 3D Game Creation using C# Script

, 11 Sep 2015
Rate this:
Please Sign up or sign in to vote.
In this article we can see how to create a simple Unity 3D game using C# Script.

Introduction

In this article let’s see how to

  • 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 game. It also supports multi platform means we can build and deploy our Unity program to WEB, Windows and MAC OS, iOS, Android, BlackBerry and etc. It’s interesting right, yes this is the main advantage of Unity as we write once and can deploy to multiple platform. Another advantage of Unity is we can write our C# script to perform some actions on Game Objects for example we can write C# Script to rotate 3d text ,Cube and 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 https://unity3d.com/learn/tutorials

For game lovers who don’t know how to write your game application using C# then it’s 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 this link http://docs.unity3d.com/ScriptReference/ .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 here. https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx

After installing the Visual Studio 2015 Click Start -> Programs-> Click Visual Studio 2015.

Click New -> Project - > Select Game. If you didn’t installed 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 few minutes to complete the installation process.

Once the Unity installation was completed Click Start Menu -> Click Unity Folder and click Unity.

When you click the 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 using 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 the 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 6 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 and etc to our Scene by right clicking the Hierarchy Window and select as per our need.(Here the game objects like Cube,Sphere3D text and 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 we can also
  • 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 and 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 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 tool bar at the top of Unity Framework.

  1. First tool will be used to Drag and adjust the Scene of our Game.
  2. 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.
  3. 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.
  4. 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 I have selected the green color for the Directional Light Object and I have increase the Identity of the Light Object from the property window.

The final light effect we can see it 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 same like our Visual Studio Property window, the 3D Text object has properties like Text (Here 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 and 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 for 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 clicks 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 and etc. For more details on MonoDevelop refer the link here http://docs.unity3d.com/Manual/MonoDevelop.html

C# Script file Code Part: by default when we open the C# Code file it will be look like below code.

We can see 2 default namespace will be added in our C# Script file.

Our entire C #script class will inherit MonoBehaviour class by default. MonoBehaviour is base class of all the script derives from, for more details of MonoBehaviour kindly refer to the link http://docs.unity3d.com/ScriptReference/MonoBehaviour.html .

When we create our C# Script file we can see 2 default method 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 a 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 continues rotation of the object from our game output.

using UnityEngine;
using System.Collections;

public class shanuBehaviour : MonoBehaviour {

           // Use this for initialization
           void Start () {     
           }
          
           // Update is called once per frame
           void Update () {
                          }
}

Simple C# Script file to rotate our Objects

Here we can see I have a written a simple code on Update() Method to rotate the objects in set of interval times.

transofrm.RotateAround() method will be used to rotated 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 {

           // Use this for initialization
           void Start () {
           }

          
           // Update is called once per frame
           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 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 and 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 and etc we can select any one to see our output in or 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

shanuFirstUnity.zip - 2015-08-06

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

syed shanu
Team Leader
India India
Microsoft MVP | Code Project MVP | CSharp Corner MVP | Author | Blogger and always happy to Share what he knows to others. MyBlog

My Interview on Microsoft TechNet Wiki Ninja Link

You may also be interested in...

Pro

Comments and Discussions

 
GeneralInteresting Pin
yuzaihuan23-Sep-15 17:04
memberyuzaihuan23-Sep-15 17:04 
GeneralMy vote of 5 Pin
Mihai MOGA16-Sep-15 7:46
professionalMihai MOGA16-Sep-15 7:46 
GeneralRe: My vote of 5 Pin
syed shanu16-Sep-15 14:14
mvasyed shanu16-Sep-15 14:14 
GeneralMy vote of 5 Pin
moizsharp15-Sep-15 6:25
membermoizsharp15-Sep-15 6:25 
GeneralExcellent Article +5 Pin
CodingK27-Aug-15 14:11
professionalCodingK27-Aug-15 14:11 
GeneralRe: Excellent Article +5 Pin
syed shanu27-Aug-15 14:50
mvasyed shanu27-Aug-15 14:50 
GeneralRe: Excellent Article +5 Pin
CodingK27-Aug-15 15:02
professionalCodingK27-Aug-15 15:02 
GeneralRe: Excellent Article +5 Pin
syed shanu27-Aug-15 16:28
mvasyed shanu27-Aug-15 16:28 
GeneralMy vote of 5 Pin
BytePower25-Aug-15 7:33
memberBytePower25-Aug-15 7:33 
GeneralRe: My vote of 5 Pin
syed shanu25-Aug-15 13:07
mvasyed shanu25-Aug-15 13:07 
Questionwhy install unity from vs2015.. Pin
andrea contoli23-Aug-15 20:51
memberandrea contoli23-Aug-15 20:51 
AnswerRe: why install unity from vs2015.. Pin
syed shanu23-Aug-15 21:56
mvasyed shanu23-Aug-15 21:56 
GeneralRe: why install unity from vs2015.. Pin
CodingK27-Aug-15 14:46
professionalCodingK27-Aug-15 14:46 
Questionwow interesting Pin
georr11-Aug-15 22:16
membergeorr11-Aug-15 22:16 
AnswerRe: wow interesting Pin
syed shanu11-Aug-15 22:28
mvasyed shanu11-Aug-15 22:28 
Questiongreat article Pin
OwenGrad11-Aug-15 19:48
memberOwenGrad11-Aug-15 19:48 
AnswerRe: great article Pin
syed shanu11-Aug-15 19:58
mvasyed shanu11-Aug-15 19:58 
GeneralMy vote of 5 Pin
Santhakumar Munuswamy @ Chennai8-Aug-15 5:06
professionalSanthakumar Munuswamy @ Chennai8-Aug-15 5:06 
GeneralRe: My vote of 5 Pin
syed shanu9-Aug-15 14:48
mvasyed shanu9-Aug-15 14:48 
Questionvs2013 Compatible? Pin
sreeyush sudhakaran5-Aug-15 21:36
professionalsreeyush sudhakaran5-Aug-15 21:36 
AnswerRe: vs2013 Compatible? Pin
syed shanu5-Aug-15 21:38
mvasyed shanu5-Aug-15 21:38 
GeneralRe: vs2013 Compatible? Pin
sreeyush sudhakaran5-Aug-15 22:19
professionalsreeyush sudhakaran5-Aug-15 22:19 
AnswerRe: vs2013 Compatible? Pin
Zephire5-Aug-15 21:43
memberZephire5-Aug-15 21:43 
GeneralRe: vs2013 Compatible? Pin
sreeyush sudhakaran5-Aug-15 22:17
professionalsreeyush sudhakaran5-Aug-15 22:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190306.1 | Last Updated 11 Sep 2015
Article Copyright 2015 by syed shanu
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid