Editor de niveles (Parte 1)

Hoy empezaremos con el editor de niveles. Este editor nos permitirá crear las etapas del juego a partir de ficheros JSON guardados en la carpeta de recursos. Este editor nos permitirá generar información parar poder proseguir con el juego donde lo habíamos dejado.


En este artículo sólo me centraré en la creación de la pantalla visual del editor, poniendo atención en la creación automática de cuadrículas (grids) a partir de una rutina pasando el objeto casilla como un prefab de la aplicación y definiendo el tamaño de cuadrícula con un Box collider.



Podéis ver el vídeo del proceso aquí.


Gizmo del Box Collider:
 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 public class ShowColliders : MonoBehaviour {  
   private BoxCollider2D objectCollider2D;  
   private void OnDrawGizmos()  
   {  
     objectCollider2D = GetComponent<BoxCollider2D>();  
     Gizmos.color = Color.green;  
     Gizmos.DrawWireCube(objectCollider2D.bounds.center, new Vector2(objectCollider2D.size.x, objectCollider2D.size.y));  
   }  
 }  

Grid dinámico:
 using UnityEngine;  
 public class Grid : MonoBehaviour  
 {  
   //Grid  
   public int rows;  
   public int cols;    
   public Vector2 gridOffset;  
   public GameObject cell;  
   private BoxCollider2D gridSize;  
   private Vector2 originalCellSize;  
   private Vector2 cellScale;  
   private Vector2 cellSize;  
   private Vector2 offset;  
   // Start is called before the first frame update  
   void Start()  
   {  
     PaintCells();  
   }  
   public void Clean()  
   {  
     foreach (Transform child in gameObject.transform)  
     {  
       Destroy(child.gameObject);  
     }  
   }  
   public void PaintCells()  
   {  
     gridSize = GetComponent<BoxCollider2D>();  
     originalCellSize = cell.GetComponent<SpriteRenderer>().sprite.bounds.size;  
     cellSize = new Vector2(gridSize.size.x / (float)cols, gridSize.size.y / (float)rows);  
     cellScale.x = cellSize.x / (originalCellSize.x + gridOffset.x);  
     cellScale.y = cellSize.y / (originalCellSize.y + gridOffset.y);  
     cell.transform.localScale = new Vector2(cellScale.x, cellScale.y);  
     offset.x = gridSize.offset.x - ((gridSize.size.x) / 2 ) + cellSize.x / 2;  
     offset.y = gridSize.offset.y - ((gridSize.size.y) / 2 ) + cellSize.y / 2;  
     int sufixName = 0;  
     for (int row = 0; row < rows; row++)  
     {  
       for (int col = 0; col < cols; col++)  
       {  
         sufixName++;  
         //add the cell size so that no two cells will have the same x and y position  
         Vector2 pos = new Vector2(col * cellSize.x + transform.position.x + offset.x, row * cellSize.y + transform.position.y + offset.y);  
         //Add Cell information  
         cell.name = "Cell" + sufixName.ToString();  
         //instantiate the game object, at position pos, with rotation set to identity  
         GameObject cO = Instantiate(cell, pos, Quaternion.identity) as GameObject;  
         //set the parent of the cell to GRID so you can move the cells together with the grid;  
         cO.transform.parent = transform;  
       }  
     }      
   }  
 }  

El resto del editor sólo se utilizan elementos de la UI parar definir la interfaz de trabajo.

No hay comentarios:

Publicar un comentario

Como hacer copias de tu código de Unity con GitHub

Podriamos escribir un libro entero de las bondades de Git para el trabajo colaborativo y la gestión de versiones en un entorno como Unity. D...