我在GabeObject []卡上有5张牌。我想将这些卡片随机播放并保存到GameObject [] cardshuffle。这段代码出了点问题。它洗牌同样的牌。我怎么能让它不再洗牌?谢谢。
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public GameObject[] cards;
public GameObject[] cardshuffle;
int i=0;
int j = 0;
int save=0;
int[] kayit = new int[10];
int kayit2;
public GameObject[] player1;
public GameObject[] player2;
public GameObject[] player3;
// Use this for initialization
void Start () {
kayit[0] = 20;
while(i<5)
{
save = Random.Range(0, 5);
for (int a = 0; a < 5; a++ )
{
if(kayit[a] == save)
{
kayit2 = save;
}
}
if (kayit2 != save)
{
cardshuffle[i] = cards[save];
}
else
i--;
kayit[i] = save;
i++;
}
while (cards[j] != null)
{
player1[j] = cards[j];
j++;
Debug.Log(j);
}
}
// Update is called once per frame
void Update () {
}
}
答案 0 :(得分:1)
好的,这要归功于Plastic Sturgeon。
using UnityEngine;
using System.Collections;
public class Main : MonoBehaviour {
public GameObject[] cards;
public GameObject[] tmp;
public GameObject[] player1;
public GameObject[] player2;
public GameObject[] player3;
// Use this for initialization
void Start () {
for (int t = 0; t < cards.Length; t++)
{
tmp[0] = cards[t];
int r = Random.Range(t, cards.Length);
cards[t] = cards[r];
cards[r] = tmp[0];
}
for(int i=0; i<cards.Length;i++)
{
player1[i] = cards[i];
}
}
// Update is called once per frame
void Update () {
}
}
答案 1 :(得分:0)
将int数组传递给此方法。它将洗牌阵列:
void Shuffle(int[] texts)
{
// Knuth shuffle algorithm :: courtesy of Wikipedia :)
for (int t = 0; t < texts.Length; t++)
{
int tmp = texts[t];
int r = Random.Range(t, texts.Length);
texts[t] = texts[r];
texts[r] = tmp;
}
}