Skip to content

Instantly share code, notes, and snippets.

@naojitaniguchi
Created October 8, 2016 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naojitaniguchi/95e6ce669d5558321896c64d31c316e6 to your computer and use it in GitHub Desktop.
Save naojitaniguchi/95e6ce669d5558321896c64d31c316e6 to your computer and use it in GitHub Desktop.
Shuffling array sample for Unity
using UnityEngine;
using System.Collections;
public class Shuffle : MonoBehaviour {
public int[] blockNums;
int[] blockIndexArray ;
// Use this for initialization
void setupArray(){
int total = 0 ;
for ( int i = 0 ; i < blockNums.Length ; i ++ ){
total += blockNums[i] ;
}
blockIndexArray = new int[total];
int count = 0 ;
for ( int i = 0 ; i < blockNums.Length ; i ++ ){
for ( int j = 0 ; j < blockNums[i] ; j ++ ){
blockIndexArray[count] = i ;
count ++ ;
}
}
}
void shuffle()
{
for(int i=0;i<blockIndexArray.Length;i++)
{
int j = Random.Range(0,blockIndexArray.Length);
int t = blockIndexArray[i];
blockIndexArray[i] = blockIndexArray[j];
blockIndexArray[j] = t;
}
}
void Start () {
setupArray();
shuffle();
for ( int i = 0 ; i < blockIndexArray.Length ; i ++ ){
Debug.Log( blockIndexArray[i] );
}
}
// Update is called once per frame
void Update () {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment