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를 이용한 개발 작업에 도움이 되길 바랍니다.

Read more

Unity 매뉴얼 스크립팅 API 해설

이 문서는 Unity의 매뉴얼 스크립팅 API에 대한 간단한 해설과 활용 예제들을 포함하고 있습니다. Unity는 게임 개발 플랫폼으로, 스크립팅 API를 통해 게임의 다양한 기능을 제어하고 수정할 수 있습니다. 버전 Unity 스크립팅 API는 여러 버전으로 제공됩니다. 주의 깊게 선택하여 사용하는 것이 중요합니다. 버전 설명 2023.2 최신 기능 및 버그 수정이 추가됨

By 이재협/실장/시스템개발실/PHYSIA

Unity 매뉴얼 스크립팅 API 설명서 해설

이 문서는 Unity의 매뉴얼 스크립팅 API에 대한 정보를 제공하며, 버전에 따라 다르게 적용되는 내용들을 설명합니다. 본 문서에서는 주요 내용을 간단히 정리하고 활용 가능 예제를 통해 이해를 돕겠습니다. 기본 개념 Unity에서 스크립팅 API는 게임 오브젝트와 그들의 동작을 제어하기 위한 강력한 도구입니다. 이를 통해 게임의 로직, 물리 엔진, 애니메이션 및 사용자 인터페이스를

By 이재협/실장/시스템개발실/PHYSIA

Unity 스크립팅 API 가이드

이 문서는 Unity의 스크립팅 API에 대해 설명합니다. Unity는 게임 개발을 위한 인기 있는 엔진으로, 강력한 스크립팅 기능을 제공합니다. 이 가이드는 Unity에서 스크립트를 작성하고 사용하는 방법을 이해하는 데 도움을 드립니다. 목차 * Unity 스크립팅 소개 * 기본 스크립트 생성 * 스크립트 사용 예제 * 응용 프로그램 * 참고 자료 Unity 스크립팅 소개 Unity는 C# 프로그래밍 언어를

By 이재협/실장/시스템개발실/PHYSIA