ここでは、Unity エディターとスタンドアロンプレイヤーをバッチモードで実行するときに、サポートされる機能について説明します。
Unity を使用する際に、以下のビルトイン コルーチン オペレーターで機能を加えることができます。
以下の表は、エディター、スタンドアロンプレイヤー、それぞれをバッチモードコマンドライン引数を使用してバッチモードで実行する場合に、どの演算子がサポートされるかを示しています。
Editor | エディター (バッチモード) | Unity スタンドアロンプレイヤー | Unity スタンドアロンプレイヤー (バッチモード) | |
---|---|---|---|---|
AsyncOperation |
Yes | Yes | Yes | Yes |
WaitForEndOfFrame |
Yes | No* | Yes | Yes |
WaitForFixedUpdate |
Yes | Yes | Yes | Yes |
WaitForSeconds |
Yes | Yes | Yes | Yes |
WaitForSecondsRealtime |
Yes | Yes | Yes | Yes |
WaitUntil |
Yes | Yes | Yes | Yes |
WaitWhile |
Yes | Yes | Yes | Yes |
* -batchmode
でエディターを実行する場合は、 WaitForEndOfFrame
を使用できません。なぜなら、アニメーション、物理演算、タイムラインなどのシステムがエディターで正しく動作しない可能性があるからです。なぜなら、WaitForEndOfFrame
を使用すると Unity がこれらのシステムを更新しないことが原因です。
エディターでコルーチンを使用してコードを実行するには、再生ボタンを押します。
エディターをバッチモードで起動するときにコルーチンを実行するには、コマンドラインに以下を入力します。
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 に追加
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.