团结冻结 - (Unity Newbie)

发布于 2025-01-30 19:50:20 字数 1533 浏览 4 评论 0 原文

我在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);
   
  }
}

这是我使用过的教程:

https://www.youtube.com/watch?v=-pj5o5i_wl4&amp;t=1263s

I'm totally newbie in Unity and C#, and will apprecaite your Help.
i'm trying to make tower Defense Game,with help of Tutorial, but it seems to be a Problem with while under my Game Loop,it will freez each time i use While. and something else that i notice is EnemyIDsToSummon.Count is whole the time 0.
here you can see my 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);
   
  }
}

and here is the tutorial i've used:

https://www.youtube.com/watch?v=-Pj5o5I_Wl4&t=1263s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2025-02-06 19:50:20

您已经写了一个Unity coroutine gameloop ,其中有一个时循环。

引擎将进入WARE循环,并且永远不会离开,因为没有任何内容将 loopshould true ,或者脱离循环。

通常,您会通过从内部 the while循环中的coroutine(收益率返回null )来解决此问题,然后设置 loopshouldend to false 在您要停止的框架上,或在该框架内部产卵后结束时循环(您可以 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 to true, 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 setting LoopShouldEnd to false on the frame you want it to stop, or ending the while loop once you're done spawning inside that frame (you can break 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文