using System.Collections.Generic; using UnityEngine; using System.Threading.Tasks; using UnityEngine.Rendering; public class SpawnMonster : Base { public static SpawnMonster intance; [Header("生成位置")] public List SpawnLocations; [Header("临时索引")] private int index=1; public int Index { get => index; set { index=value; StartSpawning(); } } public List enemysList=new List(); private void Awake() { intance = this; } void Start() { Base.GlobalObj.GetComponent().OnGamePlay += this.StartSpawning; //StartSpawning(); } public void UpdateNodeList() { // 获取所有带有 SortingGroup 组件的敌人对象 List gameObjectsList = new List(); foreach (GameObject go in enemysList) { // 获取该物体上的 SortingGroup 组件 if (go != null) { SortingGroup sortingGroup = go.GetComponent(); if (sortingGroup != null && go.activeSelf) { // 添加到列表中 gameObjectsList.Add(go); } } } // 按照 y 轴排序 gameObjectsList.Sort((a, b) => a.transform.position.y.CompareTo(b.transform.position.y)); // 设置 SortingGroup 和子 Canvas 的 sortingOrder for (int i = 0; i < gameObjectsList.Count; i++) { SortingGroup sortingGroup = gameObjectsList[i].GetComponent(); if (sortingGroup != null) { // 计算排序顺序 int order = gameObjectsList.Count - i + 2; sortingGroup.sortingOrder = order; // 获取子 Canvas 并设置其 sortingOrder Canvas canvas = gameObjectsList[i].GetComponentInChildren(); if (canvas != null) { canvas.sortingOrder = order; } } } } void Update() { UpdateNodeList(); } public async void StartSpawning() { foreach (Wave value in MapLevelJsonRead.instance.waves) { if (value.wave == index) { /*foreach (var enemy in value.enemies) { await Task.Delay(enemy.startTime * 3); GameObject go = GameObject.Instantiate(Monster_Infos.instance.GetMonster(enemy.id), SpawnLocations[value.spawnPoint - 1].position, Quaternion.identity); go.GetComponent().index = value.spawnPoint-1; go.GetComponent().gold = enemy.Reward; enemysList.Add(go); }*/ for (int i = 0; i < value.enemies.Count;) { if(Time.timeScale == 0) { await Task.Delay(1000); } else { var enemy = value.enemies[i]; await Task.Delay(enemy.startTime * 3); GameObject go = GameObject.Instantiate( Monster_Infos.instance.GetMonster(enemy.id), SpawnLocations[value.spawnPoint - 1].position, Quaternion.identity ); go.GetComponent().index = value.spawnPoint - 1; go.GetComponent().gold = enemy.Reward; enemysList.Add(go); i++; } } } } } private void OnDisable() { Base.GlobalObj.GetComponent().OnGamePlay -= this.StartSpawning; } }