Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Test
- {
- public class Test : MonoBehaviour
- {
- // Change this to your Images.
- public string [ ] ImageNames = { "One", "Two", "Three", "Four" };
- private int [ ] order;
- private void Awake ( )
- {
- var count = ImageNames.Length;
- // Create an order array that we will shuffle, leaving the original array intact.
- order = new int [ ImageNames.Length ];
- for ( int i = 0; i < count; i++ )
- order [ i ] = i;
- // Shuffle the order array, so that you can then select the right Images.
- // This order array now holds a reference to the original Image array.
- order.Shuffle ( );
- // Demonstrate that the order has been shuffled.
- System.Text.StringBuilder sb = new System.Text.StringBuilder ( );
- for ( int i = 0; i < order.Length; i++ )
- {
- sb.Append ( ImageNames [ order[i] ] );
- if ( i < order.Length - 1 )
- sb.Append ( ", " );
- }
- Debug.Log ( sb.ToString ( ) );
- }
- }
- public static class ArrayExtension
- {
- private static readonly System.Random rng = new System.Random ( );
- public static void Shuffle<T> ( this T [ ] array )
- {
- int n = array.Length;
- while ( n > 1 )
- {
- n--;
- int k = rng.Next ( n + 1 );
- T value = array [ k ];
- array [ k ] = array [ n ];
- array [ n ] = value;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment