SlideServe
Menu
Préférences
© SlideServe · v2026

Web Games Programming

hija avatar
18 diapositives · 337 Views · Oct 07, 2014

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

Web Games Programming - slide 1
01 / 18Web Games Programming
1 / 17

Télécharger la présentation

Web Games Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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