Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Assertions;
  5.  
  6. public class Warp : MonoBehaviour
  7. {
  8. // Para almacenar el punto de destino
  9. public GameObject target;
  10.  
  11. // Para almacenar el mapa de destino
  12. public GameObject targetMap;
  13.  
  14.  
  15. // Para controlar si empieza o no la transición
  16. bool start = false;
  17. // Para controlar si la transición es de entrada o salida
  18. bool isFadeIn = false;
  19. // Opacidad inicial del cuadrado de transición
  20. float alpha = 0;
  21. // Transición de 1 segundo
  22. float fadeTime = 1f;
  23.  
  24.  
  25. GameObject area;
  26.  
  27.  
  28. void Awake ()
  29. {
  30. // Nos aseguraremos de que target se ha establecido o lanzaremos except
  31. Assert.IsNotNull(target);
  32.  
  33. // Si queremos podemos esconder el debug de los Warps
  34. GetComponent<SpriteRenderer> ().enabled = false;
  35. transform.GetChild (0).GetComponent<SpriteRenderer> ().enabled = false;
  36.  
  37. Assert.IsNotNull(targetMap);
  38.  
  39. // Buscamos el Area para mostrar el texto
  40. area = GameObject.FindGameObjectWithTag("Area");
  41.  
  42. }
  43.  
  44. IEnumerator OnTriggerEnter2D (Collider2D col) {
  45.  
  46. // Comprobación para cambiar entre escenarios
  47. if (col.tag == "Player"){
  48.  
  49. col.GetComponent<Animator> ().enabled = false;
  50. col.GetComponent<Player> ().enabled = false;
  51. FadeIn();
  52.  
  53. yield return new WaitForSeconds(fadeTime);
  54.  
  55. // Actualizamos la posición
  56. col.transform.position = target.transform.GetChild (0).transform.position;
  57.  
  58. FadeOut();
  59. col.GetComponent<Animator> ().enabled = true;
  60. col.GetComponent<Player> ().enabled = true;
  61.  
  62. StartCoroutine(area.GetComponent<Area>().ShowArea(targetMap.name));
  63.  
  64. // Actualizamos la cámara
  65. Camera.main.GetComponent<MainCamera>().SetBound(targetMap);
  66.  
  67. }
  68.  
  69. }
  70.  
  71. // Dibujaremos un cuadrado con opacidad encima de la pantalla simulando una transición
  72. void OnGUI () {
  73.  
  74. // Si no empieza la transición salimos del evento directamente
  75. if (!start)
  76. return;
  77.  
  78. // Si ha empezamos creamos un color con una opacidad inicial a 0
  79. GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
  80.  
  81. // Creamos una textura temporal para rellenar la pantalla
  82. Texture2D tex;
  83. tex = new Texture2D (1, 1);
  84. tex.SetPixel (0, 0, Color.black);
  85. tex.Apply ();
  86.  
  87. // Dibujamos la textura sobre toda la pantalla
  88. GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), tex);
  89.  
  90. // Controlamos la transparencia
  91. if (isFadeIn) {
  92. // Si es la de aparecer le sumamos opacidad
  93. alpha = Mathf.Lerp (alpha, 1.1f, fadeTime * Time.deltaTime);
  94. } else {
  95. // Si es la de desaparecer le restamos opacidad
  96. alpha = Mathf.Lerp (alpha, -0.1f, fadeTime * Time.deltaTime);
  97. // Si la opacidad llega a 0 desactivamos la transición
  98. if (alpha < 0) start = false;
  99. }
  100.  
  101. }
  102.  
  103. // Método para activar la transición de entrada
  104. void FadeIn () {
  105. start = true;
  106. isFadeIn = true;
  107. }
  108.  
  109. // Método para activar la transición de salida
  110. void FadeOut () {
  111. isFadeIn = false;
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement