3次元ライフゲーム

細胞の不思議 すべてはここからはじまる
 ついでのついでに3次元バージョンも作ってみた。


……が、パラメータの設定が微妙過ぎて何とも言えない。
(処理が激しく重いので注意!)


リセットボタンでリセット。
[←][→]キー、もしくは[A][D]で回転。


using UnityEngine;
using System.Collections;

public class ScGameManager : MonoBehaviour
{

    public GameObject Box;
    public Material OnMaterial;
    public Material OffMaterial;

    private int CellsSize = 30;
    private float WaitTime = 0.1f;

    private bool[, ,] Cells;
    private bool[, ,] Buff;
    private GameObject[, ,] Boxs;

    // Use this for initialization
    void Start()
    {
        this.Init();
        StartCoroutine("Runing");
    }

    // Update is called once per frame
    void Update()
    {

    }

    //走り出すアレ
    private IEnumerator Runing()
    {
        while (true)
        {
            yield return new WaitForSeconds(this.WaitTime);
            this.MakeNextGeneration();
        }
    }

    //初期化
    private void Init()
    {
        this.Cells = new bool[this.CellsSize, this.CellsSize, this.CellsSize];
        this.Buff = new bool[this.CellsSize, this.CellsSize, this.CellsSize];
        this.Boxs = new GameObject[this.CellsSize, this.CellsSize, this.CellsSize];

        //初期状態と配置の設定
        for (int x = 0; x < this.CellsSize; x++)
        {
            for (int y = 0; y < this.CellsSize; y++)
            {
                for (int z = 0; z < this.CellsSize; z++)
                {
                    this.Boxs[x, y, z] = Instantiate(this.Box);
                    this.Boxs[x, y, z].transform.position = new Vector3(x, y, z);
                    if (Random.Range(0, 2) == 0)
                    {
                        this.Cells[x, y, z] = true;
                        this.Boxs[x, y, z].GetComponent<MeshRenderer>().material = this.OnMaterial;
                    }
                    else
                    {
                        this.Cells[x, y, z] = false;
                        this.Boxs[x, y, z].GetComponent<MeshRenderer>().material = this.OffMaterial;
                    }
                }
            }
        }
    }

    //次世代を作成
    private void MakeNextGeneration()
    {
        int count;

        //次の状態をバッファに書き込む
        for (int x = 0; x < this.CellsSize; x++)
        {
            for (int y = 0; y < this.CellsSize; y++)
            {
                for (int z = 0; z < this.CellsSize; z++)
                {
                    count = this.CountCells(x, y, z);
                    this.CellRules(count, x, y, z);
                }
            }
        }

        //セルの更新・色の変更
        for (int x = 0; x < this.CellsSize; x++)
        {
            for (int y = 0; y < this.CellsSize; y++)
            {
                for (int z = 0; z < this.CellsSize; z++)
                {
                    this.Cells[x, y, z] = this.Buff[x, y, z];
                    if (this.Cells[x, y, z] == true)
                    {
                        this.Boxs[x, y, z].GetComponent<MeshRenderer>().material = this.OnMaterial;
                    }
                    else if (this.Cells[x, y, z] == false)
                    {
                        this.Boxs[x, y, z].GetComponent<MeshRenderer>().material = this.OffMaterial;
                    }
                }
            }
        }
    }

    //周囲の生存セルをカウント
    private int CountCells(int x, int y, int z)
    {
        int count = 0;

        for (int fx = -1; fx <= 1; fx++)
        {
            for (int fy = -1; fy <= 1; fy++)
            {
                for (int fz = -1; fz <= 1; fz++)
                {
                    if (!(fx == 0 && fy == 0 && fz == 0))
                    {
                        int px = x + fx;
                        int py = y + fy;
                        int pz = z + fz;

                        if (px < 0) px = this.CellsSize - 1;
                        else if (px >= this.CellsSize) px = 0;
                        if (py < 0) py = this.CellsSize - 1;
                        else if (py >= this.CellsSize) py = 0;
                        if (pz < 0) pz = this.CellsSize - 1;
                        else if (pz >= this.CellsSize) pz = 0;

                        if (this.Cells[px, py, pz] == true) count++;
                    }
                }
            }
        }

        return count;
    }

    //セルのルール
    private void CellRules(int count, int x, int y, int z)
    {
        switch (count)
        {
            case 0:
                this.Buff[x, y, z] = false;
                break;
            case 1:
                this.Buff[x, y, z] = false;
                break;
            case 2:
                this.Buff[x, y, z] = false;
                break;
            case 3:
                this.Buff[x, y, z] = false;
                break;
            case 4:
                this.Buff[x, y, z] = false;
                break;
            case 5:
                this.Buff[x, y, z] = true;
                break;
            case 6:
                this.Buff[x, y, z] = true;
                break;
            case 7:
                this.Buff[x, y, z] = true;
                break;
            case 8:
                this.Buff[x, y, z] = false;
                break;
            case 9:
                this.Buff[x, y, z] = false;
                break;
            case 10:
                this.Buff[x, y, z] = false;
                break;
            case 11:
                this.Buff[x, y, z] = false;
                break;
            case 12:
                this.Buff[x, y, z] = false;
                break;
            case 13:
                this.Buff[x, y, z] = false;
                break;
            case 14:
                this.Buff[x, y, z] = false;
                break;
            case 15:
                this.Buff[x, y, z] = false;
                break;
            case 16:
                this.Buff[x, y, z] = false;
                break;
            case 17:
                this.Buff[x, y, z] = false;
                break;
            case 18:
                this.Buff[x, y, z] = false;
                break;
            case 19:
                this.Buff[x, y, z] = false;
                break;
            case 20:
                this.Buff[x, y, z] = false;
                break;
            case 21:
                this.Buff[x, y, z] = false;
                break;
            case 22:
                this.Buff[x, y, z] = false;
                break;
            case 23:
                this.Buff[x, y, z] = false;
                break;
            case 24:
                this.Buff[x, y, z] = false;
                break;
            case 25:
                this.Buff[x, y, z] = false;
                break;
            case 26:
                this.Buff[x, y, z] = false;
                break;
        }
    }

    //リセットボタン
    void OnGUI()
    {
        Rect rect = new Rect(10, 10, 100, 50);
        bool click = GUI.Button(rect, "リセット");
        if (click == true)
        {
            clickinit();
        }
    }

    private void clickinit()
    {
        for (int x = 0; x < this.CellsSize; x++)
        {
            for (int y = 0; y < this.CellsSize; y++)
            {
                for (int z = 0; z < this.CellsSize; z++)
                {
                    this.Buff[x, y, z] = false;
                    if (Random.Range(0, 2) == 0)
                    {
                        this.Cells[x, y, z] = true;
                        this.Boxs[x, y, z].GetComponent<MeshRenderer>().material = this.OnMaterial;
                    }
                    else
                    {
                        this.Cells[x, y, z] = false;
                        this.Boxs[x, y, z].GetComponent<MeshRenderer>().material = this.OffMaterial;
                    }
                }
            }
        }
    }
}


 カメラの回転はどこかのサイトを参考にコピペしました。

using UnityEngine;
using System.Collections;

public class ScCamera : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        this.transform.Rotate(0, (Input.GetAxis("Horizontal") * 1), 0);
	}
}