Version: 2022.3
Unity Standalone Player command line arguments
基于文本的场景文件

批处理模式和内置协程兼容性

本页将介绍在批处理模式下运行 Unity Editor 和独立平台播放器 (Standalone Player) 时支持的功能。

运行 Unity 时,以下内置协程运算符可添加功能:

下表显示了 Unity 在 Editor 和独立平台播放器中支持的运算符,还包括使用 -batchmode 命令行参数以批处理模式运行两者时的运算符支持情况:

Editor Editor -batchmode Unity 独立平台播放器 Unity 独立平台播放器 -batchmode
AsyncOperation
WaitForEndOfFrame 否*
WaitForFixedUpdate
WaitForSeconds
WaitForSecondsRealtime
WaitUntil
WaitWhile

* 使用 -batchmode 运行 Editor 时,不能使用 WaitForEndOfFrame,因为动画、物理和时间轴等系统可能无法在 Editor 中正常工作。这是因为 Unity 在使用 WaitForEndOfFrame 时当前不会更新这些系统。

运行协程

在 Editor 中

在 Editor 中,按下“Play”按钮以使用协程运行代码。

批处理模式下的 Editor

要在以批处理模式启动 Editor 时从命令行运行协程,请输入:

C:\Program Files\Unity\Editor\Unity.exe -projectPath PROJECT_PATH -batchMode

在独立平台播放器中

启动独立平台播放器以运行您的代码。播放器会加载,然后等待协程完成。

批处理模式下的独立平台播放器

要在以批处理模式启动播放器时从命令行运行协程,请输入:

PATH_TO_STANDALONE_BUILD -projectPath PROJECT_PATH -batchMode

例如,在 Windows 上:

C:\projects\myproject\builds\myproject.exe -batchMode

在 Mac 上:

~/UnityProjects/myproject/builds/myproject -batchMode

协程脚本示例

AsyncOperation

using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(Example_AsyncTests());
    }

    public IEnumerator Example_AsyncTests()
    {
        Debug.Log("Start of AsyncLoad Example");
        
        var load = UnityEngine.Resources.LoadAsync("");
        yield return load;
        yield return null;
        
        Debug.Log("End of AsyncLoad Example");
    }
}

WaitForEndOfFrame

using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(Example_WaitForEndOfFrame_Coroutine());
    }

    public IEnumerator Example_WaitForEndOfFrame_Coroutine()
    {
        Debug.Log("Start of WaitForEndOfFrame Example");
        
        yield return new WaitForEndOfFrame();
        
        Debug.Log("End of WaitForEndOfFrame Example");
    }
}

WaitForFixedUpdate

using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(Example_WaitForFixedUpdate_Coroutine());
    }

    public IEnumerator Example_WaitForFixedUpdate_Coroutine()
    {
        Debug.Log("Start of WaitForFixedUpdate Example");
        
        yield return new WaitForFixedUpdate();
        
        Debug.Log("End of WaitForFixedUpdate Example");
    }
}

WaitForSeconds

using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(Example_WaitForSeconds_Coroutine());
    }

    public IEnumerator Example_WaitForSeconds_Coroutine()
    {
        Debug.Log("Start of WaitForSeconds Example");
        
        yield return new WaitForSeconds(1.5f);
        
        Debug.Log("End of WaitForSeconds Example");
    }
}

WaitForSecondsRealtime

using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(Example_WaitForSecondsRealtime_Coroutine());
    }

    public IEnumerator Example_WaitForSecondsRealtime_Coroutine()
    {
        Debug.Log("Start of WaitForSecondsRealtime Example");
        
        yield return new WaitForSecondsRealtime(1.5f);
        
        Debug.Log("End of WaitForSecondsRealtime Example");
    }
}

WaitUntil

using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(Example_WaitUntil_Coroutine());
    }

    public IEnumerator Example_WaitUntil_Coroutine()
    {
        Debug.Log("Start of WaitUntil Example");
        
        yield return new WaitUntil(() => Time.time > 5.0f);
        
        Debug.Log("End of WaitUntil Example");
    }
}

WaitWhile

using System.Collections;
using UnityEngine;

[ExecuteInEditMode]
public class ExampleClass : MonoBehaviour
{
    public void Start()
    {
        StartCoroutine(Example_WaitWhile_Coroutine());
    }

    public IEnumerator Example_WaitWhile_Coroutine()
    {
        Debug.Log("Start of WaitWhile Example");
        
        yield return new WaitWhile(() => Time.time < 5.0f);
        
        Debug.Log("End of WaitWhile Example");
    }
}

  • 2018–06–06 页面已发布

  • 在 2017.4 版中添加了关于使用 batchmode 和协程的建议

Unity Standalone Player command line arguments
基于文本的场景文件