0
0

More than 1 year has passed since last update.

【テトリス作成①】ボード生成について深掘る

Last updated at Posted at 2022-11-14

こちらを元に解説しています。
https://qiita.com/hanatan079/items/3675855784e14acda119

★★★★★随時更新★★★★★
2022.11.22追記
2022.11.28追記
★★★★★★★★★★★★★★★

(見本)Board.cs

(変数)
    //ボード基盤用の四角枠格納用、Transformへ四角枠を格納する
        [SerializeField]
        private Transform emptySprite;

    //ボードの高さ,ボードの幅,ボードの高さ調整用数値
        [SerializeField]
        private int height = 30, width = 10, header = 8;
(関数)
    //ボード作成
        void CreateBoard()
        {
            if(emptySprite)
            {
    
                for (int y =0; y < height - header; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        Transform clone = Instantiate(emptySprite, new Vector3(x,y,0), Quaternion. identity);
                        clone.transform.parent = transform;
                    }
                }
            }
        }

(呼び出し)
        void Start()
        {
            CreateBoard();
        }

解説

【変数】

    //ボード基盤用の四角枠格納用、Transformへ四角枠を格納する
        ①[SerializeField]
        ②private ③Transform ④emptySprite;

    //ボードの高さ,ボードの幅,ボードの高さ調整用数値
        ①[SerializeField]
         ②private int height = 30, width = 10, header = 8;

①インスペクタから呼び出し可能な変数
②宣言したモジュールの中でのみ
③Transform内にボード基盤用の四角枠格納用
④名前
⑤高さ、横幅、ボードの高さ調整用数値を整数で指定

型についてまとめ記事 https://qiita.com/hanatan079/items/36386ae4335bb0b06c88

【関数】

 //ボード作成
        ①void CreateBoard()
        {
            ②if(emptySprite)
            {
                ③for (int y =0; y < height - header; y++)
                {
                    ④for (int x = 0; x < width; x++)
                    {
                        ⑤Transform clone = ⑥Instantiate(emptySprite, new Vector3(x,y,0), Quaternion. identity);
                        ⑦clone.transform.parent = transform;
                    }
                }
            }
        }

①何も返さない型、変数名
②四角枠に格納する条件
③y(上下(高さ))が0から始まり、height(30) - header(8)=(22)マス目までいくまで繰り返し処理する
④x(左右)が0マス目からwidth(10)の間で処理する
⑤Transform clone(子)
⑥emptySpriteをz(手前奥)は0の場所から回転をせずに生成する
⑦親の指定

void型について
https://qiita.com/hanatan079/items/36386ae4335bb0b06c88
for文についてまとめた記事
 https://qiita.com/hanatan079/items/01530bc45bab85f3d19f
Instantiate関数についてまとめ
 https://qiita.com/hanatan079/items/66a183ea5168c985c366
基本構文について
https://qiita.com/hanatan079/items/a109b40b0e159779590c
parentプロパティについて
https://qiita.com/hanatan079/items/9adbadebf4fcc867b182

【呼び出し】

 void Start()
        {
            CreateBoard();
        }

スタートメゾットで作成した関数を呼び出す

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0