Programming Chapter 2 Old

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
Line 6: Line 6:
  
 
====JavaScript - Function ====
 
====JavaScript - Function ====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// function FunctionName(parameters) : type
 
// function FunctionName(parameters) : type
  
Line 49: Line 49:
  
 
====JavaScript - Array ====
 
====JavaScript - Array ====
<javascript>
+
<syntaxhighlight lang="javascript">
 
var weapons : String[] = new String[5]; // Declare an array of 5 strings
 
var weapons : String[] = new String[5]; // Declare an array of 5 strings
 
weapons[0] = "Sword";                  // Set item 0 to Sword
 
weapons[0] = "Sword";                  // Set item 0 to Sword
Line 88: Line 88:
  
 
====JavaScript Variables====
 
====JavaScript Variables====
<javascript>
+
<syntaxhighlight lang="javascript">
 
var playerName;
 
var playerName;
 
var playerLives;
 
var playerLives;
Line 109: Line 109:
  
 
====Code - Index Operator====
 
====Code - Index Operator====
<javascript>
+
<syntaxhighlight lang="javascript">
 
weapons[0] = "Sword";    // Set item 0 to Sword
 
weapons[0] = "Sword";    // Set item 0 to Sword
 
playerName = weapons[0]; // playerName to item 0
 
playerName = weapons[0]; // playerName to item 0
Line 116: Line 116:
  
 
====Code - New Operator====
 
====Code - New Operator====
<javascript>
+
<syntaxhighlight lang="javascript">
 
playerLives = new int(); // playerLives = 0;
 
playerLives = new int(); // playerLives = 0;
 
</javascript>
 
</javascript>
Line 122: Line 122:
  
 
====Code - Cast Operator====
 
====Code - Cast Operator====
<javascript>
+
<syntaxhighlight lang="javascript">
 
playerStrength = 2.51;            // use "2.51f" in C#
 
playerStrength = 2.51;            // use "2.51f" in C#
 
playerLives = (int)playerStrength; // don't need to cast in JavaScript
 
playerLives = (int)playerStrength; // don't need to cast in JavaScript
Line 129: Line 129:
  
 
====Code - Arithmetic Operators====
 
====Code - Arithmetic Operators====
<javascript>
+
<syntaxhighlight lang="javascript">
 
playerLives = 3 + 2;  // addition
 
playerLives = 3 + 2;  // addition
 
playerLives = 3 - 2;  // subtraction
 
playerLives = 3 - 2;  // subtraction
Line 138: Line 138:
 
Those are the arithmetic operators (+, -, *, /, %). The arithmetic operators are operated on first because there is an order of operation, called the operator precedence. The assign operator has the lowest precedence, so it always goes last. So for example, the first thing the compiler does with the statement 'playerLives = 3 + 2', is add the 3 and 2 together, then that value is assigned to playerLives.
 
Those are the arithmetic operators (+, -, *, /, %). The arithmetic operators are operated on first because there is an order of operation, called the operator precedence. The assign operator has the lowest precedence, so it always goes last. So for example, the first thing the compiler does with the statement 'playerLives = 3 + 2', is add the 3 and 2 together, then that value is assigned to playerLives.
 
====Code - Parenthesis====
 
====Code - Parenthesis====
<javascript>
+
<syntaxhighlight lang="javascript">
 
playerLives = 3 * (3 + 2 - (1 + 1));
 
playerLives = 3 * (3 + 2 - (1 + 1));
 
</javascript>
 
</javascript>
Line 144: Line 144:
  
 
====Code - Arithmetic Assignment Operators====
 
====Code - Arithmetic Assignment Operators====
<javascript>
+
<syntaxhighlight lang="javascript">
 
playerLives += 1; // Same as "playerLives = playerLives + 1;"
 
playerLives += 1; // Same as "playerLives = playerLives + 1;"
 
playerLives -= 1; // Same as "playerLives = playerLives - 1;"
 
playerLives -= 1; // Same as "playerLives = playerLives - 1;"
Line 151: Line 151:
  
 
====Code - Increment/Decrement Operators====
 
====Code - Increment/Decrement Operators====
<javascript>
+
<syntaxhighlight lang="javascript">
 
playerLives++;  // player got an extra life
 
playerLives++;  // player got an extra life
 
playerLives--;  // oh, but now he lost a life
 
playerLives--;  // oh, but now he lost a life
Line 158: Line 158:
  
 
====Code - Logical Operators====
 
====Code - Logical Operators====
<javascript>
+
<syntaxhighlight lang="javascript">
 
isPlayerAlive = true && true;  // = true
 
isPlayerAlive = true && true;  // = true
 
isPlayerAlive = true && false;  // = false
 
isPlayerAlive = true && false;  // = false
Line 172: Line 172:
  
 
====Code - Relational Operators====
 
====Code - Relational Operators====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// Different ways to say player is still alive if they have lives left
 
// Different ways to say player is still alive if they have lives left
 
isPlayerAlive = playerLives > 0;
 
isPlayerAlive = playerLives > 0;
Line 182: Line 182:
  
 
====Code - Ternary Operator====
 
====Code - Ternary Operator====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// If player is alive, name will be LivingMan, otherwise DeadMan
 
// If player is alive, name will be LivingMan, otherwise DeadMan
 
playerName = isPlayerAlive ? "LivingMan" : "DeadMan";
 
playerName = isPlayerAlive ? "LivingMan" : "DeadMan";
Line 189: Line 189:
  
 
====Code - Concat Operators====
 
====Code - Concat Operators====
<javascript>
+
<syntaxhighlight lang="javascript">
 
playerName = "Super" + "Player";  // = SuperPlayer
 
playerName = "Super" + "Player";  // = SuperPlayer
 
playerName += "One";              // = SuperPlayerOne
 
playerName += "One";              // = SuperPlayerOne
Line 196: Line 196:
  
 
====Code - Member Access Operator====
 
====Code - Member Access Operator====
<javascript>
+
<syntaxhighlight lang="javascript">
 
Debug.Log(message);
 
Debug.Log(message);
 
guiText.text = message;
 
guiText.text = message;
Line 207: Line 207:
  
 
====JavaScript - Class====
 
====JavaScript - Class====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// File: Point.js
 
// File: Point.js
 
var x;
 
var x;
Line 243: Line 243:
 
Scope modifiers are a way to tell what "scope" a variable, method, class, etc. has. An object has full access to everything inside, however non-public items cannot be used from the outside. Using the Example-1 project again, change the message type to private and save...
 
Scope modifiers are a way to tell what "scope" a variable, method, class, etc. has. An object has full access to everything inside, however non-public items cannot be used from the outside. Using the Example-1 project again, change the message type to private and save...
 
====JavaScript - Scope Modifier====
 
====JavaScript - Scope Modifier====
<javascript>
+
<syntaxhighlight lang="javascript">
 
private var message : String;
 
private var message : String;
 
</javascript>
 
</javascript>
Line 257: Line 257:
  
 
'''JavaScript'''
 
'''JavaScript'''
<javascript>
+
<syntaxhighlight lang="javascript">
 
var p : Point;
 
var p : Point;
 
p.x = 2;
 
p.x = 2;
Line 273: Line 273:
  
 
====JavaScript - New Operator Revisited====
 
====JavaScript - New Operator Revisited====
<javascript>
+
<syntaxhighlight lang="javascript">
 
var p = new Point();
 
var p = new Point();
 
p.x = 2;
 
p.x = 2;
Line 293: Line 293:
  
 
====JavaScript - Constructor====
 
====JavaScript - Constructor====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// File: Point.js
 
// File: Point.js
 
// Have to declare as class to make a constructor
 
// Have to declare as class to make a constructor
Line 358: Line 358:
  
 
'''JavaScript'''
 
'''JavaScript'''
<javascript>
+
<syntaxhighlight lang="javascript">
 
var p = new Point(2, 3);
 
var p = new Point(2, 3);
 
p.Translate(1, 1); // Point is now [3, 4]
 
p.Translate(1, 1); // Point is now [3, 4]
Line 378: Line 378:
  
 
====JavaScript - Child====
 
====JavaScript - Child====
<javascript>
+
<syntaxhighlight lang="javascript">
 
class Location extends Point
 
class Location extends Point
 
{
 
{
Line 396: Line 396:
  
 
'''JavaScript'''
 
'''JavaScript'''
<javascript>
+
<syntaxhighlight lang="javascript">
 
var home = new Location();
 
var home = new Location();
 
home.description = "Home";
 
home.description = "Home";
Line 414: Line 414:
  
 
====JavaScript - ToString====
 
====JavaScript - ToString====
<javascript>
+
<syntaxhighlight lang="javascript">
 
function Start()
 
function Start()
 
{
 
{
Line 434: Line 434:
  
 
====JavaScript - Custom ToString ====
 
====JavaScript - Custom ToString ====
<javascript>
+
<syntaxhighlight lang="javascript">
 
function ToString() : String
 
function ToString() : String
 
{
 
{
Line 456: Line 456:
  
 
====JavaScript - Object====
 
====JavaScript - Object====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// This is an variable (field, attribute) within the HelloWorld class
 
// This is an variable (field, attribute) within the HelloWorld class
 
var message : String = "Hello World!";
 
var message : String = "Hello World!";
Line 586: Line 586:
  
 
====JavaScript - GameManager====
 
====JavaScript - GameManager====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// The result text that will display the outcome of the game
 
// The result text that will display the outcome of the game
 
var resultText : GUIText;
 
var resultText : GUIText;
Line 716: Line 716:
  
 
====JavaScript - TextButton====
 
====JavaScript - TextButton====
<javascript>
+
<syntaxhighlight lang="javascript">
 
// Colors for our text depending on it's state (normal, hover, pressed)
 
// Colors for our text depending on it's state (normal, hover, pressed)
 
var normalColor = Color.white;
 
var normalColor = Color.white;

Revision as of 20:48, 10 January 2012

Author: Lucas Goss

Contents

Are we Functional?

A function is a group of instructions that perform a specific task. If you completed Example-1, we had two functions in our script: Update and Start.

JavaScript - Function

// function FunctionName(parameters) : type
 
// This function takes as input two numbers and adds them, returning the value
function Add(number1 : int, number2 : int) : int
{
    return number1 + number2;
}
 
// This function has no parameters and returns nothing
function Start()
{
    // Call a function using it's name and parameter list
    var sum = Add(3, 4);
}
</javascript>
 
====C# - Function ====
<syntaxhighlight lang="csharp">
// modifiers type FunctionName(parameters)
 
// This function takes as input two numbers and adds them, returning the value
int Add(int number1, int number2)
{
    return number1 + number2;
}
 
// This function has no parameters and returns nothing
void Start()
{
    // Call a function using it's name and parameter list
    int sum = Add(3, 4);
}

In C# you'll notice we have a void keyword before our function name "Start"... void is an empty type. So when you don't want to return anything you use void. return is another keyword, used to return a value from a function. You can have multiple return statements in one function, but the first one that gets called is used and the others will do nothing. So when a function reaches a return, it immediately returns that value. Note that it must be of the same type declared as the function return type. Also note that if you have a void function you can still use return, but it must be an empty "return;".

Parameters are a comma separated variable declaration that are your inputs to a function. Functions don't always have to have inputs (like Start and Update), but hopefully you can see the usefulness as in the examples above. When you call a function it must have the same number of parameters as the function declaration. The "+" sign is an operator... but first lets try our hand at arrays.

An Array of Information

An Array is simply a static list of variables, meaning it's a fixed size and doesn't change automatically. If you have an array of size 5, it stays at size 5 unless you create a new one. So you can't add items dynamically (that's a dynamic list). An array is declared with square brackets '[]' and the index is 0 based (meaning 0 is the first number, not 1). So a size of 5 would be numbers 0 to 4.

JavaScript - Array

var weapons : String[] = new String[5]; // Declare an array of 5 strings
weapons[0] = "Sword";                   // Set item 0 to Sword
weapons[3] = "Knife";                   // Set item 3 to Knife
weapons[5] = "Gun";                     // Error: invalid index (only 0-4 exists)
 
weapons = new String[6];                // Create a new array of size 6
 
var weapon1 = weapons[0];               // weapon1 will be "" not "Sword" because of new list
weapons[5] = "Gun";                     // OK now, since the size is 6
 
 
var weapons = ["Sword", "Knife", "Gun"]  // Array values can also be declared along side the array itself
 
var weapons : String[] = new String[3];  //Same as the above example but takes 4 lines rather than one.
weapons[0] = "Sword";
weapons[1] = "Knife";
weapons[2] = "Gun";      
</javascript>
 
====C# - Array ====
<syntaxhighlight lang="csharp">
string[] weapons = new string[5]; // Declare an array of 5 strings
weapons[0] = "Sword";             // Set item 0 to Sword
weapons[3] = "Knife";             // Set item 3 to Knife
weapons[5] = "Gun";               // Error: invalid index (only 0-4 exists)
 
weapons = new string[6];          // Create a new array of size 6
 
string weapon1 = weapons[0];      // weapon1 will be "" not "Sword" because of new list
weapons[5] = "Gun";               // OK now, since the size is 6

So you probably noticed a new keyword, it's an operator just like the []s are.

Call the Operator

Note: The examples in this section use the following variables:

JavaScript Variables

var playerName;
var playerLives;
var playerStrength;
var isPlayerAlive;
var weapons = new String[5];
var message = "Hello World!";
</javascript>
 
====C# Variables====
<syntaxhighlight lang="csharp">
string playerName;
int playerLives;
float playerStrength;
bool isPlayerAlive;
string[] weapons = new string[5];
string message = "Hello World!";

An operator is a symbol used to operate on constants and variables. We already learned one operator, the assignment operator, but there are many others. I won't cover them all (use the language references), but a few common ones.

Code - Index Operator

weapons[0] = "Sword";    // Set item 0 to Sword
playerName = weapons[0]; // playerName to item 0
</javascript>
The index operator "[]", which you just saw earlier, is used to get a specified index in an array.
 
====Code - New Operator====
<syntaxhighlight lang="javascript">
playerLives = new int(); // playerLives = 0;
</javascript>
The 'new' operator creates objects and invokes constructors. What? We'll explain this later.
 
====Code - Cast Operator====
<syntaxhighlight lang="javascript">
playerStrength = 2.51;             // use "2.51f" in C#
playerLives = (int)playerStrength; // don't need to cast in JavaScript
</javascript>
A cast simply converts one type of object to another. However, a conversion operator must be defined (which it is for most basic types). This is a straight conversion, so you may lose information if casting to a different type.  C# will error if you don't have an "f" on the end of 2.51, because by default all decimal numbers are double. The f suffix is a shorthand cast to a float. JavaScript does implicit casting, meaning it will automatically cast to whatever type the object is. C# requires more explicit casting (you have to do it yourself). There are highs and lows to both, as sometimes you can lose information in JavaScript and not realize it (like if a float is casted to an int and back to a float).
 
====Code - Arithmetic Operators====
<syntaxhighlight lang="javascript">
playerLives = 3 + 2;   // addition
playerLives = 3 - 2;   // subtraction
playerLives = 3 * 2;   // multiplication
playerLives = 3 / 2;   // division
playerLives = 3 % 2;   // modulus (division remainder)
</javascript>
Those are the arithmetic operators (+, -, *, /, %). The arithmetic operators are operated on first because there is an order of operation, called the operator precedence. The assign operator has the lowest precedence, so it always goes last. So for example, the first thing the compiler does with the statement 'playerLives = 3 + 2', is add the 3 and 2 together, then that value is assigned to playerLives.
====Code - Parenthesis====
<syntaxhighlight lang="javascript">
playerLives = 3 * (3 + 2 - (1 + 1));
</javascript>
You can also use parenthesis to specify order. For every open parenthesis there must be a closing one as well, and they work just like they do in mathematics (the inner most parenthesis is executed first, then the next, and so on).
 
====Code - Arithmetic Assignment Operators====
<syntaxhighlight lang="javascript">
playerLives += 1; // Same as "playerLives = playerLives + 1;"
playerLives -= 1; // Same as "playerLives = playerLives - 1;"
</javascript>
We've already covered the assignment operator, but there are arithmetic assignment operators as well (+=, -=, *=, /=, %=). They are a shorthand way to do an arithmetic operation on the current value. This code takes the current value of playerLives and in the first case adds one to it. The second case subtracts one from playerLives. The others work in a similar manner.
 
====Code - Increment/Decrement Operators====
<syntaxhighlight lang="javascript">
playerLives++;  // player got an extra life
playerLives--;  // oh, but now he lost a life
</javascript>
The '++' and '--' are increment and decrement operators, that increment and decrement a variable by 1.
 
====Code - Logical Operators====
<syntaxhighlight lang="javascript">
isPlayerAlive = true && true;   // = true
isPlayerAlive = true && false;  // = false
isPlayerAlive = false && false; // = false
 
isPlayerAlive = true || true;   // = true
isPlayerAlive = true || false;  // = true
isPlayerAlive = false || false; // = false
 
isPlayerAlive = !true;          // = false
</javascript>
Conditional operators are a boolean type of operation. They're really important for conditional statements which we'll cover later. First there is the logical operators, AND (&&), OR (||) and NOT (!). The code above shows some truth tables using the logical operators.
 
====Code - Relational Operators====
<syntaxhighlight lang="javascript">
// Different ways to say player is still alive if they have lives left
isPlayerAlive = playerLives > 0;
isPlayerAlive = playerLives >= 1;
isPlayerAlive = playerLives != 0;
isPlayerAlive = !(playerLives == 0);
</javascript>
Another type of conditional operators are relational operators. These are EQUAL (==), NOT EQUAL (!=), LESS THAN (<), GREATER THAN(>), LESS THAN OR EQUAL (<=), and GREATER THAN OR EQUAL (>=).
 
====Code - Ternary Operator====
<syntaxhighlight lang="javascript">
// If player is alive, name will be LivingMan, otherwise DeadMan
playerName = isPlayerAlive ? "LivingMan" : "DeadMan";
</javascript>
And a third type of conditional operator is the ternary operator (?). It returns the first expression if true and returns the second expression if false as in "condition ? 1st expression : 2nd expression". If the expression in the code above "isPlayerAlive" is true, then playerName will be "LivingMan".
 
====Code - Concat Operators====
<syntaxhighlight lang="javascript">
playerName = "Super" + "Player";  // = SuperPlayer
playerName += "One";              // = SuperPlayerOne
</javascript>
The '+' and '+=' operators are also used as shorthand for concatenation of strings.
 
====Code - Member Access Operator====
<syntaxhighlight lang="javascript">
Debug.Log(message);
guiText.text = message;
</javascript>
Remember those? If you did the chapter one example, "Example-1", they may be familiar to you. The member access operator is used to access the members and methods of an object.
 
== Class.ified ==
 
A class is a custom type that allows variables and functions that are related to be grouped together and treated as one thing. Note that variables are sometimes called fields or attributes within a class, and functions may be called methods or behaviors in a class. For a class example lets look at a point. A point in 2D space is made up of an x and y coordinate and you can translate that point to another location.
 
====JavaScript - Class====
<syntaxhighlight lang="javascript">
// File: Point.js
var x;
var y;
 
// Move from current position the requested distance
function Translate(dx, dy)
{
	x += dx;
	y += dy;
}
</javascript>
 
====C# - Class====
<syntaxhighlight lang="csharp">
// File: Point.cs
public class Point
{
	public int x;
	public int y;
 
	// Move from current position the requested distance
	public void Translate(int dx, int dy)
	{
		x += dx;
		y += dy;
	}
}

In Unity when you create a new script, it automatically creates a new class for you. In JavaScript you can use the keyword class as I did with the C# code, but it's not necessary as Unity automatically does this for you if it's not there. Unity also makes the JavaScript class, variables and methods public by default...

Scope Modifiers

Scope modifiers are a way to tell what "scope" a variable, method, class, etc. has. An object has full access to everything inside, however non-public items cannot be used from the outside. Using the Example-1 project again, change the message type to private and save...

JavaScript - Scope Modifier

private var message : String;
</javascript>
====C# - Scope Modifier====
<syntaxhighlight lang="csharp">
private string message;

Now look in the inspector... message is gone! This is because the inspector will only show public variables because that's all it has access to. private only allows access from inside the class. Another type protected allows access only to the class and child classes. And public allows anyone access.

I Object

An object is a class that has been instantiated. If you tried to use the Point class that we made above like:

JavaScript

var p : Point;
p.x = 2;
p.y = 3;
</javascript>
 
'''C#'''
<syntaxhighlight lang="csharp">
Point p;
p.x = 2;
p.y = 3;

You'd get an error saying something like: "Use of unassigned local variable...", or "Object reference not set to an instance of an object...". This is because your class is not instantiated. A class is just a representation of an object, but not an actual object itself (meaning the computer has not allocated any memory for it). To create an object you have to use the operator new

JavaScript - New Operator Revisited

var p = new Point();
p.x = 2;
p.y = 3;
p.Translate(1, 1); // Point is now [3, 4]
p.Move(3, -4);     // This will give an error since there is no method named "Move"
</javascript>
 
====C# - New Operator Revisited====
<syntaxhighlight lang="csharp">
Point p = new Point();
p.x = 2;
p.y = 3;
p.Translate(1, 1); // Point is now [3, 4]
p.Move(3, -4);     // This will give an error since there is no method named "Move"

New is followed by a class constructor. By default an empty constructor (has no parameters) is created for you (in our case Point()). A constructor initializes all of the class members (x and y in our Point class), and is required to create an object. It also must be the same name as your class. We can also create our own constructor (including our own empty constructor).

JavaScript - Constructor

// File: Point.js
// Have to declare as class to make a constructor
class Point
{
	var x : int;
	var y : int = 5; // We can initialize here too, setting a default value if none is supplied
 
	// Empty constructor, initialize point to [1, 1]
	function Point()
	{
		x = 1;
		y = 1;
	}
 
	// Constructor to let user set initial point
	function Point(ix, iy)
	{
		x = ix;
		y = iy;
	}
 
	// Move from current position the requested distance
	function Translate(dx, dy)
	{
		x += dx;
		y += dy;
	}
}
</javascript>
 
====C# - Constructor====
<syntaxhighlight lang="csharp">
// File: Point.cs
public class Point
{
	public int x;
	public int y = 5; // We can initialize here too, setting a default value if none is supplied
 
	// Empty constructor, initialize point to [1, 1]
	public Point()
	{
		x = 1;
		y = 1;
	}
 
	// Constructor to let user set initial point
	public Point(int ix, int iy)
	{
		x = ix;
		y = iy;
	}
 
	// Move from current position the requested distance
	public void Translate(int dx, int dy)
	{
		x += dx;
		y += dy;
	}
}

Most of the time in Unity you don't need a constructor, as you'll want to set the initial values in the editor. But you can place a default value if none is supplied by initializing variables where they are created (such as our y = 5). Lets go back to our example and use the constructor we made:

JavaScript

var p = new Point(2, 3);
p.Translate(1, 1); // Point is now [3, 4]
p.Move(3, -4);     // This will give an error since there is no method named "Move"
p.ToString();         // This won't give us an error. Huh?
</javascript>
 
'''C#'''
<syntaxhighlight lang="csharp">
Point p = new Point(2, 3);
p.Translate(1, 1); // Point is now [3, 4]
p.Move(3, -4);     // This will give an error since there is no method named "Move"
p.ToString();         // This won't give us an error. Huh?

If you noticed, we also added a call to the method ToString(). But there is no method ToString in our Point class you say... well, not exactly.

In .NET, every class is a child of the object class. A child basically means that it inherits all of the members and methods of the parent. You can inherit any public class you want (making it a child) by doing:

JavaScript - Child

class Location extends Point
{
	var description = "";
}
</javascript>
 
====C# - Child====
<syntaxhighlight lang="csharp">
public class Location : Point
{
	public string description = "";
}

Now we can use these new classes and every property of their parent. So now we can do:

JavaScript

var home = new Location();
home.description = "Home";
home.x = 0;
home.y = 0;
</javascript>
 
'''C#'''
<syntaxhighlight lang="csharp">
Location home = new Location();
home.description = "Home";
home.x = 0;
home.y = 0;

The object class (which every class inherits even without explicitly declaring it in code), has some basic characteristics that are defined for everything (which is why everything is a child of it). One of them is ToString, which gets a string representation of your class. By default the value of ToString is the name of your class. Take example one and add this to the Start method:

JavaScript - ToString

function Start()
{
	var p = new Point(2, 3);
	Debug.Log(p.ToString());
}
</javascript>
 
====C# - ToString====
<syntaxhighlight lang="csharp">
void Start()
{
	Point p = new Point(2, 3);
	Debug.Log(p.ToString());
}

Now run the program and the debug log should now print "Point". Of course, that's not too useful. The real power of child classes is the ability to override the parent. Change your Point class by adding this method:

JavaScript - Custom ToString

function ToString() : String
{
	// Concatenate and format so Point appears as "[x,y]"
	return "[" + x.ToString() + "," + y.ToString() + "]";
}
</javascript>
 
====C# - Custom ToString ====
<syntaxhighlight lang="csharp">
public string override ToString()
{
	// Concatenate and format so Point appears as "[x,y]"
	return "[" + x.ToString() + "," + y.ToString() + "]";
}

Now if you run the program the debug log should print "[2,3]". Pretty nifty huh?

Well since everything is an object, or appears to be, there's quite a few things you can do with default types. Lets take a look at our Example-1 project that we created last chapter. Be sure to read the new comments and code added to the Start() method...

JavaScript - Object

// This is an variable (field, attribute) within the HelloWorld class
var message : String = "Hello World!";
 
// This is a method within the HelloWorld class
function Start()
{
	// Debug is a Debug object that uses the . operator to access the static method "Log",
	// which takes one parameter of type string. A static method is one that can be accessed
	// without using the keyword new
	Debug.Log(message);
 
	// Use our newly created point class
	var p = new Point(2, 3);
	Debug.Log(p.ToString());
 
	// Declare an int variable and set the value to 5
	var x = 5;
 
	// ToString is a method of x... it is a member of the object class that gets the string
	// representation of an object. In this case "5" the string, not the number
	Debug.Log(x.ToString());
 
	// Almost everything is an object (or appears to be)...
	Debug.Log(21.ToString());
	Debug.Log("Done".ToString());
}
 
 
// Another method
function Update()
{
	// guiText is a GUIText object that uses the . operator to access the member variable "text"
	guiText.text = message;
}
</javascript>
 
====C# - Object====
<syntaxhighlight lang="csharp">
using UnityEngine;
using System.Collections;
 
// This is a HelloWorld class, that is public and a child of (inherits) MonoBehavior
public class HelloWorld : MonoBehaviour
{
	// This is an variable (field, attribute) within the HelloWorld class
	public string message = "Hello World!";
 
	// This is a method within the HelloWorld class
	void Start ()
	{
		// Debug is a Debug object that uses the . operator to access the static method "Log",
		// which takes one parameter of type string. A static method is one that can be accessed
		// without using the keyword new
		Debug.Log(message);
 
		// Use our newly created point class
		Point p = new Point(2, 3);
		Debug.Log(p.ToString());
 
		// Declare an int variable and set the value to 5
		int x = 5;
 
		// ToString is a method of x... it is a member of the object class that gets the string
		// representation of an object. In this case "5" the string, not the number
		Debug.Log(x.ToString());
 
		// Almost everything is an object (or appears to be)...
		Debug.Log(21.ToString());
		Debug.Log("Done".ToString());
	}
 
	// Another method
	void Update ()
	{
		// guiText is a GUIText object that uses the . operator to access the member variable "text"
		guiText.text = message;
	}
}

The HelloWorld class inherits from MonoBehavior, meaning it now has all the variables and methods that are in MonoBehavior as well as object. Everything inside of the begin and end blocks is part of the HelloWorld object. Also since just about everything is an object you can cast any type to an object (using the cast operator). Well you can read more about Object Oriented Programming here.

Recap

Things we learned:

  • Functions are a group of instructions that take parameters and return a type
  • An Array is a list of items, declared and indexed with []
  • Operators
    • Index: [index] where index is the 0 based number of an item
    • New: new creates a new object and invokes it's constructor
    • Cast: (type) where the object has a conversion operator for type
    • Arithmetic (and Arithmetic Assignment)
      • Addition: + (+=)
      • Subtraction: - (-=)
      • Multiplication: * (*=)
      • Division: / (/=)
      • Modulus: % (%=)
    • Increment: ++, increment by one
    • Decrement: --, decrement by one
    • Logical
      • And: &&
      • Or: ||
      • Not: !
    • Relational
      • Equal: ==
      • Not Equal: !=
      • Less Than: <
      • Greater Than: >
      • Less Than or Equal: <=
      • Greater Than or Equal: >=
    • Ternary: ?
    • Concatenation: +, for strings
    • Member Access: .
    • A class is created using the keyword class and contains methods (functions) and members (variables)
    • Scope Modifiers
      • private is only accessible to the class defining it
      • protected is only accessible to the class defining it and classes that inherit from it
      • public is accessible to everything
    • An Object is created by instantiating a class using the keyword new followed by the constructor
    • ToString() is a method in the object class, which all classes are children of
    • Almost everything is of type object (and can be cast to an object)

Example-2

  • Create a new project and name it 'Example-2.
  • Create a folder in the project view and name it Example-2 Assets
  • Create a script in the "Example-2 Assets" folder and name it GameManager. Open it and insert this code.

JavaScript - GameManager

// The result text that will display the outcome of the game
var resultText : GUIText;
 
// Track our wins, losses and draws
private var wins : int;
private var losses : int;
private var draws : int;
 
// Used to initialize values
function Start()
{
	wins = 0;
	losses = 0;
	draws = 0;
}
 
// OnSelection will be called when the player makes a selection
// The pSelection will be the option they chose ("Rock", "Scissors", or "Paper")
function OnSelection(pSelection : String)
{
	var pIsRock = pSelection == "Rock" ? true : false;
	var pIsScissors = pSelection == "Scissors" ? true : false;
	var pIsPaper = pSelection == "Paper" ? true : false;
 
	// Get a random number between 1 and 3 for computer selection
	var cSelection : int = Random.value * 3;
 
	// Just to make sure it's working right, log the computers selection
	Debug.Log(cSelection.ToString());
 
	var cIsRock = cSelection == 0 ? true : false;
	var cIsScissors = cSelection == 1 ? true : false;
	var cIsPaper = cSelection == 2 ? true : false;
 
	// Figure out if it was a win or draw, otherwise it's a loss
	var isDraw = (pIsRock && cIsRock) || (pIsScissors && cIsScissors) ||
		(pIsPaper && cIsPaper);
 
	var isWin = (pIsRock && cIsScissors) || (pIsScissors && cIsPaper) ||
		(pIsPaper && cIsRock);
 
	// Set the results text based on the outcome
	var result = isDraw ? "A Draw" : "";
	result = isWin ? "You Win" : result;
	result = result == "" ? "You Lose" : result;
 
	// Set the totals based on outcome
	draws += isDraw ? 1 : 0;
	wins += isWin ? 1 : 0;
	losses += (!isDraw && !isWin) ? 1 : 0;
 
	// Set and show the results
	// "\n" is the character for a newline
	resultText.guiText.text = "Winner: " + result + "\n" +
		"W: " + wins + " L: " + losses + " D: " + draws;
}
</javascript>
 
====C# - GameManager====
<syntaxhighlight lang="csharp">
using UnityEngine;
using System.Collections;
 
public class GameManager : MonoBehaviour
{
	// The result text that will display the outcome of the game
	public GUIText resultText;
 
	// Track our wins, losses and draws
	private int wins;
	private int losses;
	private int draws;
 
	// Used to initialize values
	void Start()
	{
		wins = 0;
		losses = 0;
		draws = 0;
	}
 
	// OnSelection will be called when the player makes a selection
	// The pSelection will be the option they chose ("Rock", "Scissors", or "Paper")
	public void OnSelection(string pSelection)
	{
		bool pIsRock = pSelection == "Rock" ? true : false;
		bool pIsScissors = pSelection == "Scissors" ? true : false;
		bool pIsPaper = pSelection == "Paper" ? true : false;
 
		// Get a random number between 1 and 3 for computer selection
		int cSelection = (int)(Random.value * 3);
 
		// Just to make sure it's working right, log the computers selection
		Debug.Log(cSelection.ToString());
 
		bool cIsRock = cSelection == 0 ? true : false;
		bool cIsScissors = cSelection == 1 ? true : false;
		bool cIsPaper = cSelection == 2 ? true : false;
 
		// Figure out if it was a win or draw, otherwise it's a loss
		bool isDraw = (pIsRock && cIsRock) || (pIsScissors && cIsScissors) ||
			(pIsPaper && cIsPaper);
 
		bool isWin = (pIsRock && cIsScissors) || (pIsScissors && cIsPaper) ||
			(pIsPaper && cIsRock);
 
		// Set the results text based on the outcome
		string result = isDraw ? "A Draw" : "";
		result = isWin ? "You Win" : result;
		result = result == "" ? "You Lose" : result;
 
		// Set the totals based on outcome
		draws += isDraw ? 1 : 0;
		wins += isWin ? 1 : 0;
		losses += (!isDraw && !isWin) ? 1 : 0;
 
		// Set and show the results
		// "\n" is the character for a newline
		resultText.guiText.text = "Winner: " + result + "\n" +
			"W: " + wins + " L: " + losses + " D: " + draws;
	}
}

The GameManager will process all of the users selections, make a random computer selection, compare and display the results, and keep track of the game stats. Next we need to create some buttons.

  • Create a script in the "Example-2 Assets" folder and name it TextButton. Open it and insert this code.

JavaScript - TextButton

// Colors for our text depending on it's state (normal, hover, pressed)
var normalColor = Color.white;
var hoverColor = Color.white;
var pressedColor = Color.white;
var manager : GameManager;
 
private var isPressed = false;
 
function Start()
{
	guiText.material.color = normalColor;	
}
 
function OnMouseEnter ()
{
	guiText.material.color = isPressed ? pressedColor : hoverColor;
}
 
function OnMouseExit ()
{
	guiText.material.color = normalColor;
}
 
function OnMouseUp ()
{
	isPressed = false;
	manager.OnSelection(guiText.text);
}
 
function OnMouseDown ()
{
	isPressed = true;
	guiText.material.color = pressedColor;
}
</javascript>
 
====C# - TextButton====
<syntaxhighlight lang="csharp">
using UnityEngine;
using System.Collections;
 
public class TextButton : MonoBehaviour
{
	// Colors for our text depending on it's state (normal, hover, pressed)
	public Color normalColor = Color.white;
	public Color hoverColor = Color.white;
	public Color pressedColor = Color.white;
	public GameManager manager;
 
	private bool isPressed = false;
 
	// Use this for initialization
	void Start()
	{
		// Set the initial text color to the normalColor
		guiText.material.color = normalColor;
	}
 
	// Called whenever the mouse enters (goes over) our text
	void OnMouseEnter()
	{
		guiText.material.color = isPressed ? pressedColor : hoverColor;
	}
 
	// Called whenever the mouse leaves (no longer over) our text
	void OnMouseExit()
	{
		guiText.material.color = normalColor;
	}
 
	// Called when the mouse button is released after being pressed
	void OnMouseUp()
	{
		isPressed = false;
		manager.OnSelection(guiText.text);
	}
 
	// Called when the mouse button is pressed
	void OnMouseDown()
	{
		isPressed = true;
		guiText.material.color = pressedColor;
	}
}

Now we can use that script on any GUIText object to make it a button. When pressed it will call the OnSelection method of manager. Well that's all the scripting, it's now time to create the Unity objects.

  • Create a GUI Text object, name it ResultText, and set the text to "Winner"
  • Create an Empty Game Object, and name it GameManager
  • Drag the "GameManager" script onto the "GameManager" object.
  • Select the "GameManager" object and drag the "ResultText" object onto the "Result Text" variable in the "Game Manager (Script)" section.
  • Create 3 GUI Text objects and name them PaperText, RockText, and ScissorText
    • Drag the "TextButton" script on each of them
    • Set the text to "Paper", "Rock", and "Scissors" respectively and set X position of each in the Inspector as 0.6, 0.7 and 0.8 respectively
    • Set the Normal Color to Cantaloupe
    • Set the Hover Color to Honeydew
    • Set the Pressed Color to Licorice
    • Drag the "GameManager" object on to the Manager variable
  • Save the scene as MainScene in the "Example-3 Assets" folder

Run it and... let the games begin!!!

Unity Package: Media:Example-2 Package.unityPackage.zip


Programming Index : Previous Chapter : Next Chapter

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox