18
UFCEKU-20-3 Web Games Programming Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

Embed Size (px)

Citation preview

Page 1: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Web Games Programming

Unity Scripting Fundamentals

Page 2: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Agenda

Scripting Languages in Unity Creating Script Assets Default Script Structure Attaching Scripts to Objects in the 3D Environment Communicating with Environment Objects using ‘tag’ Using the Debug Utility Function Establishing Script to Script Communication

Page 3: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Supported Languages in Unity

Javascript C# (sharp) Boo

Unity documentation provides examples for each type. Javascript is most widely used examples but C# now becoming

first choice in current Unity tutorials . Module using Javascript to align with the set text: Unity Game

Development Essentials.

Page 4: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Creating Script Assets

From the Assets Menu choose Create Choose the language you want from the menu list The default name newly created scripts is NewBehaviorScript Rename the script to give a name that conveys the scripts

functionality.

Page 5: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Javascript Example

#pragma strict

function Start () {

}

function Update () {

}

Page 6: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

C# Example

using UnityEngine;

using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

}

}

Page 7: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Initialization Example (.js)

#pragma strict

function Start () {

var anIntegerVariable:int = 5;

var afloatVariable:float = 5.0f

}

function Update () {

}

Page 8: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Attaching Scripts to Objects

Create the new script Drag the script from the Assets Window onto the

object you want the script to affect. Or select Add Component from the Inspector

Window and navigate to the Scripts Panel. Script behaviours can be switched on or off via the

Component Panel checkbox.

Page 9: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Changing an Object’s Properties via Scripts

Cube Object

// fragment

function Update () {

if(Input.GetKeyDown(KeyCode.R)){

gameObject.renderer.material.color = Color.red;

}

ColourSwitcher.js

Page 10: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Referencing Objects Using ‘Tags’

Tags – tags are simply keywords that can be assigned to an object in the environment. Tagged objects can then be referenced inside scripts.

Tags are similar to instance names in ActionScript. Adding tags to objects is a two-step process. First add your chosen tag name to a list in the Tag

Manager. Then apply it to your chosen object. You can then make references to the tag name in your

scripts.

Page 11: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Adding a Tags to Environment Objects

e.g. Environment object Sphere with tag ‘aSphere’

// code fragment

if(hit.collider.gameObject.tag == “aSphere”){

Debug.Log(‘I collided with the sphere’);

}

Page 12: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Debug Utility Function

Very useful debug and environment events utility function

Debug.Log (“Message you to want to send to the Console Window”);

Debug.Log("I have received " + numberOfMessages + ” messages from the transmit script");

Page 13: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Inter-Script Communication

Transmit Receive

Transmit.js

Code to send message to Receive.js attached tocube object

Receive.js

Code to handle message sent by Transmit.js attached to sphere object

Sphere Cube

Page 14: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Transmit.js Attached to Sphere Object

#pragma strict

// example of script - to - script communication

// this script transmits messages to the script attached to the cube

function Start () {

}

function Update () {

if(Random.value > 0.5f){

Receive.messageReceived = true;

}

else

Receive.messageReceived = false;

}

}

Page 15: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Receive.js Attached to Cube Object

#pragma strict

/// this script receive messages from the transmit script attached to the sphere

static var messageReceived:boolean;// important! static variable

var numberOfMessages:int;

function Start () {

messagesReceived = false;

numberOfMessages = 0;

}

function Update () {

if (messageReceived == true){

numberOfMessages++;

Debug.Log("I have received " + numberOfMessages + " messages from the transmit script");

}

else {

Debug.Log("No messages received");

}

}

Page 16: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Static Variables

Declaring variables as static make them available to be referenced by other scripts in the 3D environment.

In programming parlance static variables have global scope

// static variable declaration in Receive.js

static var messageReceived:boolean;

Page 17: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Static Variable Referenced in Transmit.js

// Transmit.js

if(Random.value > 0.5f){

Receive.messageReceived = true;

}

else {

Receive.messageReceived = false;

}

}

Page 18: UFCEKU-20-3Web Games Programming Unity Scripting Fundamentals

UFCEKU-20-3Web Games Programming

Unity 3D Scripting Resources

http://docs.unity3d.com/Documentation/ScriptReference/

http://answers.unity3d.com/index.html

http://www.lynda.com