Unity将List打乱,乱序(洗牌等)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Random = System.Random;
public class Outorder : MonoBehaviour
{
public List<int> testLixt = new List<int>();
void Start()
{
for (int i = 0; i < 10; i++)
{
testLixt.Add(i);
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Outoforder(testLixt);
}
}
#region 乱序
public List<T> Outoforder<T>(List<T> bag)
{
Random randomNum = new Random();
int index = 0;
T temp;
for (int i = 0; i < bag.Count; i++)
{
index = randomNum.Next(0, bag.Count - 1);
if (index != i)
{
temp = bag[i];
bag[i] = bag[index];
bag[index] = temp;
}
}
return bag;
}
#endregion
}
Start执行Add方法
通过按键来打乱List