本页将介绍在批处理模式下运行 Unity Editor 和独立平台播放器 (Standalone Player) 时支持的功能。
运行 Unity 时,以下内置协程运算符可添加功能:
The following table shows which of these operators Unity supports inside the Editor and Standalone Player, and when running each of them in batch mode using the -batchmode command line argument:
Editor | Editor -batchmode | Unity 独立平台播放器 | Unity 独立平台播放器 -batchmode | |
---|---|---|---|---|
AsyncOperation |
是 | 是 | 是 | 是 |
WaitForEndOfFrame |
是 | 否* | 是 | 是 |
WaitForFixedUpdate |
是 | 是 | 是 | 是 |
WaitForSeconds |
是 | 是 | 是 | 是 |
WaitForSecondsRealtime |
是 | 是 | 是 | 是 |
WaitUntil |
是 | 是 | 是 | 是 |
WaitWhile |
是 | 是 | 是 | 是 |
* 使用 -batchmode
运行 Editor 时,不能使用 WaitForEndOfFrame
,因为动画、物理和时间轴等系统可能无法在 Editor 中正常工作。这是因为 Unity 在使用 WaitForEndOfFrame
时当前不会更新这些系统。
在 Editor 中,按下“Play”按钮以使用协程运行代码。
要在以批处理模式启动 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
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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 和协程的建议