using DG.Tweening;
using KetosGames.SceneTransition;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//子物体按名称排序
public class GameObjectSort : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
[ContextMenu("从小到大排序")]
void Sort() {
foreach (Transform item in GetTransforms(gameObject))
{
item.SetAsLastSibling();
}
}
[ContextMenu("从大到小排序")]
void Sort2()
{
foreach (Transform item in GetTransforms(gameObject))
{
item.SetAsFirstSibling();
}
}
/// <summary>
/// 返回 children transforms, 根据名称排列 .
/// </summary>
public Transform[] GetTransforms(GameObject parentGameObject)
{
if (parentGameObject != null)
{
List<Component> components = new List<Component>(parentGameObject.GetComponentsInChildren(typeof(Transform)));
List<Transform> transforms = components.ConvertAll(c => (Transform)c);
transforms.Remove(parentGameObject.transform);
transforms.Sort(delegate (Transform a, Transform b)
{
return a.name.CompareTo(b.name);
});
return transforms.ToArray();
}
return null;
}
}
unity 子物体按名称排序
最新推荐文章于 2023-02-15 15:46:23 发布