这是我的卡牌(GameObject)
我将卡牌设置为预制体,并将标签设定为“untagged”,在每张卡牌生成时,我设定他们获得“shang”,“xia”,“zuo”,“you”这四个值,并将标签变为“clone”来区别新的卡牌,以此获得不同卡牌的不同值。
顺带一提,我将数值存储在脚本(CardDataBase)中。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CardDataBase : MonoBehaviour
{
public static List<Card> cardList = new List<Card>();
void Awake ()
{
cardList.Add(new Card(0, 3, 4, 4, 4));
cardList.Add(new Card(1, 4, 1, 5, 5));
cardList.Add(new Card(2, 4, 6, 5, 5));
cardList.Add(new Card(3, 4, 4, 1, 1));
cardList.Add(new Card(4, 2, 6, 1, 1));
cardList.Add(new Card(5, 5, 3, 5, 7));
cardList.Add(new Card(6, 8, 5, 7, 5));
cardList.Add(new Card(7, 3, 4, 5, 3));
cardList.Add(new Card(8, 8, 4, 4, 4));
cardList.Add(new Card(9, 5, 3, 6, 6));
cardList.Add(new Card(10, 6, 7, 6, 6));
cardList.Add(new Card(11, 6, 7, 10, 7));
cardList.Add(new Card(12, 5, 5, 5, 10));
cardList.Add(new Card(13, 3, 4, 8, 5));
cardList.Add(new Card(14, 4, 4, 4, 3));
cardList.Add(new Card(15, 3, 3, 2, 2));
cardList.Add(new Card(16, 10, 10, 5, 5));
cardList.Add(new Card(17, 5, 6, 5, 9));
cardList.Add(new Card(18, 2, 7, 3, 3));
cardList.Add(new Card(19, 2, 2, 3, 3));
cardList.Add(new Card(20, 5, 5, 5, 5));
cardList.Add(new Card(21, 7, 6, 6, 6));
}
}
以下是卡牌上的脚本(ThisCard)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ThisCard : MonoBehaviour
{
public List thisCard = new List();
public int thisId;
public int id;
public int shang;
public int xia;
public int zuo;
public int you;
public int num;
void Start()
{
thisCard[0] = CardDataBase.cardList[thisId];
num = CreatCard.decksize;
}
void Update()
{
id = thisCard[0].id;
shang= thisCard[0].shang;
xia = thisCard[0].xia;
zuo = thisCard[0].zuo;
you = thisCard[0].you;
if (this.tag == "Clone")
{
thisCard[0] = CreatCard.staticDeck[num - 1];
num -= 1;
CreatCard.decksize -= 1;
this.tag = "Untagged";
}
}
}
以下是我生成卡牌的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreatCard : MonoBehaviour
{
public List deck = new List();
public static List staticDeck = new List();
[SerializeField] private GameObject CardPrefab;
[SerializeField] private Sprite[] CardImage;
private int CardsNum = 0;
public int x;
public static int decksize;
bool exist;
int[] wyf = { 22, 22, 22, 22, 22};
void Start()
{
CardsNum = 0;
int[] wyf = { 22, 22, 22, 22, 22 };
decksize = 5;
x = 0;
while (CardsNum < decksize)
{
x = Random.Range(0, 22);
exist = ((IList)wyf).Contains(x);
while (exist)
{
x = Random.Range(0, 22);
exist = ((IList)wyf).Contains(x);
}
deck[CardsNum] = CardDataBase.cardList[x];
GameObject newCard = Instantiate(CardPrefab);
newCard.transform.position =
new Vector3(CardsNum * 208 - 415, -796, -2);
newCard.GetComponent<SpriteRenderer>().sprite =
CardImage[x];
wyf[CardsNum] = x;
++CardsNum;
}
}
void Update()
{
staticDeck = deck;
}
public void Shuffle()
{
var cube = GameObject.Find("Card(Clone)");
while (cube)
{
DestroyImmediate(cube);
cube = GameObject.Find("Card(Clone)");
}
Start();
}
}
然后,我点击运行,并获得了五张牌
但是!
如图所示,
所有牌的四个目标值都是一样的,这是为什么呢,如何才能让他们获得对应的值呢?