本页将介绍在批处理模式下运行 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 中,按下“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 和协程的建议
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.