Upload
gertrude-sullivan
View
216
Download
3
Embed Size (px)
Citation preview
UFCEKU-20-3Web Games Programming
Web 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
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.
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.
UFCEKU-20-3Web Games Programming
Javascript Example
#pragma strict
function Start () {
}
function Update () {
}
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 () {
}
}
UFCEKU-20-3Web Games Programming
Initialization Example (.js)
#pragma strict
function Start () {
var anIntegerVariable:int = 5;
var afloatVariable:float = 5.0f
}
function Update () {
}
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.
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
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.
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’);
}
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");
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
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;
}
}
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");
}
}
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;
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;
}
}
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