unityでよく使う オブジェクトを生成するやり方を見ていきます。
unityのvrは、 2019.4を使用しています。
では今回はカードを生成していきます。
まず 用意するものは カードのオブジェクトです。
今回はトランプのクラブを使っています。
ちなみに トランプのカードはアセットストアで無料であります。
こちら

Free Playing Cards Pack | 3D Tools | Unity Asset Store
Elevate your workflow with the Free Playing Cards Pack asset from Game Asset Studio. Find this & other Tools options on the Unity Asset Store.
そして下準備として
ヒエラルキーで UIから キャンバス、パネルを入れて
カードのオブジェクト【プレハブにしておく】を入れて
親子関係にしておいてください。
キャンバスの下にパネルを入れて
パネルの下にオブジェクトを入れていきます。
これで準備の完了です。
次にスクリプトを作って、 以下のコードを記入します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardIn : MonoBehaviour
{
public GameObject Club;
public GameObject Panel;
void Start()
{
for(int i =0; i<5; i++)
{
GameObject cardObj = Instantiate(Club);
cardObj.transform.SetParent(Panel.transform);
}
}
// Update is called once per frame
void Update()
{
}
}
クラス名は CardInという名前にしています。
今回は Clubのカードを生成したいので Clubという名前です。
for文では 5回としていますが、 ここは好きな数字でOKです。
これによって、指定したパネルの上にカードを5枚
生成することができます。
生成するには、Instantiateを使うと覚えておけばOKです。
次にこのスクリプトを カードにアタッチして
インスペクターで
Clubの所に カードのオブジェクトをドラッグ
パネルの所に パネルをドラッグして入れてください。

これらを入れたら ゲームを再生してみましょう。
そしたら こんな感じにカードが生成されていると思います。
ヒエラルキーを確認して クローンが5枚 できていればOKです。

コメント