Skip to content

Instantly share code, notes, and snippets.

@n-yoda
Last active December 20, 2015 01:19
Show Gist options
  • Save n-yoda/6047846 to your computer and use it in GitHub Desktop.
Save n-yoda/6047846 to your computer and use it in GitHub Desktop.
Unityで動くタッチザナンバーをLINQを乱用して書いてみた
using UnityEngine;
using System.Collections;
using System.Linq;
// タッチザナンバーをLINQを乱用して書いてみた.
// 数字が0から始まることに注意.
public class TouchTheNumberLINQ : MonoBehaviour
{
public int len = 5; //横方向のマス数=縦方向のマス数
Rect[] rects; //ボタンの位置がランダムに入っている
int next = 0; //次押すべき番号
void Awake ()
{
float size = Mathf.Min (Screen.width, Screen.height) / len;
//ボタンの描画位置
rects = Enumerable.Range (0, len)
.SelectMany (x => Enumerable.Range (0, len).Select (y => new Rect (x * size, y * size, size, size)))
.ToArray ();
Init ();
}
// 初期化(シャッフル)
void Init ()
{
next = 0;
// 並び替えのキーとして乱数を指定
rects = rects.OrderBy (r => Random.value).ToArray ();
}
// 次の番号〜終わりの中に、
// 「ボタンが押され、かつ、そのボタンが押すべきボタンであり、かつ、そのボタンが最後のボタン」
// であるものがひとつでもあれば、初期化
void OnGUI ()
{
if (Enumerable.Range (next, rects.Length - next)
.Any (i => GUI.Button (rects [i], i.ToString ()) && i == next && ++next >= rects.Length))
Init ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment