With C# or “JavaScript” Telerik School Academy Unity 2D Game Development.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Make swiftly iOS development Telerik Academy Telerik Academy Plus.
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Unleash the Power of JavaScript Tooling Telerik Software Academy End-to-end JavaScript Applications.
Touch and Gestures with Xamarin Forms
Telerik School Academy ASP.NET MVC.
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
C# Fundamentals – Part I
Telerik Software Academy Telerik School Academy Creating E/R Diagrams with SQL Server.
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Learning & Development Telerik Software Academy.
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Optimization problems, Greedy Algorithms, Optimal Substructure and Greedy choice Learning & Development Team Telerik Software.
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Creating E/R Diagrams with SQL Server Management Studio and MySQL Workbench Svetlin Nakov Telerik Software Academy Manager Technical.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Telerik Software Academy End-to-end JavaScript Applications.
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Course Overview Doncho Minkov Telerik Software Academy Technical Trainer
Subroutines in Computer Programming Telerik School Academy C# Fundamentals – Part 1.
Revealing the Secrets of Self-Documenting Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer.
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Building Rock-Solid Software Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Presentation transcript:

With C# or “JavaScript” Telerik School Academy Unity 2D Game Development

1. Creating and Using Scripts 2. Variables Sent To Inspector 3. Controlling Objects and Components 4. Input From Mouse, Touch, Keyboard 5. Game Loop and Events 6. Time Management 7. Creating and Destroying Game Objects 8. Coroutines 9. Saving and Loading 10. Good Practices 2

Add Script Components To Objects

 Scripts are just normal components added to objects  They are not built-in, you "code" them and then compile them  They give you more control  Can be written in C# (Mono) or "JavaScript" (Unity style)  Other.NET languages are compatible (sort of) 4

 To add new script to an object select it and click "Add Component"  Start writing the name of the script – ex. "PlayerController"  Or simply write click on any directory on the "Project Browser", create it from there and drag it to an object  You should forget most OOP principles from now on – Unity is component-based and has its own standards 5

 Double-clicking a script will open it in your IDE  There are couple of things to remember  All components inherit from MonoBehavior class  All components are using the UnityEngine namespace  Do not declare anything in namespaces (although Unity claims to support it)  Non-component classes (not attached to objects) do not need above requirements 6

 Start and Update functions are included  You may add FixedUpdate if using Physics 7 using UnityEngine; public class MainPlayer : MonoBehaviour { // Use this for initialization // Use this for initialization void Start () { void Start () { } // Update is called once per frame // Update is called once per frame void Update () { void Update () { }}

 Use Start method for initialization  Set needed variables  Do not initialize with constructors  Use the predefined methods - GetComponent  Use Update method for object update on every frame – read input, change parameters  Use FixedUpdate method for Physics update to Rigidbodies – add forces, colliders  Use Debug.Log for debugging purposes 8

 Sometimes you need to execute code on certain platforms:  More here: endentCompilation.html endentCompilation.html endentCompilation.html 9 #if UNITY_EDITOR Debug.Log("Unity Editor"); Debug.Log("Unity Editor"); #elif UNITY_IPHONE Debug.Log("Unity iPhone"); Debug.Log("Unity iPhone"); #elif UNITY_STANDALONE_WIN Debug.Log("Stand Alone Windows"); Debug.Log("Stand Alone Windows");#else Debug.Log("Any other platform"); Debug.Log("Any other platform");#endif

What is supported by the editor?

 It is common to use some sort of parameters to make the game work  Movement speed  Offsets  Texts  More  The easiest way is to set a public field  The editor will find it right away  You will be able to change the value run-time 11 private string myName;

 Which fields can be serialized:  Public or with [SerializeField] attribute  Non-static, non-constant, non-readonly  Which types can be serialized:  Primitive data types  Custom non abstract classes and structs with [Serializable] attribute  Children of UnityEngine.Object  Array or List of serializable types  Avoid serializing complex objects 12

The true power of Unity

 From code you can change all components' parameters to make the game change each frame  Additionally you can call methods, which are not available through the editor  Most common case  Declare private variable for the component in the class  Get the component in the Start method  Change it in the Update method 14

 Example: 15 using UnityEngine; public class MainPlayer : MonoBehaviour { private RigidBody2D rigidbody; private RigidBody2D rigidbody; void Start () { void Start () { this.rigidbody = this.GetComponent (); this.rigidbody = this.GetComponent (); } void Update () { void Update () { this.rigidbody.mass = 10f; this.rigidbody.mass = 10f; this.rigidbody.AddForce(Vector2.Up * 10f); this.rigidbody.AddForce(Vector2.Up * 10f); }}

 You can access other GameObjects  Add public GameObject variable  Drag'n'drop the object in the editor to assign it 16 using UnityEngine; public class MainPlayer : MonoBehaviour { public GameObject enemy; public GameObject enemy; void Start () { void Start () { this.transform.position = this.enemy.transform. position – Vector3.forward * 10f; this.transform.position = this.enemy.transform. position – Vector3.forward * 10f; }}

 You can access other GameObjects' components  Add public variable of the component  Drag'n'drop the object 17 using UnityEngine; public class MainPlayer : MonoBehaviour { private Transform enemy; private Transform enemy; void Start () { void Start () { this.transform.position = this.enemy. position – Vector3.forward * 10f; this.transform.position = this.enemy. position – Vector3.forward * 10f; }}

 You can find other objects by tag or name 18 using UnityEngine; public class MainPlayer : MonoBehaviour { private GameObject enemy; private GameObject enemy; private GameObject[] enemies; private GameObject[] enemies; void Start () { void Start () { this.enemy = GameObject.Find("Boss"); // name this.enemy = GameObject.Find("Boss"); // name this.enemy = GameObject.FindWithTag("Boss"); // tag this.enemy = GameObject.FindWithTag("Boss"); // tag this.enemies = GameObject. this.enemies = GameObject. FindGameObjectsWithTag("Enemy"); // collection FindGameObjectsWithTag("Enemy"); // collection }}

 Most important classes  MonoBehavior Behaviour.html Behaviour.html Behaviour.html  Transform form.html form.html form.html  Rigidbody2D body2D.html body2D.html body2D.html 19

Mouse, Keyboard, Touchscreen

 Input Input  All player input is accessed via the static Input class (do it in the Update)  You can read  Buttons  Input Axes (Horizontal, Vertical)  Mouse movements  Accelerometer, Gyroscope  Multi-touch presses on touch screens  Edit -> Project Settings -> Input 21

 You can use GetKey, GetKeyDown and GetKeyUp  All captured input is saved until the next Update call  This looks quite straightforward  But avoid using it – you are device dependent  Not all devices have the same buttons 22 if(Input.GetKeyDown(Keycode.Space)){ //Jump Code //Jump Code}

 Buttons are way more generic  They are not device dependent  For example "Fire1" button is for left-mouse click, tap on touchscreen and CTRL key on keyboards  Use GetButton, GetButtonDown and GetButtonUp 23 if(Input.GetKeyDown(Keycode.Space)){ //Jump Code //Jump Code}

 Input axes are mainly used for movement  Give you much smoother experience  "Horizontal" and "Vertical"  Usually they return values between -1 and 1  Depending on the direction  Depending on the time the button was pressed 24 var hor = Input.GetAxis("Horizontal") * speed; this.transform.Translate( new Vector3(hor, 0, 0) * Time.deltaTime);

 To get touchscreen information  Input.touches and Input.Touch structure  To get accelerometer information  Input.acceleration  To get location information  Input.location  To get mouse information  Input.mousePosition  More here: t.html t.html t.html 25

How The Game Is Running

 Game programming is usually rendering frame after frame  Before each frame different calculations are done  Object Positions and Rotations  Colliding  Physics  Unity fires different events before and after rendering the next frame 27

 Update  Called once per frame (not fixed time)  Executed before frames and animations  Perfect for inputs and getting different states of objects  FixedUpdate  Called every fixed time seconds – 0.02 default  Perfect for Physics Engine updates and code  LateUpdate  Called after all calculations  Perfect for code that you want to run after the frame is updated 28

 OnCollisionEnter2D, OnCollisionStay2D, OnCollistionExit2D  Called on every collision  Enter – when collision starts  Stay – when collision occurs  Exit – when collision ends  OnTriggerEnter2D, OnTriggerStay2D, OnTriggerExit2D  Fired only on trigger collisions 29

 Start  Called before the first frame of Physics update on the object  Awake  Called before the scene is loaded and before the Start is called  OnGUI  All GUI elements will respond to this event  OnMouseOver, OnMouseDown  When a object has a mouse over or clicked on 30

31

32

Time Is Money

 Since the Update function is not called on regular time intervals, you may get certain “lag”  If the framerate drops  Different CPU load  Consider the following code 34 public float distancePerFrame; void Update() { transform.Translate(distancePerFrame, 0, 0); transform.Translate(distancePerFrame, 0, 0);}

 If the framerate drop, you may see different “laggy” results in the gameplay  The object will move with irregular speed  Solution is to scale all time dependent vectors (for example movement) with the delta time  You can get it from Time.deltaTime field  Now the object’s speed will be constant 35 public float distancePerFrame; void Update() { transform.Translate(distancePerFrame, 0, 0) transform.Translate(distancePerFrame, 0, 0) * Time.deltaTime; * Time.deltaTime;}

Useful Techniques

 It is very common to create different objects run-time  Usually you should use prefabs  Instantiate method is used  Just drag’n’drop an object to the editor’s enemy field 37 public GameObject enemy; void Start() { for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) { Instantiate(enemy); Instantiate(enemy); }}

 There is also an Destroy method  You can set time after which the object is destroyed  Destroy can also destroy only component  The following will destroy the script 38 void OnCollisionEnter2D(Collision2D otherObj) { if (otherObj.gameObject.tag == "Missile") { if (otherObj.gameObject.tag == "Missile") { Destroy(gameObject,.5f); Destroy(gameObject,.5f); }} Destroy(this); Destroy(this);

Sometimes very useful!

 All functions are finished to the end before the next frame is updated  Because of this animations cannot be done directly with code  This function will not gradually fade the object – it will disappear directly 40 void Fade() { for (float f = 1f; f >= 0; f -= 0.1f) { for (float f = 1f; f >= 0; f -= 0.1f) { Color c = renderer.material.color; Color c = renderer.material.color; c.a = f; c.a = f; renderer.material.color = c; renderer.material.color = c; }}

 A coroutine will help you here  Coroutine is executed over sequence of frames instead of one  Do declare coroutine  The yielded value will pause until the next frame of the game 41 IEnumerator Fade() { for (float f = 1f; f >= 0; f -= 0.1f) { for (float f = 1f; f >= 0; f -= 0.1f) { Color c = renderer.material.color; Color c = renderer.material.color; c.a = f; c.a = f; renderer.material.color = c; renderer.material.color = c; yield return null; yield return null; }}

 Then all you need is to start the routine  You can also set time interval for the coroutine  You can use coroutines to reduce the checks in the Update method 42 void Update() { if (Input.GetKeyDown("f")) { if (Input.GetKeyDown("f")) { StartCoroutine("Fade"); StartCoroutine("Fade"); }} yield return new WaitForSeconds(.1f);

Application!

 To invoke a method after certain time  To load another scene  To quit the game  To pause the game set the Time.timeScale  And to resume set it to 1 44 Invoke("LoadLevel", 3f); Time.timeScale = 0; Application.LoadLevel("WinScene"); Application.Quit();

Persistence!

 The PlayerPrefs class have methods to save and load values  HasKey  SetInt and GetInt  SetFloat and GetFloat  SetString and GetString  For saving and loading the whole game: PlayerPrefs.SetInt("High Score", highscore); var highscore = PlayerPrefs.GetInt("High Score");

High Quality Code is important!

 Coding gives you good control of the game mechanics  Public fields are exported to the inspector  Use GetComponent to set different components  Use time management in your Update functions  How to use the Input class  Use coroutines where needed 48

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

 C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com