Download presentation
Presentation is loading. Please wait.
Published byMaryann Townsend Modified over 9 years ago
1
With C# or “JavaScript” Telerik School Academy http://academy.telerik.com Unity 2D Game Development
2
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
3
Add Script Components To Objects
4
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
5
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
6
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
7
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 () { }}
8
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
9
Sometimes you need to execute code on certain platforms: More here: http://docs.unity3d.com/Manual/PlatformDep endentCompilation.html http://docs.unity3d.com/Manual/PlatformDep endentCompilation.html http://docs.unity3d.com/Manual/PlatformDep 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
10
What is supported by the editor?
11
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;
12
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
13
The true power of Unity
14
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
15
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); }}
16
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; }}
17
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; }}
18
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 }}
19
Most important classes MonoBehavior http://docs.unity3d.com/ScriptReference/Mono Behaviour.html http://docs.unity3d.com/ScriptReference/Mono Behaviour.html http://docs.unity3d.com/ScriptReference/Mono Behaviour.html Transform http://docs.unity3d.com/ScriptReference/Trans form.html http://docs.unity3d.com/ScriptReference/Trans form.html http://docs.unity3d.com/ScriptReference/Trans form.html Rigidbody2D http://docs.unity3d.com/ScriptReference/Rigid body2D.html http://docs.unity3d.com/ScriptReference/Rigid body2D.html http://docs.unity3d.com/ScriptReference/Rigid body2D.html 19
20
Mouse, Keyboard, Touchscreen
21
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
22
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}
23
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}
24
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);
25
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: http://docs.unity3d.com/ScriptReference/Inpu t.html http://docs.unity3d.com/ScriptReference/Inpu t.html http://docs.unity3d.com/ScriptReference/Inpu t.html 25
26
How The Game Is Running
27
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
28
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
29
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
30
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
31
32
32
33
Time Is Money
34
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);}
35
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;}
36
Useful Techniques
37
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); }}
38
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);
39
Sometimes very useful!
40
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; }}
41
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; }}
42
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);
43
Application!
44
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();
45
Persistence!
46
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: http://goo.gl/3HZXZT http://goo.gl/3HZXZT 46 PlayerPrefs.SetInt("High Score", highscore); var highscore = PlayerPrefs.GetInt("High Score");
47
High Quality Code is important! http://goo.gl/xPcrAx
48
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
49
форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен 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# Николай Костов - блог за програмиране http://academy.telerik.com
50
C# Programming @ Telerik Academy csharpfundamentals.telerik.com csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com forums.academy.telerik.com
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.