1 / 18

Web Games Programming

Web Games Programming. Unity Scripting Fundamentals. 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

hija
Download Presentation

Web Games Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 () { varanIntegerVariable:int = 5; varafloatVariable: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 Cube Sphere 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

  14. Transmit.jsAttached 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.jsAttached 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 varmessageReceived:boolean;

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

  18. Unity 3D Scripting Resources • http://docs.unity3d.com/Documentation/ScriptReference/ • http://answers.unity3d.com/index.html • http://www.lynda.com

More Related