Unity 스크립팅 API 가이드
이 문서는 Unity의 스크립팅 API와 관련된 내용을 설명합니다. 이 가이드를 통해 Unity에서 코루틴을 사용하는 방법과 다양한 예제들을 살펴보겠습니다.
배치 모드와 코루틴
Unity의 배치 모드는 에디터와 스탠드얼론 플레이어에서 코드 실행을 간소화할 수 있는 기능입니다. 배치 모드에서는 Unity를 GUI 없이 실행할 수 있으며, 이는 자동화된 작업에 유용합니다.
다음은 에디터와 스탠드얼론 플레이어에서 지원되는 코루틴 연산자들입니다.
| 연산자 | 에디터 | 에디터 -batchmode | 스탠드얼론 플레이어 | 스탠드얼론 플레이어 -batchmode |
|---|---|---|---|---|
| AsyncOperation | 지원 | 지원 | 지원 | 지원 |
| WaitForEndOfFrame | 지원 | 미지원* | 지원 | 지원 |
| WaitForFixedUpdate | 지원 | 지원 | 지원 | 지원 |
| WaitForSeconds | 지원 | 지원 | 지원 | 지원 |
| WaitForSecondsRealtime | 지원 | 지원 | 지원 | 지원 |
| WaitUntil | 지원 | 지원 | 지원 | 지원 |
| WaitWhile | 지원 | 지원 | 지원 | 지원 |
- 에디터를 -batchmode로 실행할 경우,
WaitForEndOfFrame을 사용할 수 없습니다. 이는 이 시스템이 업데이트되지 않기 때문입니다.
코루틴 실행 방법
에디터 내에서 코루틴 실행
에디터에서 "Play" 버튼을 누르면 코루틴이 포함된 코드가 실행됩니다.
배치 모드에서 코드 실행
커맨드 라인에서 배치 모드로 에디터를 실행하려면 다음 명령어를 입력합니다.
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
코루틴 예제
다음은 다양한 yield 연산자를 사용하는 예제 코드입니다.
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");
}
}
마무리
이 문서에서는 Unity에서 코루틴을 사용하는 방법과 그 예제들을 소개하였습니다. 이러한 방법들을 통해 효율적으로 작업을 처리하고, 배치 모드에서의 필요성을 이해하며 활용할 수 있습니다. Unity를 이용한 개발 작업에 도움이 되길 바랍니다.