1
1

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 5 years have passed since last update.

Unity カードの生成と反映方法

1
Last updated at Posted at 2019-04-03
Card.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Card : MonoBehaviour {
	public Text hpText;
	int hp;
	string name;

	public void Load(CardData _cardData){
		hp = _cardData.hp;
		hpText.text = hp.ToString();
		name = _cardData.name;
	}
}

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

public class CardGenerater : MonoBehaviour {
	public GameObject cardPrefab;
	public GameObject hand;
	// Use this for initialization
	List<CardData> cardDataList = new List<CardData>(){
		new CardData(1, "スタジオしまづ", 1),
		new CardData(2, "マリオネットAI", 10),
		new CardData(3, "コマンドクラフト", 5),
	};

	void Start () {
		for(int i=0; i<cardDataList.Count; i++){
			GameObject cardObj = Instantiate(cardPrefab);
			cardObj.name = cardDataList[i].name;
			cardObj.transform.SetParent(hand.transform);

			Card card = cardObj.GetComponent<Card>();
			card.Load(cardDataList[i]);
		}
	}
		
}
CardData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CardData {
	public int id;
	public string name;
	public int hp;

	public CardData(int _id, string _name, int _hp){
		id = _id;
		name = _name;
		hp = _hp;
	}
}

スタジオしまづでゲームの作り方を学びたい人向けのサロン▶︎https://camp-fire.jp/projects/view/149191

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?