Grid.CreateGrid C# (CSharp) Method

CreateGrid() public method

public CreateGrid ( ) : void
return void
	void CreateGrid() 
	{
		grid = new Node[gridSizeX,gridSizeY];
		Vector2 transPos2D = myTransform.position;
		Vector2 worldBottomLeft = transPos2D - Vector2.right * gridWorldSize.x/2 - Vector2.up * gridWorldSize.y/2;

		for (int x = 0; x < gridSizeX; x ++) 
		{
			for (int y = 0; y < gridSizeY; y ++) 
			{
				Vector2 worldPoint = worldBottomLeft + Vector2.right * (x * nodeDiameter + nodeRadius) + Vector2.up * (y * nodeDiameter + nodeRadius);
				bool walkable = !(Physics2D.OverlapCircle(worldPoint, nodeRadius-nodeMargin, unwalkableMask));
				grid[x,y] = new Node(walkable,worldPoint, x,y);
			}
		}
	}

Usage Example

Example #1
0
    private void Awake()
    {
        level      = new byte[levelTexture.width, levelTexture.height];
        hexToIndex = new Dictionary <string, byte>();

        foreach (TileTypes t in tileTypes)
        {
            hexToIndex.Add(t.hex, t.index);
        }

        for (int x = 0; x < levelTexture.width; x++)
        {
            for (int y = 0; y < levelTexture.height; y++)
            {
                Color  color = levelTexture.GetPixel(x, y);
                string hex   = ColorUtility.ToHtmlStringRGB(color);
                byte   index = hexToIndex[hex];

                level[x, y] = index;
            }
        }

        for (int x = 0; x < levelTexture.width; x++)
        {
            for (int y = 0; y < levelTexture.height; y++)
            {
                byte       cell = level[x, y];
                GameObject obj;

                if (cell == 0) //isAir
                {
                    continue;
                }


                if (tileTypes[cell].isWall == true)
                {
                    if (tileTypes[level[x, y - 1]].isWalkable || tileTypes[level[x, y - 1]].index == 5)
                    {
                        obj = Instantiate(tileTypes[cell].prefab[0], new Vector3(x, y), Quaternion.identity);
                        obj.transform.parent = this.transform;
                    }
                    else
                    {
                        obj = Instantiate(tileTypes[cell].prefab[1], new Vector3(x, y), Quaternion.identity);
                        obj.transform.parent = this.transform;
                    }
                }
                else
                {
                    obj = Instantiate(tileTypes[cell].prefab[0], new Vector3(x, y), Quaternion.identity);
                    obj.transform.parent = this.transform;
                }
            }
        }

        grid.CreateGrid(level, levelTexture.width, levelTexture.height, tileTypes);
    }
All Usage Examples Of Grid::CreateGrid