【问题标题】:Unity 3D CustomDebugger with timeScale = 0 not stopping FixedUpdateUnity 3D CustomDebugger with timeScale = 0 不停止 FixedUpdate
【发布时间】:2018-07-09 01:09:33
【问题描述】:

制作一个调试器,在播放场景时按下按钮即可启动。一旦调试为真,游戏需要在代码中调用一个名为 DebugStep 的函数处完全暂停,然后从该点继续向前。这是我的脚本附加到画布按钮

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ToggleDebugButton : MonoBehaviour {

//private TimeDebugger timeDebugger;

[Header("Debugger")]
public bool isDebugging;
public string debugStep;

[Header("Basic Properties")]
public bool isMoving;
public bool isAttacking;
public bool isSwinging;
private BasicProperties basicProperties;

void Start()
{
    basicProperties = GameObject.Find("MrPresident").GetComponent<BasicProperties>();
    isDebugging = false;
    GetComponent<Button>().onClick.AddListener(TogglePause);
}
public void TogglePause()
{
    isDebugging = !isDebugging;
    if ( !isDebugging )
    {
        Time.timeScale = 1f;
    }  
}

public void FixedUpdate()
{
    DebugStep("DebugUpdateEnd");
}

public void DebugStep(string source)
{
    if (isDebugging)
    {
        isMoving = basicProperties.isMoving;
        isAttacking = basicProperties.isAttacking;
        isSwinging = basicProperties.isSwinging;

        debugStep = source;
        print("start set timescale 0");
        Time.timeScale = 0f;
        print("end set timescale 0");
    }
}

}

问题是当在 FixedUpdate 中调用 DebugStep 时,第一个打印语句和第二个打印语句会执行,即使它们之间的时间刻度已设置为 0。当我尝试从其他脚本中的 FixedUpdate 内部调用 DebugStep 时,这是一个更大的问题,我希望在 DebugStep 调用之后暂停整个 FixedUpdate 脚本,然后在我取消暂停时从该点继续。

我认为这不会发生,因为当我在其他地方的 FixedUpdate 中放置了 2 个 DebugStep 调用时,当 timeScale 应该设置为 0 并在第一次打电话。我认为所有脚本中属于当前 FixedUpdate 周期的所有 FixedUpdates 都会完成,即使 timeScale 在其中设置为 0 也是如此。我对么?实际上我真的不确定,因为如果我在 FixedUpdate 中有 2 次以上对 DebugStep 的调用,那么游戏每个周期只会停止一次

有没有办法改变这一点,让游戏在 Timescale = 0 时完全停止,然后在我将 Timescale 设置回 1 时从那个确切的点恢复?谢谢

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    您的代码工作正常,应该如何。

    来自docs:“timeScale 影响 Time 类的所有时间和增量时间测量变量。”

    此外,它还会影响 Update、Lateupdate 和 fixedupdate 方法的调用次数。

    它不会影响代码执行的“速度”。

    因此,当您输入更新方法并更改时间刻度时,您只会在退出更新循环后才注意到结果。它不会像您期望/希望的那样在更新功能中神奇地停止。

    如果您希望等待特定功能完成后再继续执行某个代码块,您可能需要使用coroutines

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 2018-04-18
      相关资源
      最近更新 更多