纳金网
标题:
根据物体离目标点的距离进行排序
[打印本页]
作者:
may
时间:
2015-11-19 06:59
标题:
根据物体离目标点的距离进行排序
实现这种功能有很多种,下面是我个人认为比较好的实现方法
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Sort : MonoBehaviour {
public List<Transform> list;
// Use this for initialization
void Start () {
list = new List<Transform>();
AddAllCubes();
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
DisplaySortOnGUI();
if (GUILayout.Button("SortObjsByDistance"))
{
SortObjsByDistance();
}
}
void AddAllCubes()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("cube");
foreach(GameObject cube in go)
{
list.Add(cube.transform);
}
}
void SortObjsByDistance()
{
list.Sort(delegate(Transform t1,Transform t2)
{
Debug.Log(Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position)));
return Vector3.Distance(t1.position, transform.position).CompareTo(Vector3.Distance(t2.position, transform.position));
});
}
void DisplaySortOnGUI()
{
foreach (Transform t in list)
{
GUILayout.Label(t.name);
}
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5