SE 350 – Programming Games
4K - views

SE 350 – Programming Games

Similar presentations


Download Presentation

SE 350 – Programming Games




Download Presentation - The PPT/PDF document "SE 350 – Programming Games" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.



Presentation on theme: "SE 350 – Programming Games"— Presentation transcript:

Slide1

SE 350 – Programming Games

Lecture 5: Programming with UnityLecturer: Gazihan Alankuş

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

2/10/2012

1

Slide2

Today

No quiz today! (Germany 1 – Turkey 0)Did you watch the C# videos?Feedback about last week’s visitGo over the example gamesCover most topics about scripting in Unity

2/10/20122

Slide3

Sample Games in Homework

URL was: http://u3d.as/content/m2h/c-game-examples/1sGWhat were the things that you found interesting?What have you learned?

What were your issues?Let’s quickly go over the gamesJump in when we are close to your question!The goal was to continue on, but we got stuck on this slide. We’ll continue with the rest next week.

2/10/2012

3

Slide4

The BEST Development Environment for Unity Scripting that I Know of

Visual Studio + Resharper for codingI asked for a classroom license. 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

4

Slide5

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/20125

Slide6

Variables with Class Types are References

Transform transform;GameObject myObject;

Structs do not work that wayVector3, Quaternion, etc.Vector3 v = new Vector3(0, 0, 1);Vector3 v1 = v;v1.x = 1;//

v.x == 0 still

2/10/2012

6

Slide7

Operator Overloading

Vector3 v3 = new Vector3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);Is equivalent toVector3 v3 = v1 + v2;0.5 * Vector3.upEtc.

2/10/20127

Slide8

Components and Scripts

Components are added to game objects and appear in the inspectorScripts (MonoBehavior) are custom components that you createpublic variables appear in the inspectorCan have functions for handling

GameObject’s events (Update, OnCollisionEnter, etc.)2/10/2012

8

Slide9

Game Objects and Components

Your code runs in the context of the componentYou can accessThe game object that it is attached to gameObjectOther 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.gameObjectOther game objects in the scene

GameObject.Find

(“Game object’s name”)

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

2/10/2012

9

Slide10

Accessing Things

2/10/201210

Cube

Transform

MyOtherScript

(

MonoBehavior

)

MyScript

(

MonoBehavior

)

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

Slide11

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

11

Slide12

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

You can access anythingYou can tell them to do anythingUse the inspector and hierarchy as starting points

2/10/201212

Slide13

Which Game Object Do You Attach Your Script to?

It only changesWhose components you want to access easilyWhose 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/201213

Slide14

Let’s do some examples

How do I reach this object?How do I tell him to do something?2/10/2012

14

Slide15

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 nameDon’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

15

Slide16

Connecting Objects in Awake()

Awake runs before anything else. So, it is the preferred place for setting up connectionsotherObject = 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

16

Slide17

Creating and Destroying Objects while the Game is Running

Instantiate(game object or prefab, position, rotation)Destroy(game object)2/10/2012

17

Slide18

Some Basic Data Types (

struct, not class)Vector3For positions and directionsQuaternion For orientations and rotations

2/10/201218

Slide19

Data Structures with Unity

Don’t underestimate. It’s C#! You can do anything!Classes, objects, referencesVariables, structsCollections ArrayList

, List<>, Dictionary<,>EnumsClasses in separate files or within other classesUse them to group variables togetherThis would help even if you don’t know Java:

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

2/10/2012

19

Slide20

Some Coding Demonstration

2/10/2012

20

Slide21

Debugging with

MonoDevelop while Coding in Visual Studio at the Same TimeDo this once after you created your Unity project

Edit->Preferences->External Tools is where you set the IDE that Unity will fireSelect Visual StudioDouble-click a script, this will create the Visual Studio projectSelect 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

MonoDevelop

2/10/2012

21

Slide22

How to go about working with code

Access what you are interested inEasy, just use the inspector as a guideGet the value, it will probably be an object of a classFor example: Vector3Read about it in the script reference

http://unity3d.com/support/documentation/ScriptReference/Also, search for random stuff in the script reference. It’s likely to lead in the right direction.

2/10/2012

22

Slide23

How to learn C#

Unity and C# tutorials in catlikecoding.com are goodhttp://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

23

Slide24

TODO:

ProjectsForm your groups and solidify project ideasSend

me a report of your game idea. It should contain: list of group members from this class + outside helpers (artists, etc.)brief

(not longer than one paragraph) description of your game storylonger

description of the gameplay, rules and how the game would be in general.

brief

plan on when to complete which

part

brief

division of

labor

2/10/2012

24