1 / 18

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

rune
Download Presentation

SE 350 – Programming Games

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SE 350 – Programming Games Lecture 4: Programming with Unity Lecturer: GazihanAlankuş Please look at the last slide for assignments (marked with TODO)

  2. Outline • No quiz today! (next week) • One hour of lecture • Unity Scripting • Last two hours • We’ll visit the industrial design department

  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

  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)

  5. 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

  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

  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

  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

  9. MonoBehavior Fields (automatic variablesin MyScript) Game Object Components Transform transform Cube Rigidbody rigidbody MyScript (MonoBehavior) gameObject

  10. MonoBehavior Game Object Components Transform GetComponent<Transform>() transform Cube GetComponent(“Transform”) MyOtherScript (MonoBehavior) GetComponent<MyOtherScript>() GetComponent(“MyOtherScript”) MyScript (MonoBehavior) gameObject

  11. 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

  12. 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

  13. Using Standard Components in Code • Don’t try to memorize anything. Just look at the inspector and you’ll figure it out.

  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/ScriptReference/ • Also, search for random stuff in the script reference. It’s likely to lead in the right direction.

  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.

  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-Beginnersis not bad. If you don’t have enough time, watch 9, 14, 15, 21 • It’s fastest to learn from examples!

  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

  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

More Related