se 350 – programming games

25
SE 350 – Programming Games Lecture 6: Programming with Unity Lecturer: Gazihan Alankuş Please look at the last slide for assignments (marked with TODO) 2/10/2012 1

Upload: taurus

Post on 05-Jan-2016

37 views

Category:

Documents


1 download

DESCRIPTION

SE 350 – Programming Games. Lecture 6: Programming with Unity Lecturer: Gazihan Alankuş. Please look at the last slide for assignments (marked with TODO ). Today. Cover most topics about scripting in Unity. The BEST Development Environment for Unity Scripting that I Know of. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: SE 350 – Programming Games

1

SE 350 – Programming Games

Lecture 6: Programming with UnityLecturer: Gazihan Alankuş

Please look at the last slide for assignments (marked with TODO)

2/10/2012

Page 2: SE 350 – Programming Games

2

Today

• Cover most topics about scripting in Unity

2/10/2012

Page 3: SE 350 – Programming Games

3

The BEST Development Environment for Unity Scripting that I Know of

• Visual Studio + Resharper for coding– I asked for a classroom license. I also sent an e-mail. Still

waiting. In the meantime go ahead and install the 30 day version.

• MonoDevelop for occasional line-by-line debugging (explained towards end of presentation)

• If you are using any other way, you would certainly develop a better game if you listen to me

• (If you disagree, talk to me before discarding my advice… I have worked on large projects.)

2/10/2012

Page 4: SE 350 – Programming Games

4

Pointer Fun with Binky

• No class talking about references is complete without Binky!

• This is not a joke. If you don’t get why you watched it, please ask.

• http://en.wikipedia.org/wiki/File:Pointer_Fun_with_Binky_(Java).ogg

2/10/2012

Page 5: SE 350 – Programming Games

5

Variables with class Types are ReferencesVariables with struct Types are values

• Transform transform;• GameObject myObject;• Structs do not work that way– Vector3, Quaternion, etc.– Vector3 v = new Vector3(0, 0, 1);– Vector3 v1 = v;– v1.x = 1;– //v.x == 0 still

2/10/2012

Page 6: SE 350 – Programming Games

6

Operator Overloading

• Vector3 v3 = new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);

• Is equivalent to• Vector3 v3 = v1 + v2;

• 0.5 * Vector3.up– Etc.

2/10/2012

Page 7: SE 350 – Programming Games

7

Components and Scripts

• Components are added to game objects and appear in the inspector

• Scripts (MonoBehavior) are custom components that you create– public variables appear in the inspector– Can have functions for handling GameObject’s

events (Update, OnCollisionEnter, etc.)

2/10/2012

Page 8: SE 350 – Programming Games

8

Game Objects and Components

• Your code runs in the context of the component• You can access

– The game object that it is attached to • gameObject

– Other components that are attached to the same game object• GetComponent<ComponentType>()• Shortcuts for standard components (transform, renderer, etc. )

– Parent game object in the hierarchy• transform.parent.gameObject

– Other game objects in the scene• GameObject.Find(“Game object’s name”)

– There are more (find all components in this hierarchy, etc.)

2/10/2012

Page 9: SE 350 – Programming Games

9

Accessing Things

2/10/2012

Cube

Transform

MyOtherScript (MonoBehavio

r)

MyScript (MonoBe

havior)

Game Object Components

GetComponent<MyOtherScript>()

transform

gameObject

GetComponent(“MyOtherScript”)

GetComponent<Transform>()

GetComponent(“Transform”)

Bushes GameObject.Find(“Bushes”)

Red text shows how this component accesses others

Page 10: SE 350 – Programming Games

10

Using the Inspector as a Guide• Don’t try to memorize anything. Just look at the inspector and

you’ll figure it out.

2/10/2012

Page 11: SE 350 – Programming Games

11

If you can do it through the user interface, you can script it

• You can access anything• You can tell them to do anything• Use the inspector and hierarchy as starting

points

2/10/2012

Page 12: SE 350 – Programming Games

12

Which Game Object Do You Attach Your Script to?

• It only changes– Whose components you want to access easily– Whose events you want to react to

• If these are not important for the script, it can be in one of the standard objects (camera, etc.)

2/10/2012

Page 13: SE 350 – Programming Games

13

Let’s do some examples

• How do I reach this object?• How do I tell him to do something?

2/10/2012

Page 14: SE 350 – Programming Games

14

Better Ways of Scripting

• Reaching game objects by name – Is slow (string comparisons)– Is static (have to have that specific name, difficult to reuse for

something else)• Another way is to expose references in the inspector

(public) and make connections in the user interface• However, the inspector can be an overkill, and you still

may want to find game objects by name– Don’t keep calling GameObject.Find() and GetComponent()– Make connections in the Awake() function

• It’s faster to use references that are already set

2/10/2012

Page 15: SE 350 – Programming Games

15

Connecting Objects in Awake()

• Awake runs before anything else. So, it is the preferred place for setting up connections– otherObject = GameObject.Find(“other object”);– otherComponent =

GetComponent<OtherComponent>()– Later use these variables during the game, just like

you use the references to the standard components (transform, renderer, etc.)

2/10/2012

Page 16: SE 350 – Programming Games

16

Creating and Destroying Objects while the Game is Running

• Instantiate(game object or prefab, position, rotation)

• Destroy(game object)

2/10/2012

Page 17: SE 350 – Programming Games

17

Some Basic Data Types (struct, not class)

• Vector3– For positions and directions

• Quaternion – For orientations and rotations

2/10/2012

Page 18: SE 350 – Programming Games

18

Transform

• transform.position• transform.rotation

– With respect to the world. Changes when parent changes.– Not what you see in the inspector.

• transform.localPosition• transform.localRotation• transform.localScale

– With respect to the parent. Does not change when parent changes.– What you see in the inspector

• transform.right, transform.up, transform.forward – x, y, z axes that you see

2/10/2012

Page 19: SE 350 – Programming Games

19

Data Structures with Unity

• Don’t underestimate. It’s C#! You can do anything!• Classes, objects, references• Variables, structs• Collections ArrayList, List<>, Dictionary<,>

– using System.Collections.Generic; • Enums• Classes in separate files or within other classes

– Use them to group variables together• This would help even if you don’t know Java:

– http://www.25hoursaday.com/CsharpVsJava.html

2/10/2012

Page 20: SE 350 – Programming Games

20

Some Coding Demonstration

2/10/2012

Page 21: SE 350 – Programming Games

21

Debugging with MonoDevelop while Coding in Visual Studio at the Same Time

• Do this once after you created your Unity project– Edit->Preferences->External Tools is where you set the IDE that Unity will fire

• Select Visual Studio– Double-click a script, this will create the Visual Studio project

• Select MonoDevelop– Double-click a script, this will get the project in MonoDevelop’s recents

• Select Visual Studio again

– Close everyting• Do this at every run

– Run MonoDevelop without running Unity– Select the project that is already in the recent projects– Run->Debug

• This will run Unity

– Set a breakpoint in MonoDevelop and see how it stops at that breakpoint– Run Visual Studio by either

• running it from the start menu, • Or ensuring Edit->Preferences->External Tools is Visual Studio and double clicking on a script.

– This does not affect MonoDevelop2/10/2012

Page 22: SE 350 – Programming Games

22

How to go about working with code

• Access what you are interested in– Easy, just use the inspector as a guide

• Get the value, it will probably be an object of a class– For example: Vector3

• Read about it in the script reference– http://unity3d.com/support/documentation/ScriptRefer

ence/

• Also, search for random stuff in the script reference. It’s likely to lead in the right direction.

2/10/2012

Page 23: SE 350 – Programming Games

23

How to improve yourself in C#

• Unity and C# tutorials in catlikecoding.com are good

• http://channel9.msdn.com/Series/C-Sharp-Fundamentals-Development-for-Absolute-Beginners

is not bad. If you don’t have enough time, watch 9, 14, 15, 21

• It’s fastest to learn from examples!– The five games that you already saw:

http://u3d.as/content/m2h/c-game-examples/1sG

2/10/2012

Page 24: SE 350 – Programming Games

24

TODO: Projects

• Every member of the group will send me a separate weekly report about what you did that week about the project– This is private. Do not share with others. – Just write it as an e-mail. No attaching word

documents or pdfs please!– Send them every Thursday night!

2/10/2012

Page 25: SE 350 – Programming Games

25

TODO: Midterm Exam

• Date: 21 March 2012 Time: 18:30 - 20:20 Place: A2

• No questions about the Maya interface

2/10/2012