Skip to content

Instantly share code, notes, and snippets.

@gin0606
Created June 21, 2012 14:59
Show Gist options
  • Save gin0606/2966243 to your computer and use it in GitHub Desktop.
Save gin0606/2966243 to your computer and use it in GitHub Desktop.
Unity入門的なノリで書いた。
using UnityEngine;
using System.Collections;
struct Panel
{
public Rect rect;
public bool pushed;
public int num;
}
public class TouchTheNumber : MonoBehaviour
{
private int nextNum = 0;
private Panel[,] panels;
// Use this for initialization
void Start ()
{
int len = 5;
int panelSize = Screen.width / len;
panels = new Panel[len, len];
int len_x = panels.GetLength (0);
int len_y = panels.GetLength (1);
for (int i = 0; i< len_x; i++) {
for (int j = 0; j< len_y; j++) {
panels [i, j].rect = new Rect (j * panelSize, i * panelSize, panelSize, panelSize);
panels [i, j].pushed = false;
panels [i, j].num = i * len_y + j;
}
}
buttonShuffle ();
}
void initPanels ()
{
int len_x = panels.GetLength (0);
int len_y = panels.GetLength (1);
for (int i = 0; i< len_x; i++) {
for (int j = 0; j < len_y; j++) {
panels [i, j].pushed = false;
}
}
}
void buttonShuffle ()
{
int len_x = panels.GetLength (0);
int len_y = panels.GetLength (1);
// shuffle!
for (int i = 0; i < len_x*len_y; i++) {
int random = Random.Range (0, len_x * len_y - i);
int x = random / len_x;
int y = random % len_y;
int tmp = panels [x, y].num;
int tmp2 = len_x * len_y - 1 - i;
int xx = tmp2 / len_x;
int yy = tmp2 % len_y;
panels [x, y].num = panels [xx, yy].num;
panels [xx, yy].num = tmp;
}
}
void OnGUI ()
{
int len_x = panels.GetLength (0);
int len_y = panels.GetLength (1);
for (int i = 0; i< len_x; i++) {
for (int j = 0; j< len_y; j++) {
if (!panels [i, j].pushed && GUI.Button (panels [i, j].rect, panels [i, j].num.ToString ())) {
if (panels [i, j].num == nextNum) {
panels [i, j].pushed = true;
nextNum++;
}
}
}
}
if (nextNum >= len_x * len_y) {
nextNum = 0;
initPanels ();
buttonShuffle ();
}
}
void Update ()
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment