While it is possible to raycast and check texture color, typically this kind of problem is solved by building data structures, and checking data. Here is an example script. This script would go on each tile:
using UnityEngine;
using System.Collections;
public class ColorMatch : MonoBehaviour {
public enum Colors {
RED,
GREEN,
BLUE,
YELLOW,
WHITE,
BLACK,
GREY,
PURPLE,
ORANGE,
}
public Colors top;
public Colors right;
public Colors bottom;
public Colors left;
public ColorMatch cmTop;
public ColorMatch cmRight;
public ColorMatch cmBottom;
public ColorMatch cmLeft;
void OnMouseDown() {
Debug.Log ("Here");
if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) {
transform.Rotate (180,0,0);
Colors tmp = top;
top = bottom;
bottom = tmp;
}
else {
transform.Rotate (0,180,0);
Colors tmp = right;
right = left;
left = tmp;
}
CheckColors();
}
void CheckColors() {
if (cmTop != null && cmTop.bottom == top) {
Debug.Log ("Top color matches");
}
if (cmRight != null && cmRight.left == right) {
Debug.Log ("Right color matches");
}
if (cmBottom != null && cmBottom.top == bottom) {
Debug.Log ("Bottom color matches");
}
if (cmLeft != null && cmLeft.right == left) {
Debug.Log ("Left color matches");
}
}
}
After you attach this script to a tile, you will see in the inspector that top, right, bottom, and left are all assigned the color RED. You need to pick the appropriate color for each side of the tile. If you are going to have a bunch of the same tile, it would pay to do a prefab and therefore only have to assign the colors one for each tile. You can edit the Colors enum to only contain the colors used on your tiles.
cmTop, cmRight, cmBottom, and cmLeft are references to the ColorMatch scripts of the adjacent tiles. If there is no adjacent tile, the value is left to ‘None’ which is null. You can do this by drag and drop. Layout a pattern of tiles. Name the game objects in the hierarchy so you know by their names their position, select a tile, and then drag and drop the appropriate tile into the appropriate slot. For example say you had a layout of four tiles, the top left tile would have a neighbor to the right and a neighbor below. You would select the tile and drag the bottom left tile into the cmBottom slot and the top right tile into the cmRight slot.
All this dragging and dropping can be time consuming and error prone. After you verify the logic by setting up a 2x2 by hand, I’d work on a way to automatically do the layout and assignment of the cmTop, cmRight, cmBottom, and cmLeft variables.
The script just has something simple for the flipping. Shift-click rotate the tile around the horizontal axis, and click rotates it around the vertical. Note how the value of ‘top’ and ‘bottom’ are switched when the tile is rotated horizontally, and the swap of ‘left’ and ‘right’ for a vertical rotation. This means that top, right, bottom and left will always have the correct “world” values no matter how the tile is flipped.
The CheckColors() function is pretty straight forward. For example, you check to see if there is a tile above you, check to see if cmTop is not null. If it is not, you check this tile’s ‘top’ against the bottom of the tile above.
Whew, it took a lot longer to explain it then it did to write the code. This should get you started.