C# 将随机游戏对象生成到设置的位置

C# 将随机游戏对象生成到设置的位置,c#,unity3d,C#,Unity3d,我不熟悉编码,答案可能很简单。 我正在unity中制作一个双人记忆游戏,我希望玩家每次加载游戏时随机生成卡片。我有一个游戏对象列表的卡和一个转换列表的产卵位置,我希望卡产卵。 到目前为止,我已经成功地将牌随机排列。但为了做到这一点,我已经将纸牌游戏对象列表转换为整数 public List<GameObject> cardsToSpawn; public List<Transform> cardPositions = new List<Transform>()

我不熟悉编码,答案可能很简单。 我正在unity中制作一个双人记忆游戏,我希望玩家每次加载游戏时随机生成卡片。我有一个游戏对象列表的卡和一个转换列表的产卵位置,我希望卡产卵。 到目前为止,我已经成功地将牌随机排列。但为了做到这一点,我已经将纸牌游戏对象列表转换为整数

public List<GameObject> cardsToSpawn;
public List<Transform> cardPositions = new List<Transform>();
public int cardCount;
public int someNum;
public List<GameObject> cards;
public GameObject empty;

void Awake()
{


    // list of the card gameobjects.
    List<int> cardsToSpawn = new List<int>();

        cardsToSpawn.Add(1);
        cardsToSpawn.Add(2);
        cardsToSpawn.Add(3);
        cardsToSpawn.Add(4);

        //Randomises the order. A, B, C, D = C, D, B, A for example.
        //cardsToSpawn is assigned a random number (1-18(length of cardsToSpawn list)).

    for (int i = 0; i < cardsToSpawn.Count; i++) 
      {
        int temp = cardsToSpawn[i];
        int randomIndex = Random.Range(i, cardsToSpawn.Count);
        cardsToSpawn[i] = cardsToSpawn[randomIndex];
        cardsToSpawn[randomIndex] = temp;

        //spawn card i=1 in cardPositions[1], card i=2 in cardPositions[2]... 
        temp = cards
        Instantiate(cardsToSpawn[i], cardPositions[i].position, transform.rotation);
        // but this refers back to the original list declared at the beginning of the script rather than the new randomIndex int.
      }
    // randomises the order. A, B, C, D = C, D, B, A for example.
    //tell me the order (i.e. make sure it is randomised.)
    cardsToSpawn.ForEach(i => Debug.Log("{0}\t"+ i));   


}
公共列表cardsToSpawn;
公共列表卡片位置=新列表();
公共积分卡计数;
公共int-someNum;
公众名单卡;
公共游戏对象为空;
无效唤醒()
{
//卡片游戏对象的列表。
List cardsToSpawn=新列表();
cardsToSpawn.Add(1);
cardsToSpawn.Add(2);
cardsToSpawn.Add(3);
添加(4);
//随机排列顺序。例如,A,B,C,D=C,D,B,A。
//cardsToSpawn被分配一个随机数(1-18(cardsToSpawn列表的长度))。
for(int i=0;iDebug.Log(“{0}\t”+i));
}

在您的
for
循环中,您正在生成一个新的
随机范围。。。这并不排除重复,另一方面也不保证所有的卡都被放置

您更愿意做的是将列表中的一个随机化(当然,您可以将两者随机化,但实际上这不会获得更多的随机性),然后迭代所有元素以放置它们

洗牌有多种选择。您可以使用like或(因为我现在更容易解释)所描述的用法

例如,喜欢

using System.Linq;

public List<GameObject> cardsToSpawn;
public List<Transform> cardPositions = new List<Transform>();

void Awake()
{
    // First make sure both lists have the same length
    if(cardsToSpawn.Count != cardPositions.Count) 
    {
        Debug.LogError("List length are different! Make sure you have the same amount of elements in both lists!");
        return;
    }

    // get the cards in randomized order
    Random rnd = new Random();
    // OrderBy returns an IEnumerable ordered by the given predicate
    // since our given predicate returns a random value each time
    // the elements get shuffled 
    var shuffledCards = cardsToSpawn.OrderBy(c => rnd.Next()).ToArray();    

    for (int i = 0; i < shuffledCards.Length; i++) 
    {
        // since the cards are already shuffled you can simply iterate
        // over this array in order now
        var card = shuffledCards[i];
        var position = cardPositions[i];

        Instantiate(card, position, transform.rotation);
    }
}
使用System.Linq;
公共列表cardsToSpawn;
公共列表卡片位置=新列表();
无效唤醒()
{
//首先确保两个列表的长度相同
if(cardsToSpawn.Count!=cardPositions.Count)
{
LogError(“列表长度不同!确保两个列表中的元素数量相同!”);
返回;
}
//把卡片按随机顺序拿出来
随机rnd=新随机();
//OrderBy返回由给定谓词排序的IEnumerable
//因为我们给定的谓词每次都返回一个随机值
//元素被洗牌
var shuffledCards=cardsToSpawn.OrderBy(c=>rnd.Next()).ToArray();
对于(int i=0;i