SE 350 * Programming Games

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
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
2
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
3
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
4
Defining and using
Defining a class
class MyClass {
int AField;
void AMethod() {
}
}
c
d
AField
AField
3
5
2/10/2012
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();
5
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
6
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/Scrip
tReference/MonoBehaviour.html
2/10/2012
7
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
8
MonoBehavior
Game Object
Components
Transform
Fields (automatic variables
in MyScript)
gameObject
= Cube
transform
= Transform
rigidbody
= Rigidbody
transform
Cube
Rigidbody
rigidbody
gameObject
2/10/2012
MyScript
(MonoBe
havior)
9
MonoBehavior
Game Object
Components
Transform
GetComponent<Transform>()
transform
GetComponent(“Transform”)
MyOtherScript
(MonoBehavio
r)
Cube
GetComponent<MyOtherScript>()
GetComponent(“MyOtherScript”)
gameObject
2/10/2012
MyScript
(MonoBe
havior)
10
MonoBehavior
Game Object
Components
It’s like doing this in class:
Transform transform;
Transform
void Awake() {
transform = GetComponent<Transform>();
}
GetComponent<Transform>()
transform
GetComponent(“Transform”)
MyOtherScript
(MonoBehavio
r)
Cube
GetComponent<MyOtherScript>()
GetComponent(“MyOtherScript”)
gameObject
2/10/2012
MyScript
(MonoBe
havior)
11
MonoBehavior
Game Object
Components
It’s like doing this in class:
Transform transform;
Transform
void Awake() {
transform = GetComponent<Transform>();
}
GetComponent<Transform>()
transform
GetComponent(“Transform”)
MyOtherScript
(MonoBehavio
r)
Cube
GetComponent<MyOtherScript>()
GetComponent(“MyOtherScript”)
gameObject
MyScript
(MonoBe
havior)
Similarly, you should do:
MyOtherScript myOtherScript;
void Awake() {
myOtherScript = GetComponent<MyOtherScript>();
}
2/10/2012
12
Instead of calling GetComponent in Update()
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
13
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/ScriptRef
erence/
• Also, search for random stuff in the script
reference. It’s likely to lead in the right direction.
2/10/2012
14
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
15
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-FundamentalsDevelopment-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
16
Great Sample Code in the Asset Store
• http://u3d.as/content/m2h/c-gameexamples/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
17
TODO: Homework
• Use the five game examples from here (explained
in previous slide): http://u3d.as/content/m2h/cgame-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
18