Search Unity

[Tutorial] Making a Grid with a 2D Array

Discussion in 'Community Learning & Teaching' started by sniffle63, Feb 7, 2019.

  1. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    Hello guys, in this video i show you how to make a 2D array of floats and assign a random value to each index of the array, after that we take the 2D array of floats and draw a grid and assign a random color to it based off its value in the array

     
    Fredloire and barisaxo like this.
  2. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    Made a follow up video showing how to place tile edges!
     
    barisaxo likes this.
  3. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
  4. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
     
    khaled24 likes this.
  5. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
  6. NaughtyMoleGames

    NaughtyMoleGames

    Joined:
    Jan 25, 2018
    Posts:
    51
    How would we change the Vertical and Horizontal to work with Pixel Perfect Camera?
    Code (CSharp):
    1.  
    2. public Sprite tileSprite;
    3.     public float[,] Grid;
    4.     int Vertical, Horizontal, Columns, Rows;
    5.     float tileBoundsX = 0;
    6.     float tileBoundsY = 0;
    7.  
    8.     void Start()
    9.     {
    10.         PixelPerfectCamera cam = Camera.main.GetComponent<PixelPerfectCamera>();
    11.  
    12.         tileBoundsX = tileSprite.bounds.extents.x;
    13.         tileBoundsY = tileSprite.bounds.extents.y;
    14.  
    15.         Vertical = cam.refResolutionY/2 / cam.assetsPPU;
    16.         Horizontal = cam.refResolutionX/2 / cam.assetsPPU;
    17.         Columns = cam.refResolutionX / cam.assetsPPU;
    18.         Rows = cam.refResolutionY / cam.assetsPPU;
    19.         Grid = new float[Columns, Rows];
    20.  
    21.         for (int i = 0; i < Columns; i++)
    22.         {
    23.             for (int j = 0; j < Rows; j++)
    24.             {
    25.                 Grid[i, j] = Random.Range(0.0f, 1.0f);
    26.                 SpawnTile(i, j, Grid[i, j]);
    27.             }
    28.         }
    29.     }
    30.  
    31.     private void SpawnTile(int x, int y, float value)
    32.     {
    33.         GameObject g = new GameObject(x + ":" + y);
    34.         g.transform.position = new Vector3(x -(Horizontal - tileBoundsX) , y -(Vertical - tileBoundsY));
    35.         Debug.Log("TILE:" + g.transform.position);
    36.         var tile = g.AddComponent<SpriteRenderer>();
    37.         tile.sprite = tileSprite;
    38.         tile.color = new Color(value, value, value);
    39.     }
    40.  
    This leaves a tiny gap at the top and bottom of the screen...Any ideas?
     
    Last edited: Feb 27, 2019
  7. sniffle63

    sniffle63

    Joined:
    Aug 31, 2013
    Posts:
    365
    The easiest way is to change your screen size/tile size to fit the math :D
     
  8. elitglad

    elitglad

    Joined:
    Feb 16, 2020
    Posts:
    4
    Last edited: Feb 17, 2020
  9. suryakumara33

    suryakumara33

    Joined:
    Nov 28, 2019
    Posts:
    3
    Hello,
    I want my object moving on the 2d Array tiles and at some points, displaying the index of map (x,y).
    Do you have suggestion?
     
    quintus_hgv likes this.