Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Games Programming Unity Scripting Fundamentals.

Similar presentations


Presentation on theme: "Web Games Programming Unity Scripting Fundamentals."— Presentation transcript:

1 Web Games Programming Unity Scripting Fundamentals

2 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

3 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.

4 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.

5 Javascript Example #pragma strict function Start () { }
function Update () {

6 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 () {

7 Initialization Example (.js)
#pragma strict function Start () { var anIntegerVariable:int = 5; var afloatVariable:float = 5.0f } function Update () {

8 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.

9 Changing an Object’s Properties via Scripts
Cube Object ColourSwitcher.js // fragment function Update () { if(Input.GetKeyDown(KeyCode.R)){ gameObject.renderer.material.color = Color.red; }

10 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.

11 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’); }

12 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");

13 Inter-Script Communication
Sphere Cube Transmit Receive Transmit.js Code to send message to Receive.js attached to cube object Receive.js Code to handle message sent by Transmit.js attached to sphere object

14 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;

15 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");

16 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;

17 Static Variable Referenced in Transmit.js
if(Random.value > 0.5f){ Receive.messageReceived = true; } else { Receive.messageReceived = false;

18 Unity 3D Scripting Resources


Download ppt "Web Games Programming Unity Scripting Fundamentals."

Similar presentations


Ads by Google