Search Unity

VERY basic script for creating a 2d tile grid

Discussion in 'Scripting' started by jdsmith2816, Dec 17, 2011.

  1. jdsmith2816

    jdsmith2816

    Joined:
    Dec 16, 2011
    Posts:
    1
    I created an article on the wiki that contains code for procedurally generating a mesh for use as a tile grid in 2D games.

    The code that's pasted directly into that page is fairly generic and at a good point for someone to break it apart and use it however they please but I thought it would be useful to also have a sample project that modifies that generic code a bit to actually make use of it.

    The attached unitypackage loads a json file containing a very simple 5x5 level definition and then paints the level onto the grid. I wouldn't necessarily suggest utilizing the level code in an actual project but it serves as a decent example.

    I'm not sure why but you can't attach files to the wiki thus.. the forum thread. I needed a place to stick the unitypackage :)

    Note that the included texture file is from the Sean Howard's CC licenced free pixel project.
     

    Attached Files:

    Marrt likes this.
  2. OhiraKyou

    OhiraKyou

    Joined:
    Mar 27, 2012
    Posts:
    259
    This is an old thread, but it's still very relevant, being the only example of its kind that I've been able to find despite much searching.

    However, I've hit a wall here, and reading through the code hasn't yielded anything which I would suspect to be the cause of the problem. Changing the level's size to a width and height that are not equal to each other (e.g. 5x6) results in each column being offset by the difference between the height and width. Everything works fine as long as the width and height are equal. Anyone have a solution or alternative?
     
  3. TheSpaceMan

    TheSpaceMan

    Joined:
    Nov 6, 2011
    Posts:
    153
    If the code uses arrays with linear data. The position of the data in the array would be x+y*width if you change the width of the level, but the code for reading the data holds an old width value you would get an offset. only reading 5 expecting the 6th to be on the next row.

    Validate that this is not the case if linear data arrays are used.
     
  4. OhiraKyou

    OhiraKyou

    Joined:
    Mar 27, 2012
    Posts:
    259
    The following is used to change the UV's:
    Code (csharp):
    1.         var mesh = GetComponent<MeshFilter>().mesh;
    2.         var uvs = mesh.uv;
    3.  
    4.         var tileSizeX = 1.0f / NumTilesX;
    5.         var tileSizeY = 1.0f / NumTilesY;
    6.        
    7.         uvs[(int)(_gridWidth * gridIndex.x + gridIndex.y) * 4 + 0] = new Vector2(tileIndex.x * tileSizeX, tileIndex.y * tileSizeY);
    8.         uvs[(int)(_gridWidth * gridIndex.x + gridIndex.y) * 4 + 1] = new Vector2((tileIndex.x + 1) * tileSizeX, tileIndex.y * tileSizeY);
    9.         uvs[(int)(_gridWidth * gridIndex.x + gridIndex.y) * 4 + 2] = new Vector2((tileIndex.x + 1) * tileSizeX, (tileIndex.y + 1) * tileSizeY);
    10.         uvs[(int)(_gridWidth * gridIndex.x + gridIndex.y) * 4 + 3] = new Vector2(tileIndex.x * tileSizeX, (tileIndex.y + 1) * tileSizeY);
    A quick Debug.Log() indicates that _gridWidth is 5, and _gridHeight is 6 (which is what I entered into the level1 text file). It's all in the OP's package. Screenshot of result using the same texture with the tile coords overlayed:

    $tiles.jpg