0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【スタジオしまづ】カードゲームの作り方を学んでみる #3 カードデータの生成

Last updated at Posted at 2020-04-29

カードゲームが作りたかったので【Unityゲームスタジオ スタジオしまづ】さんの動画を見ながら学習してみる

シャドバ風!?カードゲームの作り方 #3 カードデータの生成
https://youtu.be/9VtfajLzSrg

前回はこちら
【スタジオしまづ】カードゲームの作り方を学んでみる #2 カードの生成


カードを扱うスクリプト(CardController)を作成

動画時間0:40~
・ScriptsフォルダにC#スクリプトを作成し名前を「CardController」にする
CardControllerは
 見かけ(view)に関することを操作
 データ(model)に関することを操作
の2つを扱うがデータ(model)は別のクラスで実装する

カードデータのスクリプト(CardModel)を作成

動画時間2:15~
・ScriptsフォルダにC#スクリプトを作成し名前を「CardModel」にする
・カードのデータに必要な項目をpublicで定義してく
(後で追記します)

CardModel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// カードデータそのものとその処理
public class CardModel
{
    public string name;
    public int hp;
    public int at;
    public int cost;
    public Sprite icon;
}

・CardControllerに追記(後でまた追記します)

CardController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CardController : MonoBehaviour
{
    // データ(model)に関することを操作
    CardModel model;

    public void Init()
    {
        model = new CardModel();
    }
}

・プレハブのカードにCardControllerをアタッチする(プレハブを開くを押してから行うこと)
3-01.PNG

カードエンティティ(CardEntity)を作成

動画時間4:40~
カードの元々の情報を持つ外部データ(エクセル、csvなど)として「ScriptableObject」を使用する
ScriptableObject - Unity マニュアル
・ScriptsフォルダにC#スクリプトを作成し名前を「CardEntity」にする

CardEntity.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName ="CardEntity", menuName ="Create CardEntity")]
// カードデータそのもの
public class CardEntity : ScriptableObject
{
    public new string name;
    public int hp;
    public int at;
    public int cost;
    public Sprite icon;
}

CardEntityができたらUnityの作成メニューに「Create CardEntity」が出てくるようになり、簡単にカードデータを作成できる
3-02.PNG

最初のカード(Card1)を作成

動画時間7:20~
最初のカードを作る
3-03.PNG

Card1の情報を使ってゲーム内にカードを生成する

・Assetsにフォルダを作成し名前を「Resources」にする
・PrefabsフォルダをResources内に移動させる
・Resourcesにフォルダを作成し名前を「CardEntityList」にする
・Card1をCardEntityList内に移動させる
・CardModelを編集

CardModel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// カードデータそのものとその処理
public class CardModel
{
    public string name;
    public int hp;
    public int at;
    public int cost;
    public Sprite icon;

    public CardModel(int cardID)
    {
        CardEntity cardEntity = Resources.Load<CardEntity>("CardEntityList/Card"+cardID);
        name = cardEntity.name;
        hp = cardEntity.hp;
        at = cardEntity.at;
        cost = cardEntity.cost;
        icon = cardEntity.icon;
    }
}

・CardControllerを編集

CardController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CardController : MonoBehaviour
{
    // データ(model)に関することを操作
    CardModel model;

    public void Init(int cardID)
    {
        model = new CardModel(cardID);
    }
}

・GameManagerを編集

GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    [SerializeField] Transform playerHandTransform;
    [SerializeField] CardController cardPrefab;
    void Start()
    {
        CreateCard(playerHandTransform);
    }

    void CreateCard(Transform hand)
    {
        //カードの生成とデータの受け渡し
        CardController card = Instantiate(cardPrefab, hand, false);
        card.Init(1);
    }
}

cardPrefabの型を変えたせいでGameManagerのcardPrefabの指定が「なし」になっているので改めてPrefabsからCardを設定する

今回はここまで

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?