Herramientas de usuario

Herramientas del sitio


fw:unity3d:environtment2d

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
fw:unity3d:environtment2d [2014/04/04 21:48]
alfred [Colliders]
fw:unity3d:environtment2d [2020/05/09 09:25] (actual)
Línea 12: Línea 12:
   * To **add an sprite** add it to the folder using windows explorer. To add it to the scene drag it from Project window to hierarchy or Scene window.   * To **add an sprite** add it to the folder using windows explorer. To add it to the scene drag it from Project window to hierarchy or Scene window.
   * To **create multiple sprites from one** you need to change the sprite mode property from Single to Multiple. Then you can slice your sprite using the Sprite Editor tool.   * To **create multiple sprites from one** you need to change the sprite mode property from Single to Multiple. Then you can slice your sprite using the Sprite Editor tool.
 +  * If you drag a set of sprites to the scene, Unity will create an animation with them.
   * You can use a sorting layer for the background. It's composed by sub-layers which will be arranged and could be locked with the aim of not edit them by accident.   * You can use a sorting layer for the background. It's composed by sub-layers which will be arranged and could be locked with the aim of not edit them by accident.
 ===== Scripting ===== ===== Scripting =====
 ==== Basic ==== ==== Basic ====
 === Inherited functions === === Inherited functions ===
-  * ''​Awake()'',​ it's called while the script is loaded. Used to initialize instances. +  ​* Lifetime functions:​ 
-  * ''​Start()''​ +    ​* ''​Awake()'',​ it's called while the script is loaded. Used to initialize instances. At this point it can not interact with other GameObjects
-  * ''​Update()'',​ update funcition, one by frame. +    * ''​Start()''​, it's called after Awake(). The difference is that this method won't be called if the script is not enabled. 
-  * ''​FixedUpdate()'',​ update function to be used for physic operations. It will be called so many times as needed before drawing a frame.+    * ''​Update()'',​ update funcition, one by frame. 
 +    * ''​FixedUpdate()'',​ update function to be used for physic operations. It will be called so many times as needed before drawing a frame
 +    * ''​Destroy()'',​ called when the object is destroyed.
   * ''​LateUpdate()''​   * ''​LateUpdate()''​
   * ''​OnDisable()''​   * ''​OnDisable()''​
   * ''​OnGUI()''​   * ''​OnGUI()''​
 +
 +Properties
 +  * ''​transform'',​ contains the methods and objects to move the object.
  
 === Interact with objects === === Interact with objects ===
Línea 28: Línea 34:
   * ''​Instantiate(GameObject)''​   * ''​Instantiate(GameObject)''​
   * ''​Invoke(name,​ time)''​   * ''​Invoke(name,​ time)''​
-  * ''​GameObject.Find(name).GetComponent<​tag>​()''​+  * ''​GameObject.Find(name)''​ 
 +  * ''​gameObject.GetComponent<​typeOfComponent>​()'' ​gets the first component (script, whatever...) from a GameObject object. There is also ''​GetComponents<​type>​()''​ that retrieves a list.  The GameObject must be active. 
 +  * ''​GetComponentInChildren<​Type>​()'',​ retrieves from children object. 
 +  * ''​Destroy(gameObject,​ time)'',​ will destroy an instatiated prefab on 20secs. 
 +  * ''​Destroy(gameObject)'',​ destroys the object now. 
 +  * ''​SendMessage'',​ calls a method on each script which have it inside the GameObject. 
 +<code csharp>​ 
 +// Calls the function ApplyDamage with a value of 5 
 +gameObject.SendMessage ("​ApplyDamage",​ 5.0); 
 +...  
 +// Every script attached to the game object  
 +// that has an ApplyDamage function will be called. 
 +function ApplyDamage (damage : float) {... 
 +</​code>​ 
 + 
 +  * Propiedades:​ 
 +    * ''​gameObject.active'',​ to activate or deactivate the gameObject.
  
 === Input === === Input ===
   * ''​OnMouseDown()''​ function is called when there is a mouse click on a collider.   * ''​OnMouseDown()''​ function is called when there is a mouse click on a collider.
 +  * ''​Input.GetButtonDown(str)'',​ where the string is...
 +    * Fire1, control key.
 +    * Fire2, left mouse button.
  
 === Animations === === Animations ===
Línea 56: Línea 81:
   * ''​Debug.Log()'',​ shows a message in console.   * ''​Debug.Log()'',​ shows a message in console.
   * ''​Debug.Break()'',​ stops the game.   * ''​Debug.Break()'',​ stops the game.
 +
 +=== Camera ===
 +The static object ''​Camera.main''​ contains the main camera for the scene.
 +  * ''​ViewportToWorldPoint''​ and ''​WorldToViewportPoint''​ to transform coordinates.
  
 === Useful functions === === Useful functions ===
Línea 144: Línea 173:
 if(rb != null && rb.tag == "​Enemy"​) { if(rb != null && rb.tag == "​Enemy"​) {
   rb.gameObject.GetComponent<​Enemy>​().HP = 0;   rb.gameObject.GetComponent<​Enemy>​().HP = 0;
 +</​code>​
 +
 +=== Make a gameObject invisible ===
 +Disabling its MeshRenderer component...
 +<code csharp>
 +GetComponent(MeshRenderer).enabled = false;
 +</​code>​
 +
 +=== Make blink a gameObject ===
 +For a finite amount of time...
 +<code csharp>
 +StartCoroutine(Blink(2.0)); ​
 +function Blink(waitTime : float) {
 +    var endTime=Time.time + waitTime;
 +    while(Time.time<​waitTime){
 +        renderer.enabled = false;
 +        yield WaitForSeconds(0.2);​
 +        renderer.enabled = true;
 +        yield WaitForSeconds(0.2);​
 +    }
 +}
 +</​code>​
 +To make it blink always, immiately, and repeat every 0.4 seconds...
 +<code csharp>
 +InvokeRepeating("​Blink",​ 0, 0.4);
 +function Blink()
 +{
 +    renderer.enabled = false;
 +    yield WaitForSeconds(0.2);​
 +    renderer.enabled = true;
 +}
 </​code>​ </​code>​
 ==== Notes ==== ==== Notes ====
Línea 164: Línea 224:
 ==== Scripting physics ==== ==== Scripting physics ====
   * ''​rigidbody2D.velocity''​ returns the velocity vector from one rigidbody2D attached to a GameObject.   * ''​rigidbody2D.velocity''​ returns the velocity vector from one rigidbody2D attached to a GameObject.
 +
 +==== Physics functions ====
 +  * ''​OnCollisionEnter2D(CollisionInfo2D info)''​ when another collider is touching this object collider.
 +  * ''​OnCollisionExit2D(CollisionInfo2D info)''​ when another collider is not touching this object collider anymore.
 +  * ''​OnTriggerEnter2D(Collider2D otherCollider)''​ when another collider marked as a "​Trigger"​ is touching this object collider.
 +  * ''​OnTriggerExit2D(Collider2D otherCollider)''​ when another collider marked as a "​Trigger"​ is not touching this object collider anymore.
 ==== Colliders ==== ==== Colliders ====
   * When a collider is market as ''​Is Trigger''​ won't cause physical collisions, however it will call ''​OnTriggerEnter2D()''​ function.   * When a collider is market as ''​Is Trigger''​ won't cause physical collisions, however it will call ''​OnTriggerEnter2D()''​ function.
Línea 201: Línea 267:
 </​code>​ </​code>​
  
 +=== Move an element ===
 +<code csharp>
 +float inputX = Input.GetAxis("​Horizontal"​);​
 +float inputY = Input.GetAxis("​Vertical"​);​
 +movement = new Vector2(speed.x * inputX, speed.y * inputY);
 +</​code>​
 ===== Other stuff... ===== ===== Other stuff... =====
 ==== Working with mobile phones ==== ==== Working with mobile phones ====
Línea 208: Línea 280:
   * ''​Input.touchCount''​ tells the number of touches.   * ''​Input.touchCount''​ tells the number of touches.
   * ''​Input.GetTouch(0)''​ obtains the first touch which was done.   * ''​Input.GetTouch(0)''​ obtains the first touch which was done.
 +  * Sometimes you'll need to refresh the editor to see the changes, you can do it with ''​CTRL+R''​.
 +  * To focus a gameobject you'll do in the GameObject menu, option Align View to Selected.
  
 ==== GUI elements ==== ==== GUI elements ====
Línea 215: Línea 289:
 <code csharp> <code csharp>
 if (GUI.Button()) { if (GUI.Button()) {
 +</​code>​
 +To make a property which won't be shown in the editor:
 +<code csharp>
 +[HideInInspector]
 +public float mAngle = 0f;
 +</​code>​
 +To create a option menu you will add a class of the next style:
 +<code csharp>
 +using UnityEngine;​
 +using UnityEditor;​
 +using System.Collections;​
 +
 +public class FunWindow : EditorWindow
 +{
 + [MenuItem("​Tools/​Fun Window"​)]
 + public static void ShowWindow()
 + {
 + EditorWindow.GetWindow<​FunWindow>​("​Fun Window"​);​
 + }
 +
 + void OnGUI()
 + {
 + if(GUILayout.Button("​Clear PlayerPrefs"​))
 + {
 + PreferencesManager.DeleteAll();​
 + }
 +
 + if(GUILayout.Button("​Clear Store"​))
 + {
 + Unibiller.clearTransactions();​
 + }
 + }
 +}
 </​code>​ </​code>​
 ==== Coroutines ==== ==== Coroutines ====
fw/unity3d/environtment2d.1396648098.txt.gz · Última modificación: 2020/05/09 09:24 (editor externo)