如何在Unity中制作无重复卡片的卡片组列表

时间:2019-12-13 10:00:45

标签: c# unity3d

我在Unity中的卡片组有一些问题。 我使用 AssetBundle 从目录中获取卡,并将它们放入 Array 中。然后使用循环,将卡从 array 添加到 CardList

在统一目录中,卡片顺序如下:

  

card_1,card_2,card_3,card_4 ...... card_52

这是此脚本的一部分:

public void InstanatiateDeck(string cardBundlePath)
{
    AssetBundle cardBundle = BundleSingleton.Instance.LoadBundle(DirectoryUtility.ExternalAssets() + cardBundlePath);
    string[] nameArray = cardBundle.GetAllAssetNames();
    for (int i = 0; i < nameArray.Length; ++i)
    {
        GameObject cardInstance = (GameObject)Instantiate(_cardPrefab);
        Card card = cardInstance.GetComponent<Card>();

        card.gameObject.name = Path.GetFileNameWithoutExtension(nameArray[i]);
        card.TexturePath = nameArray[ i ];
        card.SourceAssetBundlePath = cardBundlePath;
        card.transform.position = new Vector3(0, 10, 0);
        card.FaceValue = StringToFaceValue(card.gameObject.name);
        CardList.Add(card);
    }
}

但是使用此脚本,按 CardList 顺序排列的卡类似于目录顺序,如下所示:

  

元素0.card_1,元素1.card_2,元素2.card_3,元素3.card_4 ......元素51.card_52

如果在此脚本中添加Random.Range,我的 CardList

中将得到可重复的值
public void InstanatiateDeck(string cardBundlePath)
{
    AssetBundle cardBundle = BundleSingleton.Instance.LoadBundle(DirectoryUtility.ExternalAssets() + cardBundlePath);
    string[] nameArray = cardBundle.GetAllAssetNames();
    for (int i = 0; i < nameArray.Length; ++i)
    {
        GameObject cardInstance = (GameObject)Instantiate(_cardPrefab);
        Card card = cardInstance.GetComponent<Card>();
        int random = Random.Range(0, nameArray.Length);
        card.gameObject.name = Path.GetFileNameWithoutExtension(nameArray[random]);
        card.TexturePath = nameArray[ random ];
        card.SourceAssetBundlePath = cardBundlePath;
        card.transform.position = new Vector3(0, 10, 0);
        card.FaceValue = StringToFaceValue(card.gameObject.name);
        CardList.Add(card);
    }
}

如何修改脚本以按随机顺序将卡放入 CardList 中而没有可重复的值?

例如

  

元素0.card_7,元素1.card_50,元素2.card_24,元素3.card_12 ......元素51.card_9

谢谢大家,请原谅我可怜的英语

0 个答案:

没有答案