我希望转换此脚本,以使用示例快速排序,冒泡排序,选择排序等中的任何类型的排序算法对代码进行排序。
有没有办法替换
instantiatedObjects = instantiatedObjects.OrderBy(go => go.name).ToList();
并将示例中的排序方法更改为快速排序?是否有必要使用数组或使用列表这样可以使它与快速排序和其他排序算法一起使用?
脚本在场景中简要地创建游戏对象的克隆然后对它们进行排序,然后它会破坏原始游戏对象,因此克隆将是场景中唯一排序的对象。
这是我的剧本;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
namespace Assets {
class Gameobjects : MonoBehaviour {
public Button s_YourButton;
[SerializeField]
private GameObject[] deck;
public List<GameObject> instantiatedObjects;
void Start() {
Button btn = s_YourButton.GetComponent<Button>();
//Calls the TaskOnClick method when you click the Button
btn.onClick.AddListener(TaskOnClick);
}
List<Vector3> vectorList = new List<Vector3>();
void TaskOnClick() {
Fill();
instantiatedObjects = instantiatedObjects.OrderBy(go => go.name).ToList();
for (int i = 0; i < instantiatedObjects.Count; i++) {
instantiatedObjects[i].transform.position = vectorList[i];
}
string name = "1";
string name1 = "2";
string name2 = "3";
string name3 = "4";
string name4 = "5";
GameObject go1 = GameObject.Find(name);
GameObject go2 = GameObject.Find(name1);
GameObject go3 = GameObject.Find(name2);
GameObject go4 = GameObject.Find(name3);
GameObject go5 = GameObject.Find(name4);
//if the tree exist then destroy it
if (go1 & go2 & go3 & go4 & go5) {
Destroy(go1.gameObject);
Destroy(go2.gameObject);
Destroy(go3.gameObject);
Destroy(go4.gameObject);
Destroy(go5.gameObject);
}
}
public void Fill() {
vectorList.Clear();
instantiatedObjects = new List<GameObject>();
for (int i = 0; i < deck.Length; i++) {
GameObject spawnedObject = Instantiate(deck[i]) as GameObject;
instantiatedObjects.Add(spawnedObject);
vectorList.Add(spawnedObject.transform.position);
}
}
}
}