团结冻结 - (Unity Newbie)
我在Unity和C#方面完全是新手,并感谢您的帮助。
我正在尝试在教程的帮助下制作塔式防御游戏,但是在我的游戏循环下,这似乎是一个问题,每次我在时使用时都会冻结。我注意到的其他内容是
eyneidstosummon.count
是整个时间 0
。
在这里您可以看到我的gameloop:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameLoopManager : MonoBehaviour
{
private static Queue<int> EnemyIDsToSummon;
public bool LoopShouldEnd;
private void Start()
{
EnemyIDsToSummon= new Queue<int>();
EntitySummoner.Init();
StartCoroutine(GameLoop());
InvokeRepeating("SummonTest", 0f, 1f);
}
void SummonTest(){
EnqueueEnemyIDToSummon(1);
}
IEnumerator GameLoop(){
while(LoopShouldEnd==false){
//Spawn Enemies
if(EnemyIDsToSummon.Count>0)
{
for(int i=0;i<EnemyIDsToSummon.Count;i++){
EntitySummoner.SummonEnemy(EnemyIDsToSummon.Dequeue());
}
}
//Spawn Towers
//Move Enemies
//Tick Towers
//Apply Effects
//Damge Enemies
//Remove Enemies
// remove Towers
}
yield return null;
}
public static void EnqueueEnemyIDToSummon(int ID){
EnemyIDsToSummon.Enqueue(ID);
}
}
这是我使用过的教程:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经写了一个Unity coroutine
gameloop
,其中有一个时循环。引擎将进入WARE循环,并且永远不会离开,因为没有任何内容将
loopshould
true
,或者脱离循环。通常,您会通过从内部 the while循环中的coroutine(
收益率返回null
)来解决此问题,然后设置loopshouldend
tofalse
在您要停止的框架上,或在该框架内部产卵后结束时循环(您可以break
立即结束)在段循环的情况下,它只会在while循环完成后发生,这是永远不会发生的,因此没有其他代码运行,并且编辑器会冻结。
关于Coroutines的一些有用的文档:
https://docs.unity3d.com/manual/coroutines.html
You've written a Unity coroutine
GameLoop
, which has a while loop in it.The engine will enter the while loop and never leave, because nothing sets
LoopShouldEnd
totrue
, or breaks out of the loop.Usually, you'd fix this by yielding from the coroutine (
yield return null
) from inside the while loop, and settingLoopShouldEnd
tofalse
on the frame you want it to stop, or ending the while loop once you're done spawning inside that frame (you canbreak
to end it immediately)When the yield is outside the while loop, it will only happen after the while loop finishes, which is never, so no other code runs and the editor freezes.
Some useful documentation on coroutines:
https://docs.unity3d.com/Manual/Coroutines.html