ゲーム開発編次回予告:p.192「壁のプレハブを作成する」から完成まで
※ 最終回は3Dゲーム開発サンプルを用いた演習の予定
文法編次回予告:ジェネリックコレクションクラス、インターフェイスなど
p.192 壁のプレハブを作成する
・①[Create]⇒「+」 ・②[2D Object][Sprite]⇒[2D Object][Sprites][Square] ・③の名前の変更は、戻ってしまうことがあり、⑤の後でリトライすると良い ・⑤「Add Component」「Physics 2D」「BoxCollider 2D」
p.193 スクリプトを使って壁を配置する
・①[Create]⇒「+」 ・③「Add Component」「New Script」「Manager」「Create and add」「:」「Edit Script」 ・⑥[Size]を「x:0.7」「y:0.16」にする
p.194-195 Manager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : MonoBehaviour {
public GameObject wallpfb; //壁のプレハブのパブリック変数
void Start() {
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 4; y++) {
Vector3 pos = new Vector3(); //方向オブジェクトを生成
pos.x = x * 1.2f - 2.4f; //X座標を横並びに設定
pos.y = 2.5f - y; //Y座標を縦並びに設定
Instantiate(wallpfb, pos, Quaternion.identity); //無回転
}
}
}
}
p.198 スクリプトを使ってWallを回転させる
・②[Open Prefab]⇒「Open」 ・③「Add Component」「New Script」「Wall」「Create and add」「:」「Edit Script」
p.201 Wall.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Wall : MonoBehaviour {
public bool clockwise = true; //時計回りを示すパブリック変数
void Update() {
if (clockwise) { //時計回りならば
transform.Rotate(new Vector3(0, 0, -5)); //Z方向に回転
} else { //時計回りでなければ
transform.Rotate(new Vector3(0, 0, 5)); //Z方向に回転
}
}
}
p.202 Manager.cs・改
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Manager : MonoBehaviour {
public GameObject wallpfb; //壁のプレハブのパブリック変数
void Start() {
for (int x = 0; x < 5; x++) {
for (int y = 0; y < 4; y++) {
Vector3 pos = new Vector3(); //方向オブジェクトを生成
pos.x = x * 1.2f - 2.4f; //X座標を横並びに設定
pos.y = 2.5f - y; //Y座標を縦並びに設定
GameObject obj = Instantiate(wallpfb, pos, Quaternion.identity); //無回転
Wall wall = obj.GetComponent<Wall>(); //壁のコンポーネントを得る
if (y % 2 == 0) { //偶数行目(0行目と2行目)?
wall.clockwise = true; //時計回りに
} else { //奇数行目(1行目と3行目)?
wall.clockwise = false; //反時計回りに
}
}
}
}
}
p.205 ゲーム画面の「底」を作成する
・p.205上①[Create]⇒「+」、[2D Object][Sprite]⇒[2D Object][Sprites][Square] ・p.205上②の名前の変更は、戻ってしまうことがあり、③の後でリトライすると良い ・p.205上③の後で、SpriteRndererのSpriteを「Background」にする(下の方にある) ・p.205下①「Add Component」「Physics 2D」「BoxCollider 2D」 ・p.206上①の実行後、画面上でCtrlキーを押しながらマウスのホイールを下回しすることで倍率を下げると横長のオブジェクトを確認できる ・p.206下①「Add Component」「New Script」「Bottom」「Create and add」「:」「Edit Script」
p.206 Bottom.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bottom : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other) { //衝突したら
Destroy(other.gameObject); //相手を消す
}
}
p.208 Unityスクリプトリファレンスでクラスを調べよう
・Unity2022.3バージョンの場合のアドレスは: https://docs.unity3d.com/ja/2022.3/ScriptReference/index.html ・利用開始前に、バージョン番号を合わせること(Unityはバージョンによる差異が多い)
p.211 Microsoft社のC#ドキュメント
・日本語版のアドレスは: https://learn.microsoft.com/ja-jp/dotnet/csharp/language-reference/ ・検索結果には、C#と共に.NETフレームワークを構成する言語であるVisual Basicの事柄も含まれるので注意