【发布时间】:2021-02-06 20:59:31
【问题描述】:
我不熟悉统一和基于组件的编程。我还没有做任何视觉上的事情(动画、图形等),只是逻辑。
我正在创建一个独特的纸牌游戏作为爱好,需要一些关于列表的帮助。我为 Hierarchy 中的 PlayerDeck 游戏对象创建了一个 PlayerDeck 类,该类从数据库加载、洗牌和发牌给四个玩家。代码如下:
public class PlayerDeck : MonoBehaviour
{
public int d;
public int w;
public int x;
public int y;
public int z;
public List<Card> deck;
public List<Card> container;
public List<Card> player1hand;
public List<Card> player2hand;
public List<Card> player3hand;
public List<Card> player4hand;
public List<Card> distributed;
public List<Card> discarded;
public int decksize;
void Start()
{
LoadVariables();
LoadDeck();
ShuffleDeck();
DealCards();
}
public void LoadVariables()
{
d = 0;
w = 0;
x = 0;
y = 0;
z = 0;
decksize = 64;
}
public void LoadDeck()
{
for (int i = 0; i < 64; i++)
{
d = i;
deck[d] = CardDatabase.cardList;
}
}
public void ShuffleDeck()
{
for (int i=0; i<decksize; i++)
{
container[0] = deck;
int rnd = Random.Range(i, decksize);
deck = deck[rnd];
deck[rnd] = container[0];
}
}
public void DealCards()
{
// deal to player one
for (int i = 0; i < 7; i++)
{
w = i;
player1hand[w] = deck;
distributed = player1hand[w];
deck.Remove(deck);
}
//deal to player two
for (int i=7; i < 14; i++)
{
player2hand[x] = deck;
distributed = player2hand[x];
deck.Remove(deck);
x++;
}
//deal to player three
for (int i = 14; i < 21; i++)
{
player3hand[y] = deck;
distributed = player3hand[y];
deck.Remove(deck);
y++;
}
//deal to player four
for (int i = 21; i < 28; i++)
{
player4hand[z] = deck;
distributed = player4hand[z];
deck.Remove(deck);
z++;
}
}
}
到目前为止,逻辑似乎是合法的。我对其进行了调试,我可以看到每个玩家都从牌组中获得了正确的牌。 现在,我创建了一个名为 Player1 的游戏对象,并希望创建一个类,其中 Player1 可以访问 Player Deck 中的 player1hand 列表。基本上,如何在代码中将此信息传输到 Player1?这就是我所做的,但我得到了玩家 1 的空白:
[RequireComponent(typeof (PlayerDeck))]
public class PlayerHand : MonoBehaviour
{
PlayerDeck playerhand;
public List<Card> dealthand;
public int a;
private void Start()
{
playerhand = GetComponent<PlayerDeck>();
for (int i = 0; i < 7; i++)
{
a = 0;
dealthand[a] = playerhand.player1hand;
a++;
}
}
有什么想法吗?任何帮助将不胜感激。
【问题讨论】:
-
你不能从检查器注入/链接这两个组件吗?尝试将
PlayerDeck playerhand设为 public 或添加[SerializeField]属性,如果它没有显示在检查器的 PlayerHand 组件上。你的缩进也是这样吗?