Game Development Reference
In-Depth Information
Chapter 5
Introduction to Scripting with C#
To bring your games to life in Unity, you will be adding scripts to trigger and manage the desired
functionality. Scripting is about equal parts syntax, logic, and Unity functionality, with a good dose
of math thrown into the mix. If math skills are part of your distant past, the Unity community is a
good source for specialty scripts. If you already have programming skills, you will be able to tap the
community for detailed advice, suggestions, and solutions.
Whether you are already familiar with programming or are a complete beginner, you will require a
basic understanding of scripting in Unity to set you on your way. If this sounds daunting, don't worry.
Scripting is a bit like a subset of the English language in that the syntax and vocabulary are familiar
enough that you will be able to read through and figure out what a lot of it will do. Unity's Scripting
Reference is full of helpful examples once you grasp how and where to implement them. You will
also find that, just as with automobiles, you don't have to understand the inner workings of some of
the code in order to use it.
Scripting for Unity
Unity supports three scripting languages directly: C# (C sharp), UnityScript (Unity's version of
JavaScript), and Boo (a Python-like language). In the early days of Unity, UnityScript/JavaScript was
used in most of the tutorials, documentation, and sample code. It is a very forgiving language that
is easier for beginners and made the transition for users of Flash (where ActionScript is a JavaScript
derivative) relatively painless. Today, however, C# has become increasing popular within the Unity
community, as it is more powerful than UnityScript and well suited for mobile. There are now C#
examples for most of the code in the documentation, and a lot of the code samples you will find
online are C#. The syntax is very similar to Unity script, and as you become more familiar with Unity
and scripting, you will often find yourself converting one to the other.
In Unity, the scripting languages are based on a Mono framework (an open source version of .NET).
Mono is a cross-operating-system software platform that comprises language compilers (C#, etc.),
a runtime (a distributable program that manages memory, threading, and operating-system differences
and executes compiled code) and a set of class libraries for things like networking, user interface,
openGL (graphics handling), etc. This is what allows you to “author once and deploy to multiple
platforms.”
197
 
Search WWH ::




Custom Search