Unity2D_Grids

m

GettingStarted

Type

Uniform

cell has the same shape and orientation

RectGrid

DiamondGrid

Hex

PointyHexGrid

FlatHexGrid

Spliced

cells may not have the same shape or orientation

Tri

PointyTriGrid

FlatTriGrid

Rhomb

PointyRhombGrid

FlatRhombGrid

CairoGrid

grids made from splicing the cells of a uniform grid into new, smaller cells

The original grid that was spliced is called the spliced grid’s base grid

Comp

Grid Point

r

This is what we use to access the contents of a particular cell. In general, a point can be anything. The built-in grids, however, are all accessed through grid points that look like integer coordinates. The coordinates of uniform grids work like vectors: you can add and subtract them, and scale them up or down (multiply or divide by an integer).The coordinates of spliced grids have two parts: a vector part (based on the base grid), and an index part, indicating the particular slice. We call these coordinates spliced vectors, and they follow a simple algebra. Below, N is the splice count, the number of cells each base cell grid has been spliced into.[x0, y0, i0] + [x1, y1] = [x0 + x1, y0 + y1, i0][x0, y0, i0] – [x1, y1] = [x0 - x1, y0 - y1, i0][x0, y0, i0] + [x1, y1, i1] = [x0 + x1, y0 + y1, (i0 + i1) % N][x0, y0, i0] – [x1, y1, i1] = [x0 - x1, y0 - y1, (i0 - i1) % N]

use to access the contents of a particular cell

In general, a point can be anything???

Map

r

A map is the thing that converts between Unity coordinates, and grid coordinates. Grids themselves do not know where they are in the Unity world. They only know which cells are inside them or not, and how cells relate to each other (being neighbors, for instance). The missing information is provided by maps. Keeping this information separate is extremely useful. For instance, hex grids and brick grids are topologically equivalent, which is a fancy way of saying their cells have the same neighbor relationships. They merely differ in the shape of the cells, and hence, how world and grid points map from one to the other. This means we can use the same grid, but different maps for the two.Maps are where the real power lies in the Grids library. They make it possible to do unusual things with grids that are not very easy with more rigid designs, including implementing pseudo irregular-grids, animating cells, transforming grids, and many more.Maps support index syntax. If you feed it a Unity vector, it gives you back a grid point; if you feed it a grid point, it gives you a unity vector.Maps supports several operations, including transformations, alignment functions, and anchoring functions. For a bit more detail, see Working with Maps.

A map is the thing that converts between Unity coordinates, and grid coordinates

Maps supports several operations, including transformations, alignment functions, and anchoring functions

Custom

r

You may see some of these classes in the folders with grids, points and vectors, and wonder what they are. These are behind-the-scene classes that implement the shape building technology for grids. For the most part, you do not need to know what these are unless you implement your own grids with shape building technology. They are the intermediary results of shape-building expressions like these:C#1234567PointyHexGrid<int> grid = PointyHexGrid .BeginShape() .Hexagon(5) .Translate(2, 2) .Union() .Hexagon(5) .EndShape();See Constructing Grids for more details on how to use these shape building functions.

Op

ShapeInfo

Project

Unity

Hiearchy(Editor)

(Scene)

ImageToGridd.unity

MainCamera

Grid

(comp)

Flat Hex Tile Grid Builder

ImageToGrid

(Texture)

WorldMap.tga

(config)

TextureType

Advanced

NonPowero2

None

Read/Write Enabled

true

//why ???

Import Type

Default

AlphaFromGrayscale

false

ByPass sRGB Sample

true

SpriteMode

none

GenerateMipMaps

false

WrapMode

clamp

FilterMode

Bilinear

AnisoLevel

1

(children)

...Grid Hex collections...

Project

GameLogic

Plugins

Grids

Unity

Editor

Editors

FlatHexTileGridEditor.cs

Scripts

(GridBehaviour)

ImageToGrid.cs

using

Gamelogic.Grids;

public class ImageToGrid : GridBehaviour<FlatHexPoint> { ... }

(var)

public

Texture2D

texture;

(func)

override

public

void

InitGrid(){ ... }

var

imageMap =

new FlatHexMap(Vector2.one)

.WithWindow(new Rect(0, 0, texture.width, texture.height))

.Stretch(Grid);

foreach

(var point in Grid){ ...}

var imageCoordinate = imageMap[point];

Grid[point].Color = texture.GetPixel((int) imageCoordinate.x, (int) imageCoordinate.y);

Init

1

define the grid structure.

2

Next, we need to define a map

3

The next step is to instantiate our cells, and put them in the right positions using our map.

4

To make our cells clickable, we need to process mouse inputs: