Unity Scripting Primer

Unity has arguably the most flexible and complete scripting system of any engine at any price. Providing the ability for developers to use the languages Boo, Javascript, and C# – Unity covers the bases when it comes to supporting a variety of developers. While there is no specific support for Java within the system, C# is a mature language that provides a near peer to the language. In addition, through the .Net/Mono framework it is possible to roll your own support for Java, F#, or any other language that is supported by the .Net platform – including any new language that you create, though it's probably easier to just build a domain specific language (DSL) using Boo. The most important part is that each language is a first class citizen in Unity. With few exceptions, anything you want to do in Unity can be accomplished regardless of which language you choose.

The Unity Scripting system is at the heart of getting things done within Unity customizing the Unity editor and interacting with the runtime platform. Through scripts you have access to everything that you would want to change (and I do mean everything) including being able to extend the Unity compiler through the Abstract Syntax Trees (AST) themselves.

Oh no! You've got Mono!

When Microsoft originally created the .Net platform it created it principally for the Windows platform. However, Microsoft published the language specification to ECMA and shortly thereafter, the Mono open source project was created which brought the .Net platform to other platforms including OSX and Linux. At its core the Unity scripting system is built upon the .Net runtime and the benefits afforded it through the Mono project. While it was certainly possible that the Unity scripting system could have been based upon Java or some other system, .Net provided (out-of-the-box) a rich system for assemblies that made cross language functionality trivial to implement.

Common Language Infrastructure

Multiple scripting languages are possible in Unity due to the Common Language Infrastructure (Cli) of .Net. The purpose of the Common Language Infrastructure (CLI), is to provide a language-neutral platform for application development and execution, including functions for exception handling, garbage collection, security, and interoperability. By implementing the core aspects of the .NET Framework within the scope of the CLI, this functionality will not be tied to a single language, but will be available across the many languages supported by the framework. Microsoft's implementation of the CLI is called the Common Language Runtime, or CLR.

Boo- more than a ghost in mario

Boo is an object-oriented, statically typed programming language that seeks to make use of the Common Language Infrastructure's support for Unicode, internationalization and web-applications, while using a Python-inspired syntax and a special focus on language and compiler extensibility. Some features of note include type inference, generators, multimethods, optional duck typing, macros, true closures, currying, and first-class functions. Boo has been actively developed since 2003.

Boo is free software released under an MIT/BSD–style license. It is compatible with both the Microsoft .NET and Mono frameworks.

What does a Boo script look like?

import UnityEngine
import System.Collections

class example(MonoBehaviour):

    def Start():
        curTransform as Transform
        curTransform = gameObject.GetComponent[of Transform]()
        curTransform = gameObject.transform

    def Update():
        other as ScriptName = gameObject.GetComponent[of ScriptName]()
        other.DoSomething()
        other.someVariable = 5

Should I choose Boo?

If you're looking for a new and novel way to write code that doesn't have a lot of ceremony and doesn't waste your time with lots of curly braces, Boo is definitely a language you want to look into. While the idea of using whitespace as a delimiter is considered offensive by some, there is an elegance that comes from writing code in Boo. If you've written in traditional languages it's not difficult to understand and there are some functions of Unity (not the iOS version) which can only be accessed using Boo.

While Boo is a fine language, there are two reasons why you shouldn't choose it.

  1. First, there are very few examples written in Boo and very little written about it in the community at large.
  2. More importantly, Boo is not currently supported on the iOS platform as of this writing.

With that, our discussion of Boo within the context of iOS comes to an abrupt end.

UnityScript/JavaScript – Relevant beyond the web

JavaScript is an implementation of the ECMAScript language standard and is typically used to enable programmatic access to computational objects within a host environment. It can be characterized as a prototype-based object-oriented scripting language that is dynamic, weakly typed, and has first-class functions. It is also considered a functional programming language like Scheme and OCaml because it has closures and supports higher-order functions.

While UnityScript is, on the surface, syntactically similar to Javascript, the implementation used by Unity is not entirely ECMAScript compliant.

What does a JavaScript script look like?

function Start () {
  var curTransform : Transform;

  curTransform = gameObject.GetComponent(Transform);
  // This is equivalent to:
  curTransform = gameObject.transform;
}

function Update () {
  // To access public variables and functions 
  // in another script attached to the same game object.
  // (ScriptName is the name of the javascript file)
  var other : ScriptName = gameObject.GetComponent(ScriptName);
  // Call the function DoSomething on the script
  other.DoSomething ();
  // set another variable in the other script instance
  other.someVariable = 5;
}

Should I choose JavaScript?

JavaScript is the defacto language in use by the Unity community and is prevalent across the web. Many people refer to Javascript as Unityscript. There are many people in the world who use it on a daily basis and if you're coming from a Flash background – this is as close to ActionScript porting ease as you're likely to get (until someone ports ActionScript over to Unity). One of the strong benefits of JavaScript is that there isn't a lot of ceremony involved with getting things done and in most cases you will find that you write a lot less boilerplate to build an application with JavaScript. There is no need to create class structures and there isn't much need in getting directly to the attributes of objects and using them.

JavaScript is available on all platforms including the iPhone, but as you begin building more complex examples with larger teams you may find that having more structure and typing will result in fewer defects. In addition, while JavaScript is the community choice today – most authors and developers are moving over to C# as their primary development language.

C# – The revenge of Microsoft

C# (pronounced "see sharp")[6] is a multi-paradigm programming language encompassing imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It was developed by Microsoft within the .NET initiative and later approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270). C# is one of the programming languages designed for the Common Language Infrastructure.

C# is intended to be a simple, modern, general-purpose, object-oriented programming language.

What does a C# script look like?

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
  void Start() {
    Transform curTransform;
    curTransform = gameObject.GetComponent<Transform>();
    curTransform = gameObject.transform;
  }
  void Update() {
    ScriptName other = gameObject.GetComponent<ScriptName>();
    other.DoSomething();
    other.someVariable = 5;
  }
}

Should I choose C#?

As a language, C# will be familiar to anyone who has done any modern object-oriented programming in the past 10 years. The syntax is very familiar for Java developers and reasonably easy for C++ developers to pick up.

There are a number of libraries that are available on the Internet that can be dropped right into your project to do things like XML parsing, connecting to social networks, loading pages, etc. In addition there is a critical mass of C# developers in the Unity community such that there is an increased emphasis on doing projects and documentation on C# going forward.

Finally, many of those that have published large numbers of iOS projects have stated that C# is an easier path to take for writing applications. If you're coming from a professional game development background or want a clean object-oriented Java/Pascal syntax – this is your best choice.

Note

For the rest of the book, we will be using C# as the language for scripts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.234