50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class testzzz : MonoBehaviour
|
|
{
|
|
private GameObject[] gameObjects;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
public void UpdateNodeList()
|
|
{
|
|
// 获取所有子物体中具有 SpriteRenderer 组件的 GameObject
|
|
List<GameObject> gameObjectsList = new List<GameObject>();
|
|
foreach (Transform child in transform)
|
|
{
|
|
SpriteRenderer renderer = child.GetComponent<SpriteRenderer>();
|
|
if (renderer != null)
|
|
{
|
|
gameObjectsList.Add(child.gameObject);
|
|
}
|
|
}
|
|
|
|
// 按照 y 轴排序
|
|
gameObjectsList.Sort((a, b) => a.transform.position.y.CompareTo(b.transform.position.y));
|
|
|
|
// 设置 sortingOrder 并打印排序结果
|
|
for (int i = 0; i < gameObjectsList.Count; i++)
|
|
{
|
|
Debug.Log(gameObjectsList[i].name);
|
|
SpriteRenderer renderer = gameObjectsList[i].GetComponent<SpriteRenderer>();
|
|
if (renderer != null)
|
|
{
|
|
renderer.sortingOrder = gameObjectsList.Count - i;
|
|
}
|
|
}
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
UpdateNodeList();
|
|
}
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
}
|