SE 350 – Programming Games

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

description

SE 350 – Programming Games. Lecture 4 : Programming with Unity Lecturer: Gazihan Alankuş. Please look at the last slide for assignments (marked with TODO ). Outline. No quiz today! (next week) One hour of lecture Unity Scripting Last two hours - PowerPoint PPT Presentation

Transcript of SE 350 – Programming Games

Page 1: SE 350 – Programming Games

1

SE 350 – Programming Games

Lecture 4: 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

Outline

• No quiz today! (next week)• One hour of lecture– Unity Scripting

• Last two hours– We’ll visit the industrial design department

2/10/2012

Page 3: SE 350 – Programming Games

3

Scripting in Unity

• We’ll use C#– Others are not strongly-typed. Coding becomes a

guesswork without intellisense (ctrl+space and whatnot). • With C#, you get a lot of help while coding.

– I recommend using Visual Studio with ReSharper• Gives you auto-completion and quick refactoring

support like Eclipse or IntelliJ• Do a short demo

2/10/2012

Page 4: SE 350 – Programming Games

4

C# may be new for you

• Don’t read too much about it and confuse yourself. It’s easy. Learn the following:– Class (a collection of variable and function

definitions, just a blueprint)– Object (an actual copy of a class in memory that

you can use)– Method (functions defined in a class)– Field (variables defined in a class)

2/10/2012

Page 5: SE 350 – Programming Games

5

Defining and using

Defining a classclass MyClass {

int AField;void AMethod() {}

}

Creating objects from the class and using them

MyClass c = new MyClass();c.AField = 3;c.AMethod();

MyClass d = new MyClass();d.AField = 5;d.AMethod();

2/10/2012

c

AField

3

d

AField

5

Page 6: SE 350 – Programming Games

6

Learn as you go

• Don’t try to learn too much, you may confuse yourself

• Apply and practice everything you read so that it makes sense

2/10/2012

Page 7: SE 350 – Programming Games

7

Scripting in Unity

• You create a C# script (descendant from MonoBehavior)

• You attach it to a game object– Unity calls its functions at appropriate times• Awake (earliest possible, make connections)• Start (before any of the updates, after awake)• Update (at every frame)

– http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html

2/10/2012

Page 8: SE 350 – Programming Games

8

MonoBehavior

• This is one of the components that are attached to a game object

• You can access many useful fields that correspond to other components that are attached to other – transform – rigidbody

2/10/2012

Page 9: SE 350 – Programming Games

9

MonoBehavior

2/10/2012

Cube

Transform

Rigidbody

MyScript (MonoBe

havior)

Game Object Components Fields (automatic variablesin MyScript)

gameObject = Cube

transform = Transform

rigidbody = Rigidbody

rigidbody

transform

gameObject

Page 10: SE 350 – Programming Games

10

MonoBehavior

2/10/2012

Cube

Transform

MyOtherScript (MonoBehavio

r)

MyScript (MonoBe

havior)

Game Object Components

GetComponent<MyOtherScript>()

transform

gameObject

GetComponent(“MyOtherScript”)

GetComponent<Transform>()

GetComponent(“Transform”)

Page 11: SE 350 – Programming Games

11

MonoBehavior

2/10/2012

Cube

Transform

MyOtherScript (MonoBehavio

r)

MyScript (MonoBe

havior)

Game Object Components

GetComponent<MyOtherScript>()

transform

gameObject

GetComponent(“MyOtherScript”)

GetComponent<Transform>()

GetComponent(“Transform”)

It’s like doing this in class:

Transform transform;

void Awake() { transform = GetComponent<Transform>();}

Page 12: SE 350 – Programming Games

12

MonoBehavior

2/10/2012

Cube

Transform

MyOtherScript (MonoBehavio

r)

MyScript (MonoBe

havior)

Game Object Components

GetComponent<MyOtherScript>()

transform

gameObject

GetComponent(“MyOtherScript”)

GetComponent<Transform>()

GetComponent(“Transform”)

It’s like doing this in class:

Transform transform;

void Awake() { transform = GetComponent<Transform>();}

Similarly, you should do:

MyOtherScript myOtherScript;

void Awake() { myOtherScript = GetComponent<MyOtherScript>();}

Instead of calling GetComponent in Update()

Page 13: SE 350 – Programming Games

13

Using Standard Components in Code• Don’t try to memorize anything. Just look at the inspector and

you’ll figure it out.

2/10/2012

Page 14: SE 350 – Programming Games

14

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 15: SE 350 – Programming Games

15

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;

• Also, Vector3 is a struct, not a class. It’s just like an int.

2/10/2012

Page 16: SE 350 – Programming Games

16

How to learn C#

• Could not find a basic C# tutorial that is independent of other .Net stuff…

• 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!2/10/2012

Page 17: SE 350 – Programming Games

17

Great Sample Code in the Asset Store

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

• Download through Unity– Create new project and

open asset store using the link in the above site

– Click “import”– Five games with scenes– Also has a PDF file

2/10/2012

Page 18: SE 350 – Programming Games

18

TODO: Homework

• Use the five game examples from here (explained in previous slide): http://u3d.as/content/m2h/c-game-examples/1sG

• Read the PDF and understand how each game works

• Make changes in two of the games that convinces me that you understood how the game works

• Zip only the Assets folder in the Unity project • Share it through Dropbox and send me a link

2/10/2012