|
|
@@ -5,9 +5,11 @@ using cfg;
|
|
|
using System.Threading.Tasks;
|
|
|
using Base.Common;
|
|
|
using System.Linq;
|
|
|
+using System.Threading;
|
|
|
|
|
|
public class EnemyCreater : MonoBehaviour
|
|
|
{
|
|
|
+ private CancellationTokenSource _cancellationTokenSource;
|
|
|
public static EnemyCreater instance;
|
|
|
[SerializeField] public List<SingleCreateEnemyConfig> cfgCreateEnemy;
|
|
|
public List<bool> createdEnemy;
|
|
|
@@ -34,6 +36,7 @@ public class EnemyCreater : MonoBehaviour
|
|
|
|
|
|
private void Start()
|
|
|
{
|
|
|
+ _cancellationTokenSource = new CancellationTokenSource();
|
|
|
curLevel = GetComponent<LevelSelect>().curLevelID;
|
|
|
cfgCreateEnemy = GameManager.instance.allCfgData.CfgCreateEnemy.DataList;
|
|
|
createdEnemy = new List<bool>();
|
|
|
@@ -86,7 +89,12 @@ public class EnemyCreater : MonoBehaviour
|
|
|
// }
|
|
|
//}
|
|
|
}
|
|
|
-
|
|
|
+ void OnDestroy()
|
|
|
+ {
|
|
|
+ // 当 GameObject 销毁(如游戏停止)时取消任务
|
|
|
+ _cancellationTokenSource?.Cancel();
|
|
|
+ _cancellationTokenSource?.Dispose();
|
|
|
+ }
|
|
|
public void OnGameTimeChange(float gameTime)
|
|
|
{
|
|
|
int waveId = -1;
|
|
|
@@ -134,7 +142,15 @@ public class EnemyCreater : MonoBehaviour
|
|
|
Vector3 pos = new Vector3(cfgCreateEnemy.Position[j], cfgCreateEnemy.Position[j + 1], 0);
|
|
|
CreateEnemy(cfgCreateEnemy, cfgCreateWave, pos, true);
|
|
|
}
|
|
|
- await Task.Delay((int)(TimeInterval * 1000));
|
|
|
+ try
|
|
|
+ {
|
|
|
+ await Task.Delay((int)(TimeInterval * 1000), _cancellationTokenSource.Token);
|
|
|
+ }
|
|
|
+ catch(TaskCanceledException)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
List<int> randomPos = new(cfgCreateEnemy.Position);
|
|
|
for (int i = 0; i < num2; i++)
|