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 ). Outline. No quiz today! (next week) One hour of lecture Unity Scripting Last two hours
SE 350 – Programming Games
E N D
Presentation Transcript
SE 350 – Programming Games Lecture 4: Programming with Unity Lecturer: GazihanAlankuş 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 • We’ll visit the industrial design department
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
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)
Defining and using Defining a class Creating objects from the class and using them MyClass c = new MyClass(); c.AField = 3; c.AMethod(); MyClassd = new MyClass(); d.AField= 5; d.AMethod(); class MyClass { intAField; void AMethod() { } } c d AField AField 3 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
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
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
MonoBehavior Fields (automatic variablesin MyScript) Game Object Components Transform transform Cube Rigidbody rigidbody MyScript (MonoBehavior) gameObject
MonoBehavior Game Object Components Transform GetComponent<Transform>() transform Cube GetComponent(“Transform”) MyOtherScript (MonoBehavior) GetComponent<MyOtherScript>() GetComponent(“MyOtherScript”) MyScript (MonoBehavior) gameObject
MonoBehavior Game Object Components It’s like doing this in class: Transform transform; void Awake() { transform = GetComponent<Transform>(); } Transform GetComponent<Transform>() transform Cube GetComponent(“Transform”) MyOtherScript (MonoBehavior) GetComponent<MyOtherScript>() GetComponent(“MyOtherScript”) MyScript (MonoBehavior) gameObject
MonoBehavior It’s like doing this in class: Transform transform; void Awake() { transform = GetComponent<Transform>(); } Game Object Components Transform GetComponent<Transform>() transform Cube GetComponent(“Transform”) MyOtherScript (MonoBehavior) GetComponent<MyOtherScript>() GetComponent(“MyOtherScript”) MyScript (MonoBehavior) Similarly, you should do: MyOtherScriptmyOtherScript; void Awake() { myOtherScript = GetComponent<MyOtherScript>(); } Instead of calling GetComponent in Update() gameObject
Using Standard Components in Code • Don’t try to memorize anything. Just look at the inspector and you’ll figure it out.
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/ScriptReference/ • Also, search for random stuff in the script reference. It’s likely to lead in the right direction.
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.
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-Beginnersis not bad. If you don’t have enough time, watch 9, 14, 15, 21 • It’s fastest to learn from examples!
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
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