Number Wizard Console UnityScript Translation Notes What is UnityScript? UnityScript is the third language supported natively bu Unity Inspired by Boo and Javascript, UnityScript is a looser language that takes concepts from Javascript to allow for fast development without worrying about some of the overheads that C# require UnityScript is not Javascript While the syntax is superficially similar, UnityScript is a fully class based language while Javascript is prototype based There are many other smaller differences in the syntax and semantics, but the other important difference at this point is that UnityScript has access to all the standard C# class libraries and integrates natively with the Mono platform (and Unity libraries) You can find out more about the major differences between UnityScript and Javascript on the Unity3D wiki The UnityScript Basics for Noobs (pdf) is a great resource and will run through the basics of scripting using UnityScript We will not attempt to cover the same material in these translation notes, but will instead focus on the differences between C# and UnityScript You can also find a list of UnityScript Keywords on the Unity3D wiki which will come in useful as we dive into the language Generally, UnitScript syntax will look very familiar after C#: C# UnityScript void NextGuess () { guess = (max + min) / 2; print ("Higher or lower than " + guess); print ("Up = higher, down = lower, return = equal"); } function NextGuess() { guess = (max + min) / 2; print ("Higher or lower than " + guess); print ("Up = higher, down = lower, return = equal"); } Why Use UnityScript? UnityScript, by being looser with both typing and syntax, can sometimes let a developer get something working very quickly For example, much of the code that is required to set up a C# script is optional This lets us concentrate on the behaviour and not worry about the overhead This comes at a cost, however Many advanced features, such as Lambdas and Delegates that are available in C# are missing or complicated to use in UnityScript If you want a more throrough inspection of the missing features, you can take a look at this extensive list That said, because Unity will support multiple languages in the same project, nothing stops us from using UnityScript in the majority of our code, and jump into C# to use some of the more advanced features This does lead to some complexity with compilation order which we’ll go into in future translation notes when the issue arises, but works without much friction for simpler projects Translating our first script It is worth noting that there are several translators avaible that will carry out the translation automatically within unity Notable is Florent Poujol’s translator which is a commercial project available on the assetstore A limited (100 lines) version exists for free online to try out as well and can often help when translating small scripts Since this is the first time we have translated to UnityScript, we will go through the NumberWizard.cs file in some details Future translation notes will simply highlight new or interesting features that were introduced by the project instead of going over familiar ground Don’t forget that the entire project file translated into UnityScript is attached to this lecture! Learn more at www.CompleteUnityDeveloper.com Number Wizard Console UnityScript Translation Notes Declaring Classes Many of the things we have to specify in C# are optional in UnityScript Most obvious is the class declaration In C# each file is a class, and the class has to be explicitly declared within that file This can lead to problem when renaming files but not the class inside, or vice versa This can be omitted in UnityScript for classes that extend MonoBehaviour: C# UnityScript using UnityEngine; using System.Collections; // class Definitin here public class NumberWizards : MonoBehaviour{ // class Definition here } The Code to define the behaviour of the class can be added straight into the file without explicitly declaring it The class name is then the script name by default While this is useful, sometimes we need to create a class that inherits from another type, or implements an interface In that case, we can use explicit class declaration similarly to C#: class MyType extends ParentType implements AnInterface{ // class Definition here } Importing other modules You might have noted in the code above that we did not use using statements to import the required UnityEngine and System.Collections namespaces That’s because those are imported for us by default We have access to all the Unity libraries without explicitly requiring them, so we can save on some more typing That said, we can still explicitly import a module that isn’t loaded by default, like UnityEngine.UI by using the import keyword: C# UnityScript using UnityEngine.UI; import UnityEngine.UI; And since we’re accessing the same underlying libraries, we can use the same namespaces and we’ll find the object we’re used to from C# Declaring Methods Method declaration is very similar to C#, but function return types not need to be specified when declaring a function For example, we had to explicitly tell the compiler that our NextGuess() function returned nothing by using the void keyword, but we don’t have to this in UnityScript We however have to use the function keyword to declare functions: C# UnityScript void NextGuess() { guess = (max + min) / 2; print ("Higher or lower than " + guess); print ("Up = higher, down = lower, return = equal"); } function NextGuess() { guess = (max + min) / 2; print ("Higher or lower than " + guess); print ("Up = higher, down = lower, return = equal"); } Learn more at www.CompleteUnityDeveloper.com Number Wizard Console UnityScript Translation Notes As we can see, the rest of the function is essentially the same We can also optionally define a return typr to make the code more efficient (no need to infer the type) and help us catch errors at compile time by defining the return type after the function declaration using a colon: function MyMethod() : ReturnType { } Input arguments can be defined as in C#, except that the colon is once again used to define type This is optional and can be omitted function MyOtherMethod(firstArg : TypeOfFirstArg, secondArg : TypeOfSecondArg){ // } function YetAnotherMethod(arg1, arg2, arg3){ // } Declaring Variables Declaring variables in UnityScript is a little different to C# We use the var keyword to declare a new variable and the type of the variable is optionally defined after the variable name and separated from it by a colon: var foobar; // An untyped variable called foobar var max : int; // An integer variable named max var myTransform : Transform; // A Transform variable named myTransform The convention for capitalisation of variables and types is the same as in C# What’s pragma strict? At the beginning of most script you will see the #pragma strict directive What this does is explained in some details on the wiki: It’s a good habit to get into and required for iOS development #pragma strict will enforce stricter type checking, generate more useful error messages sooner, and will encourage good programming habits What pragma strict does is toggle the UnityScript compiler to be stricter with regards to typing For example, optional types for variables and method arguments will now be required for all but the trivialest operations This, as mentioned int the wiki, is good practice and will make your code more robust It is recommended that all UnityScript files begin with the #pragma strict line Full translation Taking all the above into consideration gives us the following for the full version of NumberWizard.js: #pragma strict var max : int; var : int; var guess : int; Learn more at www.CompleteUnityDeveloper.com Number Wizard Console UnityScript Translation Notes function Start() { StartGame(); } function StartGame() { max = 1000; = 1; guess = 500; max = max + 1; print ("========================"); print ("Welcome to Number Wizard"); print ("var number: Pick a in your head, but don't tell me!"); print ("The highest number you can pick is " + max); print ("The lowest number you can pick it " + min); print ("Is the number higher or lower than " + guess); print ("Up = higher, down = lower, return = equal"); } function Update() { if (Input.GetKeyDown(KeyCode.UpArrow)) { = guess; NextGuess(); } else if (Input.GetKeyDown(KeyCode.DownArrow)) { max = guess; NextGuess(); } else if (Input.GetKeyDown(KeyCode.Return)) { print("I won!"); StartGame(); } } function NextGuess() { guess = (max + min) / 2; print ("Higher or lower than " + guess); print ("Up = higher, down = lower, return = equal"); } None of which should come as a surprise Hopefully you can now translate simple scripts from C# into UnityScript Learn more at www.CompleteUnityDeveloper.com ...Number Wizard Console UnityScript Translation Notes Declaring Classes Many of the things we have to specify in C# are optional in UnityScript Most obvious is the class declaration... files but not the class inside, or vice versa This can be omitted in UnityScript for classes that extend MonoBehaviour: C# UnityScript using UnityEngine; using System.Collections; // class Definitin... by using the void keyword, but we don’t have to this in UnityScript We however have to use the function keyword to declare functions: C# UnityScript void NextGuess() { guess = (max + min) / 2;