在运行时使用数组中的字符串来改变SpriteRenderer
本文关键字:字符串 改变 SpriteRenderer 运行时 数组 | 更新日期: 2023-09-27 18:16:56
我正在尝试构建一个简单的小游戏/程序,查看两手牌并在多个级别上进行比较。我为玩家1和玩家2分别创建了5个预制件。他们的名字是玩家1牌1到玩家2牌5。
我还将所有精灵加载到一个数组中。它下面的字符串数组是牌组中每张牌的确切名称、顺序和大写字母。
我想做的基本上是告诉游戏在开始时将这些没有精灵的预制件更改为所选择的卡牌。当前系统的问题是,如果它删除列表中的项目#3并将数组缩短1,下一次滚动时(如果再次滚动3)它仍然会选择相同的图像。唯一减少的是数组末尾的牌。
我想做的是这样的:
GameObject。FindGameObjectWithTag("玩家2卡"+ _p2)。GetComponent()。sprite = CardNumberToDraw;
这样,它就会在那一行拉出条目,这样,如果它碰到相同的值2-10次,每次仍然是不同的牌。
在这一点上我完全被难住了。完整的代码如下,可以很好地处理索引整数:p。我知道这里很乱。我通常只是玩玩,直到一切正常,然后清理代码。那可能是个坏主意。
公共类GameController: MonoBehaviour{
// Use this for initialization
void Start ()
{
/*
* This loads all the art assetts we'll be using for the cards!
* */
Sprite[] CardSprites = Resources.LoadAll<Sprite> ("Card Sprites");
/*
* This is an array containing all the values of cards in poker.
* */
string[] TheNewDeck = new string[52] {
"2c", "2d", "2h", "2s", "3c", "3d", "3h", "3s", "4c","4d", "4h",
"4s", "5c", "5d", "5h", "5s", "6c", "6d", "6h", "6s", "7c", "7d",
"7h", "7s", "8c", "8d", "8h", "8s", "9c", "9d", "9h", "9s", "ac",
"ad", "ah", "as", "jc", "jd", "jh", "js", "kc", "kd", "kh", "ks",
"qc", "qd", "qh", "qs", "tc", "td", "th", "ts"
};
/*
* We'll use this deck so we can break it all we want and then refill it with the contents of the original.
* */
var TheDealingDeck = new ArrayList ();
for (int _i = 0; _i < TheNewDeck.Length; _i++) {
TheDealingDeck.Add (TheNewDeck [_i]);
//TheDealingDeck.Add (_i);
}
/*
* The next thing we need to do is create 2 arrays. One for player 1 and one for player 2.
* These are their hands.
* */
var PlayerOneHand = new ArrayList ();
var PlayerTwoHand = new ArrayList ();
/*
* Here we deal out a hand to the player.
* _p1 starts at one because the 1st card in your hand is Player 1 Card 1. Ends at Player 1 Card 5.
* */
for (int _p1 = 1; _p1 < 6; _p1++) {
var RandomRange = Random.Range (0, TheDealingDeck.Count - 1);
var CardNumberToDraw = System.Convert.ToInt16 (TheDealingDeck [RandomRange]);
GameObject.FindGameObjectWithTag ("Player 1 Card " + _p1).GetComponent<SpriteRenderer> ().sprite = CardSprites [CardNumberToDraw];
PlayerOneHand.Add (TheNewDeck [CardNumberToDraw]);
TheDealingDeck.RemoveAt (RandomRange);
}
for (int _p2 = 1; _p2 < 6; _p2++) {
var RandomRange = Random.Range (0, TheDealingDeck.Count - 1);
var CardNumberToDraw = System.Convert.ToInt16 (TheDealingDeck [RandomRange]);
GameObject.FindGameObjectWithTag ("Player 2 Card " + _p2).GetComponent<SpriteRenderer> ().sprite = CardSprites [CardNumberToDraw];
PlayerTwoHand.Add (TheNewDeck [CardNumberToDraw]);
TheDealingDeck.RemoveAt (RandomRange);
}
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyUp (KeyCode.Space)) {
Application.LoadLevel (Application.loadedLevelName);
}
}
}
我可以使用System.Convert将其转换为整数。ToInt16: D !
var RandomRange = Random.Range (0, TheDealingDeck.Count - 1);
var CardNumberToDraw = System.Convert.ToInt16 (TheDealingDeck [RandomRange]);
GameObject.FindGameObjectWithTag ("Player 1 Card " + _p1).GetComponent<SpriteRenderer> ().sprite = CardSprites [CardNumberToDraw];
PlayerOneHand.Add (TheNewDeck [CardNumberToDraw]);
TheDealingDeck.RemoveAt (RandomRange);
这(两个播放器)解决了我所有的问题,我没有看到任何错误。
谢谢!