1

I have some problem with card deck in Unity. I get card from directory using AssetBundle and put them in Array. Then using cycle I add card from array to CardList.

In the unity directory Cards order like this:

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

Here is part of this script:

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);
    }
}

But using this script the cards in CardList order similarly directory order like this:

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

If I add Random.Range in this script, I get repeatable values in my 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);
    }
}

How can modify script to put cards in CardList by random order without repeatable values?

for example

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

Thanks guys and excuse my poor English

Axel Vipovski
  • 95
  • 1
  • 7

0 Answers0