- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
- 纳金币
- 53488
- 精华
- 316
|
实现这种功能有很多种,下面是我个人认为比较好的实现方法- /*
- * @ 创建时间:2015.8.12.8: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);
- }
- }
- }
复制代码 |
|