Unity绘制GUI连连看(尚未完善效果和重置)

时间:2023-03-09 17:36:26
Unity绘制GUI连连看(尚未完善效果和重置)

Unity绘制GUI连连看(尚未完善效果和重置)

OneImage.cs

public class OneImage : MonoBehaviour
{
public int row, col;
public Rect rect;
public Texture texture;
public bool walkable = true;
public bool isinit = false;
public bool selected = false; public OneImage()
{ } public OneImage(Rect rect,int row,int col)
{
this.row = row;
this.col = col;
this.rect = rect;
this.walkable = true;
}
}
LinkLinkSee.cs

public class LinkLinkSee : MonoBehaviour
{
public int rows, cols;
public Texture black, wayTexture;
public Texture[] textures;
private OneImage[][] images; bool game = true;
int upStep = ; void Start()
{
StartCoroutine("GameInit");
} IEnumerator GameInit()
{
game = false;
rows += upStep;
cols += upStep; int realRows = rows + ;
int realCols = cols + ; float w = Screen.height / realRows;
float x = Screen.width / - ((w * realCols + realCols) / );
float y = Screen.height / - (w * realRows / ); images = new OneImage[realRows][];
for (int row = ; row < realRows; row++)
{
images[row] = new OneImage[realCols];
for (int col = ; col < realCols; col++)
{
images[row][col] = new OneImage(new Rect(x, y, w, w), row, col);
images[row][col].texture = black;
x += w + ;
}
x = Screen.width / - ((w * realCols + realCols) / );
y += w + ;
} for (int row = ; row < realRows - ; row++)
{
for (int col = ; col < realCols - ; col++)
{
if (images[row][col].isinit)
{
continue;
}
else
{
int textureIndex = Random.Range(, textures.Length);
images[row][col].texture = textures[textureIndex];
images[row][col].isinit = true;
images[row][col].walkable = false; int newRow = , newCol = ;
int max = rows * cols;
while (images[newRow][newCol].isinit)
{
newRow = Random.Range(row, realRows - );
newCol = Random.Range(, realCols - );
max--;
if (max <= )
break;
}
images[newRow][newCol].texture = textures[textureIndex];
images[newRow][newCol].isinit = true;
images[newRow][newCol].walkable = false;
yield return null;
}
}
}
game = true;
} void OnGUI()
{
if (game)
{
bool isOver = true;
for (int row = ; row < rows + ; row++)
{
for (int col = ; col < cols + ; col++)
{
OneImage img = images[row][col]; if (!img.walkable)
{
isOver = false;
} GUI.DrawTexture(img.rect, img.texture);
//GUI.Label(img.rect, img.walkable.ToString());
}
}
if (isOver)
{
game = false;
upStep++;
StartCoroutine("GameInit");
}
}
else
{
for (int row = ; row < rows + ; row++)
{
for (int col = ; col < cols + ; col++)
{
OneImage img = images[row][col];
GUI.DrawTexture(img.rect, img.texture);
}
}
}
} private OneImage ChoseFirst = new OneImage(), ChoseSecond = new OneImage();
List<OneImage> FWay;
List<OneImage> SWay; void Update()
{
if (Input.GetMouseButtonDown()&&game)
{
try
{
if (!ChoseFirst.selected)
{
ChoseFirst = GetChose(Input.mousePosition);
ChoseFirst.selected = true;
}
else
{
ChoseSecond = GetChose(Input.mousePosition);
if (CompareImg(ChoseFirst, ChoseSecond))
{
ChoseFirst.selected = false;
}
else
{
if (ChoseSecond.texture == ChoseFirst.texture)
{
FindWay(ChoseFirst, ChoseSecond);
ChoseFirst.selected = false;
}
else
{
ChoseFirst = ChoseSecond;
ChoseFirst.selected = true;
}
}
}
}
catch
{
ChoseFirst = new OneImage();
ChoseSecond = new OneImage();
}
}
} private OneImage GetChose(Vector2 mousePos)
{
mousePos.y = Screen.height - mousePos.y;
for (int i = ; i < rows + ; i++)
{
for (int j = ; j < cols + ; j++)
{
if (images[i][j].rect.Contains(mousePos))
{
return images[i][j];
}
}
}
return null;
} private void FindWay(OneImage F, OneImage S)
{
if (F.row == S.row && Mathf.Abs(F.col - S.col) == || F.col == S.col && Mathf.Abs(F.row - S.row) == )
{
F.texture = wayTexture;
F.walkable = true;
S.texture = wayTexture;
S.walkable = true;
}
else
{
FWay = GetSingleWay(F);
SWay = GetSingleWay(S); List<List<OneImage>> WayWay = new List<List<OneImage>>();
foreach (OneImage img in FWay)
{
List<OneImage> way = GetSingleWay(img);
foreach (OneImage one in way)
{
foreach (OneImage sone in SWay)
{
if (CompareImg(sone, one))
{
WayWay.Add(way);
break;
}
}
}
} if (WayWay.Count > )//找到的路线数量>0
{
F.texture = S.texture = this.wayTexture;
F.walkable = S.walkable = true;
}
}
} private List<OneImage> GetSingleWay(OneImage img)
{
List<OneImage> singleWay = new List<OneImage>();
for (int i = img.row; i >= ; i--)
{
if (CompareImg(images[i][img.col], img) || images[i][img.col].walkable)
{
singleWay.Add(images[i][img.col]);
}
else
break;
} for (int i = img.row; i < rows + ; i++)
{
if (CompareImg(images[i][img.col], img) || images[i][img.col].walkable)
{
singleWay.Add(images[i][img.col]);
}
else
break;
} for (int i = img.col; i >= ; i--)
{
if (CompareImg(images[img.row][i], img) || images[img.row][i].walkable)
{
singleWay.Add(images[img.row][i]);
}
else
break;
} for (int i = img.col; i < cols + ; i++)
{
if (CompareImg(images[img.row][i], img) || images[img.row][i].walkable)
{
singleWay.Add(images[img.row][i]);
}
else
break;
} return singleWay;
} private bool CompareImg(OneImage F, OneImage S)
{
if (F.row == S.row && F.col == S.col)
return true;
else
return false;
}
}

Unity绘制GUI连连看(尚未完善效果和重置)