81 lines
1.4 KiB
C#
81 lines
1.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
|
|
public class FishDelaySpawner : MonoBehaviour
|
|
{
|
|
[Serializable]
|
|
public struct stFish
|
|
{
|
|
public FishSpawner fishSpawner;
|
|
|
|
public stFish(FishSpawner fishSpawner_)
|
|
{
|
|
fishSpawner = fishSpawner_;
|
|
}
|
|
}
|
|
|
|
[ReadOnly]
|
|
public List<stFish> fishes = new List<stFish>();
|
|
|
|
public int maxSpawnPerFrame = 5;
|
|
|
|
private bool spawnIsRun;
|
|
|
|
private static FishDelaySpawner instance;
|
|
|
|
public bool isLock;
|
|
|
|
public static FishDelaySpawner Instance
|
|
{
|
|
get
|
|
{
|
|
if (!instance)
|
|
{
|
|
instance = UnityEngine.Object.FindObjectOfType<FishDelaySpawner>();
|
|
}
|
|
return instance;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (fishes.Count > 0 && !spawnIsRun)
|
|
{
|
|
spawnIsRun = true;
|
|
StartCoroutine(SpawnAllFish(0.1f));
|
|
}
|
|
}
|
|
|
|
public IEnumerator SpawnAllFish(float delay)
|
|
{
|
|
while (fishes.Count > 0)
|
|
{
|
|
while (isLock)
|
|
{
|
|
yield return 0.001f;
|
|
}
|
|
isLock = true;
|
|
Debug.Log("JKM spawn num fish " + fishes.Count + "_start at " + Time.time);
|
|
List<stFish> fishes2 = fishes;
|
|
fishes = new List<stFish>();
|
|
isLock = false;
|
|
foreach (stFish item in fishes2)
|
|
{
|
|
Fish fish = item.fishSpawner.SpawnFish();
|
|
fish.Initialize();
|
|
yield return 0.5f;
|
|
}
|
|
Debug.Log("JKM spawn end at " + Time.time);
|
|
}
|
|
spawnIsRun = false;
|
|
}
|
|
|
|
public void AddFish(FishSpawner fishSpawner_)
|
|
{
|
|
fishes.Add(new stFish(fishSpawner_));
|
|
}
|
|
}
|