Вот
using System.Collections.Generic;
using UnityEngine;
public class Numbers : MonoBehaviour
{
public class Button
{
public bool opend;
public string contents;
}
private List<Button> _buttons = new List<Button>();
void Start()
{
for (int i = 0; i < 8; i++)
{
_buttons.Add(new Button { contents = i.ToString() });
}
for (int i = 0; i < 8; i++)
{
_buttons.Add(new Button { contents = (7-i).ToString() });
}
}
private Button _last = null;
private bool _win;
void OnGUI()
{
if (_win)
{
GUI.Label(new Rect(10,10,100,30), "You Win!" );
return;
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int index = i*4 + j;
var rect = new Rect(10 + i*60, 10 + j*60, 50, 50);
if (GUI.Button(rect, _buttons[index].opend ? _buttons[index].contents : "?"))
{
if (_buttons[index].opend) continue;
if(_last== null)
{
_buttons[index].opend = true;
_last = _buttons[index];
}
else if (_last.contents == _buttons[index].contents)
{
_last = null;
_buttons[index].opend = true;
_win = true;
foreach (var button in _buttons)
{
if(!button.opend)
{
_win = false;
break;
}
}
}
else
{
foreach (var button in _buttons)
{
button.opend = false;
}
_last = null;
}
}
}
}
}
}
UPD: Вариант с закрытием только последних кнопок
using System.Collections.Generic;
using UnityEngine;
public class Numbers : MonoBehaviour
{
public class Button
{
public bool opend;
public string contents;
}
private List<Button> _buttons = new List<Button>();
void Start()
{
for (int i = 0; i < 8; i++)
{
_buttons.Add(new Button { contents = i.ToString() });
}
for (int i = 0; i < 8; i++)
{
_buttons.Add(new Button { contents = (7-i).ToString() });
}
}
private Button _last = null;
private bool _win;
void OnGUI()
{
if (_win)
{
GUI.Label(new Rect(10,10,100,30), "You Win!" );
return;
}
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
int index = i*4 + j;
var rect = new Rect(10 + i*60, 10 + j*60, 50, 50);
if (GUI.Button(rect, _buttons[index].opend ? _buttons[index].contents : "?"))
{
if (_buttons[index].opend) continue;
if(_last== null)
{
_buttons[index].opend = true;
_last = _buttons[index];
}
else if (_last.contents == _buttons[index].contents)
{
_last = null;
_buttons[index].opend = true;
_win = true;
foreach (var button in _buttons)
{
if(!button.opend)
{
_win = false;
break;
}
}
}
else
{
_last.opend = false;
_last = null;
}
}
}
}
}
}