Created
October 8, 2016 21:50
-
-
Save naojitaniguchi/95e6ce669d5558321896c64d31c316e6 to your computer and use it in GitHub Desktop.
Shuffling array sample for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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