条件检查后 Unity 中的协程功能异常
The Coroutine in Unity functionality anamoly after condition checking
我是 Unity 的新手,我只是在创建一个简单的银河射击游戏,我希望只有当玩家出现时才会产生敌人。所以我创建了一个协程来检查 playerExists
条件,如果结果是 true
,它应该进一步继续每 5 秒生成一次敌人。但出于某种原因,它只会产生一个敌人。我在这里遗漏了什么吗?
下面是我的 SpawnManager,控制生成行为。
public class SpawnManager : MonoBehaviour {
[SerializeField]
private GameObject _enemyShipPrefab;
[SerializeField]
private GameObject[] _powerUp;
UIManager _uimanager;
// Use this for initialization
void Start () {
_uimanager = GameObject.Find("Canvas").GetComponent<UIManager>();
StartCoroutine(SpawnPowerUps());
StartCoroutine(SpawnEnemy());
}
IEnumerator SpawnEnemy(){
while (_uimanager.playerExists == true)
{
Vector3 position = new Vector3(Random.Range(-8.23f, 8.23f), 5.7f, 0.0f);
Instantiate(_enemyShipPrefab, position, Quaternion.identity);
yield return new WaitForSeconds(5.0f);
}
}
}
下面是我的 UIManager,我控制播放器的存在。
public class UIManager : MonoBehaviour {
// Use this for initialization
public bool playerExists = false;
public int playerScores = 0;
public Sprite[] lives;
public Image playerLivesImagesToBeShown;
public Text playerScoreToBeShown;
public Image titleImage;
public GameObject playerPrefab;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && playerExists == false){
titleImage.gameObject.SetActive(false);
Instantiate(playerPrefab, new Vector3(0, 0, 0), Quaternion.identity);
playerScores = 0;
playerScoreToBeShown.text = "Score : 0";
playerExists = true;
}
}
public void updateLives(int livesToView ){
playerLivesImagesToBeShown.sprite = lives[livesToView];
if(livesToView == 0){
playerExists = false;
titleImage.gameObject.SetActive(true);
}
}
乍一看你的代码(和描述的问题)我会说,你的 SpawnEnemy()
协程运行并随后退出。
您必须将其锁定在某个循环中,如下所示:
IEnumerator SpawnEnemy ()
{
while (true) // Keep checking
{
if(_uimanager.playerExists == true)
{
Vector3 position = new Vector3(Random.Range(-8.23f, 8.23f), 5.7f, 0.0f);
Instantiate(_enemyShipPrefab, position, Quaternion.identity);
yield return new WaitForSeconds (5.0f); // After spawning waits 5secs
}
yield return null; // Starts loop with next frame.
}
}
我是 Unity 的新手,我只是在创建一个简单的银河射击游戏,我希望只有当玩家出现时才会产生敌人。所以我创建了一个协程来检查 playerExists
条件,如果结果是 true
,它应该进一步继续每 5 秒生成一次敌人。但出于某种原因,它只会产生一个敌人。我在这里遗漏了什么吗?
下面是我的 SpawnManager,控制生成行为。
public class SpawnManager : MonoBehaviour {
[SerializeField]
private GameObject _enemyShipPrefab;
[SerializeField]
private GameObject[] _powerUp;
UIManager _uimanager;
// Use this for initialization
void Start () {
_uimanager = GameObject.Find("Canvas").GetComponent<UIManager>();
StartCoroutine(SpawnPowerUps());
StartCoroutine(SpawnEnemy());
}
IEnumerator SpawnEnemy(){
while (_uimanager.playerExists == true)
{
Vector3 position = new Vector3(Random.Range(-8.23f, 8.23f), 5.7f, 0.0f);
Instantiate(_enemyShipPrefab, position, Quaternion.identity);
yield return new WaitForSeconds(5.0f);
}
}
}
下面是我的 UIManager,我控制播放器的存在。
public class UIManager : MonoBehaviour {
// Use this for initialization
public bool playerExists = false;
public int playerScores = 0;
public Sprite[] lives;
public Image playerLivesImagesToBeShown;
public Text playerScoreToBeShown;
public Image titleImage;
public GameObject playerPrefab;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && playerExists == false){
titleImage.gameObject.SetActive(false);
Instantiate(playerPrefab, new Vector3(0, 0, 0), Quaternion.identity);
playerScores = 0;
playerScoreToBeShown.text = "Score : 0";
playerExists = true;
}
}
public void updateLives(int livesToView ){
playerLivesImagesToBeShown.sprite = lives[livesToView];
if(livesToView == 0){
playerExists = false;
titleImage.gameObject.SetActive(true);
}
}
乍一看你的代码(和描述的问题)我会说,你的 SpawnEnemy()
协程运行并随后退出。
您必须将其锁定在某个循环中,如下所示:
IEnumerator SpawnEnemy ()
{
while (true) // Keep checking
{
if(_uimanager.playerExists == true)
{
Vector3 position = new Vector3(Random.Range(-8.23f, 8.23f), 5.7f, 0.0f);
Instantiate(_enemyShipPrefab, position, Quaternion.identity);
yield return new WaitForSeconds (5.0f); // After spawning waits 5secs
}
yield return null; // Starts loop with next frame.
}
}